Q1 output (33 marks, 11 lines -> 3 marks per line) Exception: not an int Finally: stringValue = -1 a.test() = -1 Finally: stringValue = -1 a.test() = -1 Finally: stringValue = 27 Resetting stringValue to -1 a.test(27) = 27 Exception: not an int 9999999999999999999999999999999999999999 Finally: stringValue = -1 a.test() = -1 Q2 (6 marks each for a total of 30) (a) What is the relationship between the classes Dictionary and Hashtable? Hashtable is a subclass of Dictionary (b) What is the protocol for the Enumeration interface? you always have to call hasMoreElements and nextElement in tandem: hasMoreElements, nextElement, hasMoreElements, nextElement, ... (c) What do you have to do to make a class Serializable, and which stream classes are used to write and read serialized objects from and to files? specify that the class ".. implements Serializable" ObjectInputStream and ObjectOutputStream (d) Will the statements in the finally clause of a try block always be executed? If your answer is no, please supply an example where the finally block is not executed. Tricky question for marking: If you just say no, (without a valid example): 3 marks no + valid example: 6 marks plain Yes: 5 marks yes, in general but + valid counter-example: 6 marks (e) Which two stream classes allow us to establish a pipe between two threads? How does a pipe synchronise the two threads it serves? PipedInputStream and PipedOutputStream implicitly using the internal buffer: if the buffer is full output operations will be blocked, if the blocked if empty, input operations will be blocked. Q3a (21 marks) class IntStackEnumeration implements Enumeration { private int index = tos; public boolean hasMoreElements() { index--; return (index >= 0); } // must wrap it into an Integer, unfortunately public Object nextElement() { return new Integer(v[index]);} } Q3b (16 marks) push may throw an exception (in theory, should not happen here, but anyway. so either you should be using a try block (were we have even accepted an empty catch body, or modify the method definition into "... throws Exception { ..." public static void testIntStack(int n) { IntStack is = new IntStack(2*n); try { for(int i = n; i>0; i--) is.push(i); } catch (Exception e) { System.out.println(e); System.exit(1); } Enumeration e = is.elements(); while(e.hasMoreElements()) System.out.println(e.nextElement()); }