Quiz 3: 1 Exceptions
Quiz 3: 1 Exceptions
Quiz 3
CS1027A University of Western Ontario
1 Exceptions
Consider the following code fragment:
1
Consider the following Java interface for the Stack ADT:
1 public interface StackADT {
2 public void push(T element);
3 public T pop();
4 public T peek();
5 }
Consider now the following code fragment:
1 StackADT<String> s = new StackADT<String>();
2 StackADT<String> q = new StackADT<String>();
3 if (s.equals(q)) {
4 System.out.println("equal");
5 }
6 else {
7 System.out.println("different");
8 }
3. What does the code fragment above output when it is executed?
(a) ”equal” (b) ”different” (c) Nothing. The above code has compilation errors.
2
Consider now the following code fragment:
10. What is output during line 8? Assume that the Person class has no toString() method.
(a) Something like ”Person@42gf32” (b) NullPointerException (c) ”ABC”
3
Note: Some of these multiple choice questions have multiple correct answers.
2 Concepts
11. What is it called when you see < T > on the same line as the class name?
(a) Variable typing (b) Inheritance (c) Polymorphism (d) Generics
14. Where in the singly linked list collection does the activity happen?
(a) Front of the list (b) Middle of the list (c) Rear of the list
(d) Always at the front, but after that everywhere can have activity
18. Stacks are useful for changing the order of something in what way?
(a) Sorting (b) Reversing (c) Preserving (doesn’t change the order) (d) Randomizing
19. Queues are useful for changing the order of something in what way?
(a) Sorting (b) Reversing (c) Preserving (doesn’t change the order) (d) Randomizing
20. What can change between different implementations for the same collection?
(a) The result of the ADT’s operations
(b) The amount of work done by the implementation
(c) The amount of memory used by the implementation
4
3 Stacks
Evaluation of: 7 4 -3 * 1 5 + / *
Indices
2
1
0
Input 7 4 -3 * 1 5 + / *
top top top top top top top top top top
21. Evaluate the postfix expression by drawing in Figure 1 and showing the contents of the
stack after each input.
22. The answer would have been different if the stack was implemented using a linked list
instead of an array.
(a) True (b) False
23. After evaluating a well-formed postfix expression, the stack will contain 1 element; this
is the answer to the expression.
(a) True (b) False
5
Consider a stack implemented using a singly linked list where top and count are instance
variables pointing to the first node of the list and giving the number of data items in the
stack, respectively (see Figure 2 for an example of such a stack). Consider now the following
implementation of the push operation:
top a b
count = 2
Figure 2
24. Starting from the original example stack in Figure 2, draw the stack after performing
push(c), using the code fragment above showing the implementation of push() (include top
and count).
25. Starting from the original example stack shown in Figure 2, draw the stack after
performing push2(c), using the code fragment above showing the implementation of push2()
(include top and count).
6
4 Queues
Consider a queue implemented using a circular array where front, rear, and count are instance
variables pointing to the front of the queue, the rear of the queue, and containing the number
of elements in the queue, respectively (see Figure 3 for an example of such a queue). Consider
now the following implementation of the enqueue() operation:
1 public void enqueue(T element) {
2 if (size() == queue.length)
3 expandCapacity()
4 queue[rear] = element;
5 rear = (rear + 1) % queue.length;
6 count++;
7 }
queue 0 1 2 3 4
cq
C D
front
rear
count
Figure 3
26. Consider Figure 3. What sequence of operations (using enqueue() and dequeue()) could
have already happened to result in this queue? Assume that the queue started empty and
that we have only been adding letters in alphabetical order.
27. Starting from the original example queue shown in Figure 3, draw the queue after
performing both enqueue(”E”); and enqueue(”F”);.
28. At what point will enqueue() method call the expandCapacity() method?
7
29. How can you use a queue to represent a repeating key when encoding a string?
30. Compare the original Caesar cipher to a cipher that uses a repeating key.
31. Use the repeating key {3, 4, 1} to encode the string ”Hello World”. Show all of your
work:
(’H’ + = , Q = { , , }):
8
5 Linked Lists
32. Starting from the linked list shown in Figure 4, assume that we have created a new
LinearNode called newNode holding the letter E, and we wish to add it between D and F.
Trace through the add() method shown below to add E between D and F, filling in the table
below as you go. Interpret the statement element < front.getElement() to be comparing
alphabetical order.
front B D F
Figure 4
Fill in the following table while you trace through the code fragment above on Figure 4:
Location in code current.getValue() previous.getValue() element Enter loop?
Line 5 (first time) null
Line 5 (second time)
Line 5 (third time)
9
34. Starting from the linked list shown in Figure 4, we wish to remove D from the middle
of the list. Trace through the remove() method shown below to remove D, drawing the
appropriate arrows in each diagram (see Figure 5) from front, current, previous, B, D, and
F. Include the arrow representing null where appropriate.
front B D F front B D F
front B D F
previous current
(c)
Figure 5: Draw your answer on this diagram. (a) The linked list right before entering the
while loop. (b) The linked list after 1 iteration of the loop. (c) The linked list after removing
D.
10
35. Starting from the linked list shown in Figure 4, assume that we have created a new
LinearNode called newNode holding the letter A, and we wish to add it to the front of the
linked list. Trace through the addToFront() method shown below to add A at the front of
the list, drawing the appropriate arrows in the diagram (see Figure 6) from front, newNode,
A, B, D, and F. Include the arrow representing null where appropriate.
newNode A
front B D F
Figure 6
11
6 Algorithm Design
Write your algorithm in Java code or in detailed Java-like pseudocode like the one used in
the lecture notes.
36. Consider an array A storing n > 1 integers. Write an algorithm inverse(A, n) that
reverses the order of the integers stored in A. So, for example, if the array A is as in the
figure on the left, the modified array must be as in the figure on the right.
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8
A 5 2 7 1 0 3 6 4 8 A 8 4 6 3 0 1 7 2 5
Figure 7
You can either use a stack or a queue in your solution, but not both; you need to decide
which of these auxiliary data structures you need to use to answer this question. You cannot
use any other additional data structures.
The only operations that you can perform on the stack are push(), pop(), peek(), and
isEmpty(); you can write Stack auxStack = new Stack() to create an empty stack. If you
decide to use a queue the only operations that you can perform on the queue are enqueue(),
dequeue(), first(), and isEmpty(); you can write Queue auxQueue = new Queue() to create
an empty queue.
If you use pseudocode, you must use sufficient detail. For example, you can write A[i] = x
or s.push(A[i]), but you cannot write statements like ”add x to the array A”, or ”remove
element x from A”.
12