Câu hỏi: CSD201 Cấu trúc dữ liệu và giải thuật – Bộ ôn tập thi lại số 2
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
O(n^2)O(1)O(log n)O(n)Node f = tail.prev; // prev is a link to predecessor node
Node q = new Node(x);
q.prev = f; q.next = tail;
f.next = q; tail.prev = q;O(n)O(1)O(log n)O(n^2)void fun()
{ if (isEmpty()) return;
if (head == tail)
head = tail = null;
else head = head.next;
}Specify the correct statement about the fun() method in the code above (choose the most suitable one).
int x = 5;
Node f = head;
while(f.next != tail) f = f.next;
Node q = new Node(x);
q.next = tail;
f.next = q;int x = 5;
Node f = head;
while(f.next != p) f = f.next;
Node q = new Node(x);
q.next = p;
f.next = q;Node p1,p2; p1 = p.next;
p2 = p1.next;
p.next = p2;
if(p2!=null) p2.prev=p;int x = 35;
Node f = p.prev; // prev is a link to predecessor node
Node q = new Node(x); // Create new node with value x
q.prev = f; q.next = p;
f.next = q; p.prev = q;int x = 5;
Node q = new Node(x);
q.prev=null;
q.next = head;
head.prev = q;
head = q;Object dequeue()
{ if (isEmpty()) return(null);
return(pool.removeFirst());
}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.removeLast());
}Đáp án đúng: "Object dequeue() {
if (isEmpty()) return(null);
return(pool.removeFirst());}". Đề quy ước đầu danh sách chính là đầu hàng đợi, mà hàng đợi lấy ra ở đầu, nên phép dequeue phải gỡ phần tử đầu; với java.util.LinkedList thì removeFirst làm đúng việc đó trong thời gian hằng số, lại trả về đối tượng vừa gỡ nên kiểu trả về Object là hợp. "Object dequeue() {
if (isEmpty()) return(null);
return(pool.removeLast());}" sai vì removeLast gỡ ở cuối, biến hàng đợi thành ngăn xếp vào sau ra trước. "
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ề giá trị null, dequeue lại không cần tham số đầu vào, và nó cũng gỡ ở cuối. "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 có kiểu trả về Object, ngoài ra nó vẫn gỡ phần tử cuối thay vì phần tử đầu.
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 "Go*od**Mor*ni**ng*Si*r" ?
donrnoodonrniMdonrniGodoringiodoringSwhile(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*Mor**ni***ngSir" ?
riSgnooGiriSgnoGoriSgnooGGMongSriSgnooSObject pop()
{if (isEmpty()) return;
return(pool.remove(pool.size()-1));
}void pop(Object x)
{ if (isEmpty()) return(null);
pool.remove(pool.size()-1);
}Object pop()
{ if (isEmpty()) return(null);
return(pool.remove(pool.size()-1));
}Object pop()
{ if (isEmpty()) return(null);
return(pool.remove(pool.size()));
}Đá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.
void push(Object x)
{ Node p = new Node(x);
p.next = head;
head=p;
}void push(Object x)
{ Node p = new Node(x);
p.next = head;
}void push(Object x)
{ Node p = new Node(x);
p.next = null;
head=p;
}void push(Object x)
{ Node p = new Node(x);
p.next = head;
head=p.next;
}Đáp án đúng: "
void push(Object x) {
Node p = new Node(x);
p.next = head;
head=p;
}". Với danh sách liên kết đơn mà nút đầu là đỉnh ngăn xếp, phép đẩy phải làm đúng hai việc theo đúng thứ tự: cho nút mới trỏ sang đỉnh cũ bằng p.next = head, rồi dời chính đỉnh về nút mới bằng head = p; nhờ vậy toàn bộ phần dưới của ngăn xếp vẫn liền mạch. Bản "
void push(Object x) {
Node p = new Node(x);
p.next = head;
}" thiếu lệnh dời đỉnh, nên head vẫn chỉ vào nút cũ và nút vừa tạo không ai trỏ tới, ngăn xếp coi như không thay đổi. Bản "
void push(Object x) {
Node p = new Node(x);
p.next = null;
head=p;
}" cắt liên kết tới phần cũ: head chỉ vào nút mới nhưng nút mới trỏ vào null, nên mọi phần tử đã đẩy trước đó mất hết. Bản "
void push(Object x) {
Node p = new Node(x);
p.next = head;
head=p.next;
}" gán head bằng p.next, mà p.next vừa được đặt bằng head, nên head giữ nguyên giá trị cũ và nút mới bị bỏ rơi.
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 "H*owAre***You**To****Day" ?
owAreYouToHowAreYouHowAreYouTwAreYouTowhile(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
}
while(the queue is not empty)dequeue and write the dequeued character to the screen What is written to the screen for the input "Goo***dAft**er**no**on" ?