Câu hỏi: CSD201 Cấu trúc dữ liệu và giải thuật – Bộ ôn tập cuối kỳ số 1
50 câu trắc nghiệm kèm lời giải của môn CSD201 - Cấu trúc dữ liệu và giải thuật, xếp đúng thứ tự trong đề.
Danh sách câu hỏi
void fun(int n)
{if(n > 0)
{ fun(n-2);
System.out.print(" " + n );
}
}What is the output when the statement fun(9); is run?
O(nlog n)O(log n)O(n)O(n^2)Object dequeue()
{ if (isEmpty()) return(null);
return(pool.remove(pool.size()));
}void dequeue(Object x)
{ if (isEmpty()) return(null);
pool.remove(pool.size()-1);
}Object dequeue()
{if (isEmpty()) return;
return(pool.remove(pool.size()-1));
}Object dequeue()
{ if (isEmpty()) return(null);
return(pool.remove(0));
}Đá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.
int x = 5;
Node f = p.prev; //prev is a link to predecessor node
Node q = new Node(x);
q.prev = f; q.next = p;
f.next = q; p.prev = q;while(there are more characters in the word to read){read a character if a character is '*' then dequeue and write the dequeued character to the screen
else
enqueue the character into the queue
}What is written to the screen for the input ""GoodA**fter***Noo*n" ?
AdoretfoGoodAfodAfterGoodAvoid printOut(int count)
{if((count % 2) == 0)
System.err.println("StdErr: Line " + count + "\n");
else
System.out.println("StdOut: Line " + count + "\n");
if(count == 0) return;
else
printOut(count-1);
}What value for X the statement printOut(X); will print EXACTLY 5 lines to standard output (Lines start with "StdOut:"), in the fewest number of recursive calls?
while(there are more characters in the word to read){read a character if a character is '*' then dequeue the queue
else
enqueue the character into the queue
}How the queue looks like after processing the input "Hello****Worl**d*"?
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 "GoodM**orn****in*g" ?
MdnronoMdnroonMdnrooGMdonroo