Specify the correct implementation of pop() method of a stack of Integers. 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: "Integer pop() {
if (isEmpty()) return(null);
return((Integer) pool.remove(pool.size()-1));}". Đỉnh ngăn xếp là cuối danh sách, nên lấy ra phải xoá phần tử ở chỉ số pool.size()-1 rồi trả về chính phần tử vừa xoá; khi ngăn xếp rỗng thì trả null để người gọi biết không có gì lấy, vẫn khớp kiểu trả về Integer đã khai báo. Phương án "Integer pop() {
if (isEmpty()) return;
return((Integer) pool.remove(pool.size()-1));}" dùng lệnh return trống trong hàm có kiểu trả về nên không biên dịch được. Phương án "
void pop(Integer x) {
if (isEmpty()) return(null);
pool.remove(pool.size()-1);
}" vừa trả giá trị trong hàm void, vừa nhận tham số vô nghĩa, lại không đưa phần tử lấy ra cho người gọi. Phương án "Integer pop() {
if (isEmpty()) return(null);
return(pool.remove(pool.size()));}" lấy ở chỉ số pool.size(), vượt quá chỉ số hợp lệ cuối cùng là pool.size()-1, nên luôn gây lỗi chỉ số ngoài phạm vi.
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