Specify the correct implementation of pop() method of a stack. This stack uses java.util.LinkedList 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.removeLast());}". Vì cuối danh sách được coi là đỉnh ngăn xếp và java.util.LinkedList có sẵn removeLast(), lệnh này vừa tháo nút cuối vừa trả về chính phần tử đó trong một bước, đúng ngữ nghĩa của pop là lấy ra và trả về đỉnh; kiểu trả Object cho phép trả null khi ngăn xếp rỗng. Vì vậy "
void pop(Object x) {
if (isEmpty()) return(null);
pool.remove(pool.size()-1);
}" sai hai chỗ: hàm void không được return(null), lại nhận tham số và không trả gì nên người gọi không bao giờ nhận được phần tử ở đỉnh. "Object pop() {
if (isEmpty()) return;
return(pool.remove(pool.size()-1));}" sai vì hàm khai báo trả Object mà có lệnh return trống, trình biên dịch báo thiếu giá trị trả về. "Object pop() {
if (isEmpty()) return(null);
return(pool.remove(pool.size()));}" sai vì chỉ số hợp lệ chỉ từ 0 đến size()-1, nên remove(pool.size()) luôn vượt biên và 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