Specify the correct implementation of dequeue() method of a queue. This queue uses java.util.ArrayList 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.remove(0));}". Đề quy ước đầu danh sách là đầu hàng đợi, mà dequeue phải gỡ ở đầu, nên với java.util.ArrayList cách đúng là remove(0): nó trả về đối tượng vừa gỡ nên khớp kiểu trả về Object, và phần tử ở chỉ số 0 chính là phần tử vào sớm nhất. "Object dequeue() {
if (isEmpty()) return(null);
return(pool.remove(pool.size()));}" sai vì chỉ số hợp lệ của ArrayList chỉ từ 0 tới size()-1, truyền size() sẽ ném IndexOutOfBoundsException. "
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ề null, dequeue không cần tham số đầu vào, và nó còn gỡ ở cuối danh sách. "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 trả về Object, ngoài ra gỡ phần tử cuối là hành vi của ngăn xếp chứ không phải hàng đợi.
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