2053 First Review 08 Name________________________ 1. _______How does a subclass set up memory for its parent class ? a) Creates an instance of it in one of its methods. b) Calls the parent class constructor in the first line of its constructor. c) Doesn’t have to set it up, Java does it automatically. d) Two of these could be correct. 2. ___The instruction super( ) used in a constructor; does which of the following? a) calls the method super as defined in the current class b) calls the method super as defined in the current class’ parent class c) calls the method super as defined in java.lang d) calls the constructor as defined in the parent class 3) Which of the following is considered an Abstract Data Type? a) array b) reference variable c) any of the primitive types (e.g., int, double, char) d) queue e) all of the above 4. ____Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by a) importing classes b) implementing interfaces c) overriding parent class methods d) creating aliases e) using public rather than protected or private modifiers 5. Which of the following interfaces would be used to implement a class that represents a group (or collection) of objects? a) List - the interface for ArrayList class b) Speaker c) Comparable d) MouseListener e) KeyListener 6) _____In order to determine the type that a polymorphic variable refers to, the decision is made a) by the programmer at the time the program is written b) by the compiler at compile time c) by the operating system when the program is loaded into memory d) by the Java run-time environment at run time e) more than one can be true For questions 7-8, use the following partial class definitions: public class A1 { public int x; private int y; protected int z; … } public class A2 extends A1 1 2053 First Review 08 Name________________________ { protected int a; private int b; … } public class A3 extends A2 { private int q; … } 7)_____Which of the following is true with respect to A1, A2 and A3? a) A1 is a subclass of A2 and A2 is a subclass of A3 b) A3 is a subclass of A2 and A2 is a subclass of A1 c) A1 and A2 are both subclasses of A3 d) A1, A2 and A3 are all subclasses of the class A e) None are correct 8)______ Which of the following lists of instance data are accessible in class A3? a) x, y, z, a, b b) x, z, q, a c) x, z, a, b d) z, a, b,a, b 9) What would happen if a subclass does not explicitly call the constructor of its parent class using super()? a) b) c) d) e) The program will not compile under any circumstances The program compiles but crashes during run time If the parent class does not have a constructor, the default constructor for parent and child are called. If the parent class has a constructor with one or more parameters, it will not compile. More than one of the above. 10. Suppose that a selectionsort of 100 items has completed 42 iterations of the main loop. How many items are now guaranteed to be in their final spot (never to be moved again)? A. 21 B. 41 C. 42 D. 43 11. When the compiler compiles your program, how is a recursive call treated differently than a non-recursive method call? A. Primitive values are all treated as reference variables B. Reference variables are all treated as primitive values C. There is no duplication of local variables D. None of the above Consider this method declaration: 2 2053 First Review 08 Name________________________ void quiz(int i){ if (i > 1) { System.out.print("*"); quiz(i / 2); } } 12. How many asterisks are printed by the method call quiz(9)? A. 3 B. 9 C. 8 D. Some other number 13. In a linked list queue, we keep track of a front node and a rear node with two reference variables. Which of these reference variables will change during insertion into a NON-EMPTY queue? A. Neither changes B. Only front changes. C. Only rear changes. D. Both change. 14. What is the worst-case time for binary search finding a single item in an array? A. Constant time B. Logarithmic time C. Linear time (N) D. Quadratic time 15. In the array version of the Stack class, which operations require linear time for their worst-case behavior? A. is_empty () B. peek () C. pop () D. push when the stack is at capacity E. None of these operations require linear time. 16. _____ Compare the worst-case big-O analysis for these two methods: The add method for the unsorted ArrayList class which allows no duplicates, and the add method for the linked list class. A. Add for ArrayList is a constant big-O except when the list must be expanded B. Addnode for LinkedList is a constant big-O C. Add for both is a big-O of N D. Two or more are correct. 17. Consider the following method: public static void testb(int n){ if (n>0) testb(n-4); System.out.print(n + " "); } 3 2053 First Review 08 Name________________________ What is printed by the call testb(13)? A. –3 1 5 9 13 B. 1 5 9 13 C. 13 9 5 1 -3 D. 13 9 5 1 E None of the above ___________________________ 18. What does the following method compute? Assume it is called initially with i = 0 public int question9(String a, char b, int i) { if (i = = a.length( )) return 0; else if (b = = a.charAt(i)) return question9(a, b, i+1) + 1; else return question9(a, b, i+1); } a) The length of String a b) The length of String a concatenated with char b c) The number of times char b appears in String a d) Returns 1 if char b appears in String a at least once, and 0 otherwise e) The char which appears at location i in String a The following method recognizes whether a String parameter consists of a specific pattern and returns true if the String has that pattern, false otherwise. Use this recursive method to answer questions. Please note that the second parameter to substring(firstindex, lastindex ) method of the String class, treats lastindex as lastindex-1. uses lastindex-1 public boolean patternRecognizer(String a) { if(a == null) return false; else if (a.length( ) = = 1 | | (a.length( ) = = 2 && a.charAt(0) = = a.charAt(1) ) ) return true; else if (a.length( ) = = 2 && a.charAt(0) != a.charAt(1) ) return false; else if (a.charAt(0) == a.charAt(a.length( ) - 1)) return patternRecognizer(a.substring(1, a.length( ) - 1)); else return false; } 19. Which String below would result in patternRecognizer returning true? a) “abcba” b) “aaabbb” c) “abcde” d) “aabba” e) all of the above Strings will result in the method returning true 20. What are the complexities of the following methods in terms of the big O notation (worst case?) (a. ) int product(int N) { int result = 1; for (int k=1;k<=N; k++) result = result * k; 4 2053 First Review 08 Name________________________ return result; } 1. 2. 3. 4. O(NlgN) O(1) O(N) O(N2) ( b.) Suppose n = array.length int search (int target, int [] array) { for (int k=1; k<=n; k = k* 2) { if( target==array[k]) return k; } return –1; } 1. O(lgN) 2. O(1) 3. O(N) 4. O(N2) (c.) Suppose n = array.length int search(int target, int [] array, int pos) { if(pos>= n) return –1; else if(array[pos]==target) return pos; else return search(target, array, pos*2); } 1. O(lgN) 2. O(1) 3. O(N) 4. O(N2) ************************************************************************ (d) Suppose n = array.length void sort(int [] array) { n = array.length; for(int i=0; i<n-1;i++) for(int j=i+1; j<n; j++) if(a[i]>a[j]) { int temp = a[i]; a[i]=a[j]; a[j]=temp; } } 5 2053 First Review 08 Name________________________ 1. O(NlgN) 2. O(1) 3. O(N) 4. O(N2) *********************************************************************** (e) count = 0; 0<N <10000 Assume these values for the method for(i = 1; i <= N; i++) for(j = 10; j < i; j++) count++; 1. O(NlgN) 2. O(1) 3. O(N) 4. O(N2) ________________ 21. If we implement a stack with an array and insist on keeping top at index 0, what is the Big(O) for push(). ________________ 1. O(NlgN) 2. O(1) 3. O(N) 4. O(N2) 22. How does the computer use stacks in method calls a) It uses them to store unused blocks of memory b) It uses a stack to keep track to method calls c) It uses a stack to store data swapped in from the hard drive d) More than one of the above e) None of the above 23. In a selectionsort of n elements, how many times is the swap function called in the complete execution of the algorithm? A. 1 B. n - 1 C. n log n D. n² 24. Suppose we are sorting an array of ten integers using a some quadratic sorting algorithm. After four iterations of the algorithm's main loop, the array elements are ordered as shown here: 1 3 54 0 6 7 8 9 Which statement is correct? (Note: Our selectionsort picks smallest items first.) A. The algorithm might be either selectionsort or insertionsort. B. The algorithm might be selectionsort, but could not be insertionsort. C. The algorithm might be insertionsort, but could not be selectionsort. D. The algorithm is neither selectionsort nor insertionsort. 25. When is insertionsort a good choice for sorting an array? A. Each component of the array requires a large amount of memory. B. Each component of the array requires a small amount of memory. C. The array has only a few items out of place. 6 2053 First Review 08 Name________________________ D. The processor speed is fast. Short Answers 1. Here is an array with exactly 16 elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Suppose that we are doing a serial search for an element in an array with N elements. Using the above array, circle the elements that will be found by examining N-5 items from the array. 2. Here is an array with exactly 16 elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Suppose that we are doing a binary search for an element. Circle any elements that will be found by examining 4 or fewer numbers from the array. 7