COMP 1006/1406 - Summer 2016 Sample Final Choose the best answer for each and record your solution in both the scantron sheet and this exam. If you think there is no answer to any question then answer (e). Good luck! This sample is meant to show you the style of questions that will appear on the final exam. The questions on the final will be similar to these. This sample is NOT meant to indicate the proportion of questions of any type (or topic) that will be on the final exam. The exam covers the entire semester from start to end. There will, however, not be many questions on generics or exceptions (there will be some but not many). 1) Which object-oriented programming principle is method overriding NOT a part of? (a) polymorphism (b) encapsulation (c) inheritance 2) What is the correct term for the situation when the compiler cannot make a decision about the specific behaviour that should be executed by an object and this behaviour is instead determined at runtime? (a) early binding (b) mid binding (c) late binding 3) How many primitive data types does Java have? (a) 4 (b) 6 (c) 8 4) What is the accessibility of a private attribute? (a) Any class in the same directory has access to this attribute (b) Only the class itself has access to the this attribute (c) Only the class and its subclasses have access to this attribute 5) Consider a Java program called Run that is executed as follows java Run One Two Three In the main method, what is the value of args.length? (a) 3 (b) 4 (c) 5 Page 1/6 COMP 1006/1406 - Summer 2016 1 2 3 4 Sample Final public class Top{ int top = 1; public Top(int top){ this.top = top; } } 5 6 7 8 9 10 11 12 public class Middle extends Top{ public Middle(int top){ super(top); this.top = this.top + top; } ---- missing line ---} 13 14 15 16 17 public class Bottom extends Middle{ public Bottom(){ super(3); } public Bottom(int top){ super(top); this.top = top; } } 18 These class definitions are used for the next two questions on this page. 6) Which of the following declarations is invalid? (a) Top top = new Top() (b) Top top = new Top(3) (c) Middle middle = new Bottom() 7) Given the following declaration Top t = new Middle(2);, what is the value of t.top? (a) 1 (b) 2 (c) 4 8) Consider the following declarations String x = ``study''; String y = new String(``study''); String z = y; Which of the following evaluates to false? (a) x.equals(y) (b) x == y (c) z == y Page 2/6 COMP 1006/1406 - Summer 2016 Sample Final 9) Consider the following declarations Double[] x = new Double[10]; double[] y = new double[10]; Which of the following is true about the memory allocated in the heap for these variables? (a) the memory needed for each array is the same (b) the memory needed for x is smaller than the memory needed for y (c) the memory needed for x is larger than the memory needed for y 10) If the string representation of a Queue shows the first element as the leftmost character of the string and the last element as the rightmost element of the string, then what would the string representation of the Queue q be after the following sequence of operations? Queue q = new Queue(); q.add(A); q.add(B); q.add(C); q.add(q.remove()); q.add(q.peek(); q.add(D); Note: peek() retruns the first element of the queue (but does not remove it). (a) BCAD (b) ABCAD (c) BCABD 11) Consider the two arrays int[] a = {2,1,3,2,4}; int[] b = {4,2,3,3,2}; What does a[b[a[1]]] evaluate to? (a) 1 (b) 2 (c) 3 (d) 4 Page 3/6 COMP 1006/1406 - Summer 2016 Sample Final 12) Consider the following Java code int z = 2; for(int i = 3; i < 6; i += 1){ for(int j = 1; j < i-2; j += 1){ z = i % j; if(z == 1){ System.out.println(i); break; } } } What is the output? (a) 4 (b) 5 (c) 6 13) Consider the following program public static long mystery(int n){ if(n<=0){ // be careful of the base case return -1; }else if(n%2 == 0){ // n is even return n + mystery(n-1) + mystery(n-3); }else{ // n is odd return n + mystery(n-2) - mystery(n-1); } } public class Exam{ public static void main(String[] args){ System.out.println(mystery(6)); } } What is printed out when this Exam program is run? Page 4/6 COMP 1006/1406 - Summer 2016 1 2 3 4 5 public class Exam{ public static final public static int public String private final int Sample Final w = 3 x; y; z = 1; 6 7 8 9 public Exam(String course){ this.course = course; } 10 11 12 13 14 15 16 17 18 19 public static void main(String[] args){ String s1 = new String(``dog''); String s2 = ``dog''; Integer n1 = 13; double n2 = 3.2; Exam[] n3 = new Exam[n1]; n3[0] = new Exam("COMP1006/1406"); Exam.x = 3; } Consider the Exam program above just after line 18 of the main method has executed but the program has not yet ended. 14) [Repeat the question for all variables] Where is the data for the attribute {w,x,y,z,s1,s2,n1,n3} stored in memory? (a) the stack (b) the heap (c) the data segment A picture of a binary tree would be given for the next two questions. 15) Given a binary tree ..., which of the following would access the node with value 12? (a) root.left.right.left.data (b) root.right.data (c) root.left.left.right.right.data 16) Given a binary tree ...., which of the following traversals would produce ...? (a) preorder traversal (b) inorder traversal (c) postorder traversal Page 5/6 COMP 1006/1406 - Summer 2016 Sample Final 17) The quicksort sorting algorithm has a partition helper function. What is the runtime of partition? (a) constant (b) logarithmic (c) linear 18) What does System.out.println(5 + ‘‘cat’’); print? (a) "5cat" (b) "fivecat" (c) this causes an error and so nothing is printed Page 6/6