Specify the correct implementation of dequeue() method of a queue. This queue uses java.util.LinkedList for storing data and the head of the list is treated as the head of the queue. (Choose the most suitable one)
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "Object dequeue() {
if (isEmpty()) return(null);
return(pool.removeFirst());}". Đề quy ước đầu danh sách chính là đầu hàng đợi, mà hàng đợi lấy ra ở đầu, nên phép dequeue phải gỡ phần tử đầu; với java.util.LinkedList thì removeFirst làm đúng việc đó trong thời gian hằng số, lại trả về đối tượng vừa gỡ nên kiểu trả về Object là hợp. "Object dequeue() {
if (isEmpty()) return(null);
return(pool.removeLast());}" sai vì removeLast gỡ ở cuối, biến hàng đợi thành ngăn xếp vào sau ra trước. "
void dequeue(Object x) {
if (isEmpty()) return(null);
pool.remove(pool.size()-1);
}" sai ngay ở chữ ký: hàm void không được trả về giá trị null, dequeue lại không cần tham số đầu vào, và nó cũng gỡ ở cuối. "Object dequeue() {
if (isEmpty()) return;
return(pool.remove(pool.size()-1));}" sai vì lệnh return trống không biên dịch được trong hàm có kiểu trả về Object, ngoài ra nó vẫn gỡ phần tử cuối thay vì phần tử đầu.
Cùng phần 2
Select the statement that is most correct.
Which of the following applications may not use a stack?Có lời giảiCâu hỏiConsider the following pseudocode:
declare a stack of characters
while(there are more characters in the word to read)
{read a character
if a character is '*' then
pop the stack
else
push the character into the stack
}
while(the stack is not empty)
pop and write the poped character to the screen
What is written to the screen for the input "Good**Morn**in***g" ?Có lời giảiCâu hỏiConsider the following pseudocode:
declare a stack of characters
while(there are more characters in the word to read)
{read a character
if a character is '*' then
pop and write the poped character to the screen
else
push the character into the stack
}
What is written to the screen for the input "Good**Morn**i***ng" ?Có lời giảiCâu hỏiCả 51 câu của chủ đề51 câu
Xuất hiện trong 1 bộ đề
Đi tiếp từ đây