Specify the correct implementation of pop() method of a stack. This stack uses java.util.ArrayList for storing data and the end of the list is treated as the top of the stack. (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 pop() {
if (isEmpty()) return(null);
return(pool.remove(pool.size()-1));}". Cuối danh sách được coi là đỉnh ngăn xếp, mà java.util.ArrayList truy cập theo chỉ số nên phần tử đỉnh có chỉ số size()-1; lệnh remove(pool.size()-1) vừa tháo phần tử đó vừa trả chính nó về, đúng ngữ nghĩa pop là lấy ra và trả về, và xoá ở cuối mảng không phải dồn phần tử nào nên vẫn rẻ. Vì vậy "Object pop() {
if (isEmpty()) return;
return(pool.remove(pool.size()-1));}" sai vì hàm khai báo trả Object mà lại có lệnh return trống, không biên dịch được. "
void pop(Object x) {
if (isEmpty()) return(null);
pool.remove(pool.size()-1);
}" sai vì hàm void không thể return(null), lại nhận tham số và không trả gì nên phần tử ở đỉnh bị mất luôn thay vì được giao cho người gọi. "Object pop() {
if (isEmpty()) return(null);
return(pool.remove(pool.size()));}" sai vì chỉ số hợp lệ chỉ tới size()-1, nên remove(pool.size()) luôn ném IndexOutOfBoundsException.
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