Title: CS1102 Midterm
1CS1102 Midterm
- Max Tan
- tanhuiyi_at_comp.nus.edu.sg
- COM1-01-09 Tel65164364http//www.comp.nus.edu.s
g/tanhuiyi
2Question 2
- Which statement(s) about interfaces are true?
- I. An interface contains only public abstract
methods and public static final fields - II. If a class implements an interface and then
fails to implement any methods in that interface
then the class must be declared abstract - III. While a class may implement just one
interface, it may extend more than 1 class - (A) I only
- (B) I and II only
- (C) I and III only
- (D) II and III only
- (E) I, II and III
Its obvious III is wrong!
3Question 3
- Which of the following is/are most suitable to be
used as underlying data structure for
implementing a queue ADT. - I. Array
- II. BasicLinkedList
- III. ExtendedLinkedList
- IV. TailLinkedList
- V. CircularLinkedList
- I and V
- II and III
- I and IV
- III and V
- All of them
4Question 4
- Which of the following statement(s) about
recursion is/are true? - I. Every recursive algorithm can be written
iteratively - II. Tail recursion is always used in divide and
conquer algorithms - III. In a recursive definition, an object is
defined in terms of a simple case of itself
- I only
- III only
- I and II
- I and III
- II and III
5Question 6
- The question refers to the following class and
the client method mystery - public static void mystery(LinearLinkedList
list) ListNode grab, hold hold
list.getFirstNode() list.setFirstNode(null)
while(hold!null) grab hold
hold hold.getNext() grab.setNext(list.get
FirstNode()) list.setFirstNode(grab)
mystery reverse.
6Question 7
- Assume the declarations for the question
- Queue q new ListQueue() //ListQueue
implements Queueint sum 0Object obj
While(!q.isEmpty()) obj q.dequeue()
sum ((Integer)obj).intValue()
q.enqueue(obj)
- Queue temp new ListQueue()while(!q.isEmpty())
obj q.dequeue() sum
((Integer)obj).intValue() temp.enqueue(obj)
q temp
Queue temp q While(!temp.isEmpty()) obj
temp.dequeue() sum ((Integer)obj).intValu
e()
7Question 10
- What is the Big O analysis for the following code
segment?for(i 3 i lt n i) for(j 1 j
lt c j 5) for(k 1 k lt n k 2)
num
n iterations
lg n iterations
8Short Answer Question 1
- Write a non-recursive method for finding, by link
hopping, the middle node of a double linked list
which is pointed to by a head pointer. - 1. You are not allowed to use a counter to count
- 2. Return the leftmost middle node if there are
even number of nodes
9Short Answer Question 1
- Use your imagination!
- The method the lecturer wants you to answer is
simple - Step 1, move all the way to the end to get the
tail - Step 2, move one step back from the tail and one
step front from the head every iteration - Step 3, once the two pointers land on the same
node, they are in the middle element! - If they go past each other, then there is an even
number of elements, and hence, return the pointer
that has been reversing!
10Short Answer Question 1
- There is a way to do it even if you dont have a
double linkedlist! - Two pointers.
- In every iteration, one pointer move two steps
and one pointer move one step. - Once the pointer that moves two steps reaches the
end, the other pointer must be at the middle
element!
11Short Answer Question 1
- Modification to make is simple, use a
TailedLinkedList, so we dont have to do the
first sweep to locate the tail - - 10 marks for 20 words P
12Short Answer Question 2
- N x N matrix consisting of 1s and 0s so that in
any row, all 1s come before the 0s
1. Start from the first row, first column, move
the pointer until you see a zero, then move down
to the next row. 2. If we see a 1, try to check
of we can move right, if so we keep moving right
until we see a zero, and mark this row as the row
that we have seen the most 1s so far 3. If we see
a 0, we move down another row until we see a 1,
and go to step (2)
13Short Answer Question 2
- Suppose now it is an array of linked lists, the
only way is to count the number of 1s in each
row. - This would result in an O(n2) algorithm