Download presentation
Presentation is loading. Please wait.
Published byCori Bradford Modified over 9 years ago
1
AP Bowl Practice Exam AB April 16, 2005 Georgia Tech
2
Question 1 A bookstore is working on an on-line ordering system. For each type of published material (books, movies, audio tapes) they need to track the id, title, author(s), date published, and price. Which of the following would be the best design? a)Create one class PublishedMaterial with the requested fields plus type b)Create classes Book, Movie, and AudioTape and each class has the requested fields c)Create the class PublishedMaterial and have Book, Movie, and AudioTape inherit from it all the listed fields d)Create one class BookStore with the requested fields plus type e)Create classes for PublishedMaterial, Books, Movies, AudioTape, Title, Price, ID, Authors, DatePublished
3
Question 1 Answer A bookstore is working on an on-line ordering system. For each type of published material (books, movies, audio tapes) they need to track the id, title, author(s), date published, and price. Which of the following would be the best design? Answer c: Create the class PublishedMaterial and have Book, Movie, and AudioTape inherit from it all the listed fields We will need to get objects based on their type so we should create classes for Book, Movie, and AudioTape. They have common fields so we should put these in a common superclass PublishedMaterial.
4
Question 2 private ArrayList nums; // precondition: nums.size() > 0; // nums contains Integer objects public void numQuest() { int k = 0; Integer zero = new Integer(0); while (k < nums.size()) { if (nums.get(k).equals(zero)) nums.remove(k); else k++; } } ArrayList nums = [0, 0, 4, 2, 5, 0, 3, 0] What will ArrayList nums contain as a result of executing numQuest? a)[0, 4, 2, 5, 3] b)[3, 5, 2, 4, 0, 0, 0, 0] c)[0, 0, 0, 0, 4, 2, 5, 3] d)[4, 2, 5, 3] e)[0, 0, 4, 2, 5, 0, 3, 0]
5
Question 2 Answer private ArrayList nums; // precondition: nums.size() > 0; // nums contains Integer objects public void numQuest() { int k = 0; Integer zero = new Integer(0); while (k < nums.size()) { if (nums.get(k).equals(zero)) nums.remove(k); else k++; } } ArrayList nums = [0, 0, 4, 2, 5, 0, 3, 0] What will ArrayList nums contain as a result of executing numQuest ? Answer d: [4, 2, 5, 3] This code will loop through the elements of the list and if the current element is zero it will remove it from the list. This moves down all remaining elements so the current index isn’t incremented.
6
Question 3 Consider the following method: public int m1 (int a) { if (a == 1) return 10; else return 10 + m1 (a – 1); } What is the output when m1(5) is called? a)50 b)20 c)60 d)10 e)30
7
Question 3 Answer Consider the following method: public int m1 (int a) { if (a == 1) return 10; else return 10 + m1 (a – 1); } What is the output when m1(5) is called? Answer a: 50 = 10 * 5 m1(5) return 10 + m1(4) return 10 + m1(3) return 10 + m1(2) return 10+ m1(1) return 10 return 20 return 30 return 40 return 50
8
Question 4 private int [] myStuff; //precondition: myStuff contains // integers in no particular order public int mystery(int num) { for (int k = myStuff.length - 1; k >= 0; k--) { if (myStuff[k] > num) return k; } return -1; } Which of the following best describes the contents of myStuff after the following statement has been executed? int m = mystery(n); a)All values in positions m+1 through myStuff.length-1 are greater than or equal to n. b)All values in position 0 through m are less than n. c)All values in position m+1 through myStuff.length-1 are less than or equal to n. d)The smallest value is at position m. e)The largest value that is smaller than n is at position m.
9
Question 4 Answer private int [] myStuff; //precondition: myStuff contains // integers in no particular order public int mystery(int num) { for (int k = myStuff.length - 1; k >= 0; k--) { if (myStuff[k] > num) return k; } return -1; } Which of the following best describes the contents of myStuff after the following statement has been executed? int m = mystery(n); Answer c: All values in position m+1 through myStuff.length-1 are less than or equal to n. This loops backwards through the array and returns the index where it finds the value at the index in the array greater than the passed number. So once it returns, all values in the array passed that index must be less than or equal to the passed num (n).
10
Question 5 //precondition: x >=0 public void mystery (int x) { System.out.print(x % 10); if ((x / 10) != 0) { mystery(x / 10); } System.out.print(x % 10); } What is the output from mystery(4321)? a)43211234 b)1234 c)4321 d)12344321 e)32144123
11
Question 5 Answer //precondition: x >=0 public void mystery (int x) { System.out.print(x % 10); if ((x / 10) != 0) { mystery(x / 10); } System.out.print(x % 10); } What is the output from mystery(4321)? Answer d: 12344321 mystery(4321) prints 1 (4321 % 10 = 1) mystery(432) (4321 / 10) prints 2 (432 % 10 = 2) mystery(43) (432 / 10) prints 3 (43 % 10 = 3) mystery(4) (43 / 10) prints 4 (4 % 10 = 4) no recursive call prints 4 (4 % 10 = 4) prints 3 (43 % 10 = 3) prints 2 (432 % 10 = 2) prints 1 (4321 %10 = 1)
12
Question 6 Which of the following best describes the data structure represented by java.util.LinkedList? a)A doubly linked list with references to the first and last nodes only. b)A singly linked list with a reference to the first node only. c)A singly linked list with references to the first and last nodes. d)A doubly linked list with a reference to the first node only. e)A doubly linked list with reference to the first, middle, and last nodes.
13
Question 6 Answer Which of the following best describes the data structure represented by java.util.LinkedLi st? Answer a: A doubly linked list with references to the first and last nodes only.
14
Question 7 public int mystery(int n) { if (n == 0) return 1; else return 2 * mystery (n - 1); } What value is returned as the result of mystery(7)? a)128 b)256 c)64 d)0 e)2
15
Question 7 Answer public int mystery(int n) { if (n == 0) return 1; else return 2 * mystery (n - 1); } What value is returned as the result of mystery(7)? Answer a: 128 This calculates 2 to the given n mystery(7) 2 * mystery(6) 2 * mystery(5) 2 * mystery(4) 2 * mystery(3) 2 * mystery(2) 2 * mystery(1) 2 * mystery(0) = 1
16
Question 8 int [][] mat = new int [4][3]; for (int row = 0; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row < col) mat[row][col] = 1; else if (row == col) mat[row][col] = 2; else mat[row][col] = 3; } } What are the contents of mat after the code segment has been executed? a){{2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}} b){{2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}} c){{2 1 1 1}, {3 2 1 1}, {3 3 2 1}} d){{2 3 3 3}, {1 2 3 3}, {1 1 2 3}} e){{1 1 1 1}, {2 2 2 2}, {3 3 3 3}}
17
Question 8 Answer int [][] mat = new int [4][3]; for (int row = 0; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row < col) mat[row][col] = 1; else if (row == col) mat[row][col] = 2; else mat[row][col] = 3; } } What are the contents of mat after the code segment has been executed? Answer b: {{2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}} This creates an array of 4 rows and 3 columns. This code will put a 1 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 3 in the array when the row index is greater than the column index.
18
Question 9 public class Point2D { public int x; public int y; public Point2D() {} public Point2D(int x,int y) { this.x = x; this.y = y; } // other methods } public class Point3D extends Point2D { public int z; // other code } Which of the following constructors would be valid for Point3D? I. public Point3D() {} II. public Point3D(int x, int y, int z) { super(x,y); this.z = z; } III. public Point3D(int x, int y) { this.x = x; this.y = y; this.z = 0; } a)I only b)II only c)III only d)I and II only e)I, II, and III
19
Question 9 Answer public class Point2D { public int x; public int y; public Point2D() {} public Point2D(int x,int y) { this.x = x; this.y = y; } // other methods } public class Point3D extends Point2D { public int z; // other code } Which of the following constructors would be valid for Point3D? I. public Point3D() {} II. public Point3D(int x, int y, int z) { super(x,y); this.z = z; } III. public Point3D(int x, int y) { this.x = x; this.y = y; this.z = 0; } Answer e: I, II, and III Point2D does have a no-arg constructor so I is okay. Point2D does have a constructor that takes x and y so II is okay. And x and y are public so III is okay.
20
Question 10 Queue que = new ListQueue(); // ListQueue implements Queue Object obj; que.enqueue("a"); que.enqueue("b"); que.enqueue("c"); obj = que.peekFront(); que.enqueue(obj + "d"); while (! que.isEmpty()) { System.out.print(que.dequeue() + " "); } What is printed as a result of executing this code segment? a)a b c bd b)a b c ad c)a b c cd d)ad c b a e)a b bd
21
Question 10 Answer Queue que = new ListQueue(); // ListQueue implements Queue Object obj; que.enqueue("a"); que.enqueue("b"); que.enqueue("c"); obj = que.peekFront(); que.enqueue(obj + "d"); while (! que.isEmpty()) { System.out.print(que.dequeue() + " "); } What is printed as a result of executing this code segment? Answer b: a b c ad The method enqueue adds items to the end of the queue. The method peekFront returns the first element in the queue. And dequeue removes and returns the first element in the queue.
22
Question 11 private static void sort(List theList) { Iterator itr = theList.iterator(); PriorityQueue pq = new PriorityQueueImpl(); // PriorityQueueImpl implements // PriorityQueue while (itr.hasNext()) { pq.add(itr.next()) itr.remove(); // removes last // item just seen by itr } while ( !pq.isEmpty()) theList.add(pq.removeMin()); } If the parameter is an ArrayList, and the PriorityQueueImpl is implemented as a min-heap, which data structure operation dominates the running time of the sort? a)pq.add b)pq.isEmpty c)pq.removeMin d)itr.remove e)theList.add
23
Question 11 Answer private static void sort(List theList) { Iterator itr = theList.iterator(); PriorityQueue pq = new PriorityQueueImpl(); // PriorityQueueImpl implements // PriorityQueue while (itr.hasNext()) { pq.add(itr.next()) itr.remove(); // removes last // item just seen by itr } while ( !pq.isEmpty()) theList.add(pq.removeMin()); } If the parameter is an ArrayList, and the PriorityQueueImpl is implemented as a min-heap, which data structure operation dominates the running time of the sort? Answer d: itr.remove When you remove from the front of the array list all the values past it must be moved down one. This is O(n).
24
Question 12 public class Student { public String getFood() { return “Pizza”; } public String getInfo() { return this.getFood(); } public class GradStudent extends Student { public String getFood() { return “Taco”; } What is the output from this: Student s1 = new GradStudent(); s1.getInfo(); a)Won’t compile since GradStudent doesn’t have a getInfo method b)Pizza c)Won’t compile since you are creating a GradStudent, not a Student d)Won’t compile since you use this.getFood() e)Taco
25
Question 12 Answer public class Student { public String getFood() { return “Pizza”; } public String getInfo() { return this.getFood(); } public class GradStudent extends Student { public String getFood() { return “Taco”; } What is the output from this: Student s1 = new GradStudent(); s1.getInfo(); Answer e: Taco Objects know what class they are created as and all methods are resolved starting with that class. If the method isn’t found in that Class the parent class is checked (and so on until it is found). This will execute the getInfo in Student and then execute the getFood in GradStudent and print Taco.
26
Question 13 The following is intended to return an integer array sum such that for all i, sum[i] is equal to arr[0] + arr[1] +... + arr[i]. For instance, if arr contains the values { 1, 4, 1, 3 }, the array sum will contain the values { 1, 5, 6, 9 }. private int[] arr; public int[] partialSum() { int[] sum = new int[arr.length]; for (int j = 0; j < sum.length; j++) sum[j] = 0; /* missing code */ return sum; } Here are two ways to replace /* missing code */ 1. for (int j = 0; j < arr.length; j++) sum[j] = sum[j - 1] + arr[j]; Or 2. for (int j = 0; j < arr.length; j++) for (int k = 0; k <= j; k++) sum[j] = sum[j] + arr[k]; Which of the following statements is true? a)Implementation 1 will cause an ArrayIndexOutOfBoundsException. b)Both work but 1 is faster than 2 c)Both work but 2 is faster than 1 d)Both work and are equally fast e)Implementation 2 will cause an ArrayIndexOutOfBoundsException
27
Question 13 Answer The following is intended to return an integer array sum such that for all i, sum[i] is equal to arr[0] + arr[1] +... + arr[i]. For instance, if arr contains the values { 1, 4, 1, 3 }, the array sum will contain the values { 1, 5, 6, 9 }. private int[] arr; public int[] partialSum() { int[] sum = new int[arr.length]; for (int j = 0; j < sum.length; j++) sum[j] = 0; /* missing code */ return sum; } Here are two ways to replace /* missing code */ 1. for (int j = 0; j < arr.length; j++) sum[j] = sum[j - 1] + arr[j]; Or 2. for (int j = 0; j < arr.length; j++) for (int k = 0; k <= j; k++) sum[j] = sum[j] + arr[k]; Answer a: Implementation 1 will cause an ArrayIndexOutOfBoundsExcep tion. When j is 0 sum[j-1] will be sum[-1] which will cause an ArrayIndexOutOfBoundsExcep tion.
28
Question 14 What are the values of a and b after the for loop finishes? int a = 10, b = 3, temp; for (int i=1; i<=6; i++) { temp = a; a = i + b; b = temp – i; } a)a = 13 and b = 0 b)a = 6 and b = 7 c)a = 9 and b = 3 d)a = 8 and b = 3 e)a = 0 and b = 13
29
Question 14 Answer What are the values of a and b after the for loop finishes? int a = 10, b = 3, t; for (int i=1; i<=6; i++) { t = a; a = i + b; b = t – i; } Answer a: a = 13 and b = 0 The variable i loops from 1 to 6 i = 1, t = 10, a = 4, b = 9 i = 2, t = 4, a = 11, b =2 i = 3, t = 11, a = 5, b = 8 i = 4, t = 5, a = 12, b = 1 i = 5, t = 12, a = 6, b = 7 i = 6, t = 6, a = 13, b = 0
30
Question 15 Susan is 5 years older than Matt. Three years from now Susan’s age will be twice Matt’s age. for (int s = 1; s <= 100; s++) { for (int m = 1; m <= 100; m++) { if ( ) System.out.println(“Susan is “ + s + “ and Matt is “ + m); } What should be in to solve this problem? a)(s == m – 5) && (s – 3 == 2 * (m – 3)) b)(s == (m + 5)) && ((s + 3) == (2 * m + 3)) c)s == (m – 5) && (2 * s – 3) == (m – 3) d)s == m + 5 && s + 3 == 2 * m + 6 e)None of the above is correct
31
Question 15 Answer Susan is 5 years older than Matt. Three years from now Susan’s age will be twice Matt’s age. for (int s = 1; s <= 100; s++) { for (int m = 1; m <= 100; m++) { if ( ) System.out.println(“Susan is “ + s + “ and Matt is “ + m); } What should be in to solve this problem? Answer d: s == m + 5 && s + 3 == 2 * m + 6 Susan is 5 years old than Matt so s == m + 5 should be true and in 3 years she will be twice as old so s + 3 = 2 * (m + 3) = 2 * m + 6.
32
Question 16 Stack s = new ListStack(); // ListStack implements Stack Queue q = new ListQueue(); // ListQueue implements Queue Assume that s is initially empty and that q initially contains the following strings: W X Y Z The front points to the W and the back points to the Z. Consider the following code segment: while (!q.isEmpty()) s.push(q.dequeue()); while (!s.isEmpty()) q.enqueue(s.pop()); After execution which is true: a)Stack s is empty and queue q contains W, X, Y, Z, in that order, with W at the front of the queue. b)Stack s contains Z, Y, X, W, in that order, with Z at the top of the stack, and queue q is empty. c)Stack s contains Z, Y, X, W, in that order, with Z at the top of the stack; and queue q contains W, X, Y, Z, in that order, with W at the front of the queue. d)Stack s is empty and queue q contains Z, Y, X, W, in that order, with Z at the front of the queue. e)Stack s contains Z, Y, X, W, in that order, with Z at the top of the stack; and queue q contains Z, Y, X, W, in that order, with Z at the front of the queue.
33
Question 16 Answer Stack s = new ListStack(); // ListStack implements Stack Queue q = new ListQueue(); // ListQueue implements Queue Assume that s is initially empty and that q initially contains the following strings: W X Y Z The front points to the W and the back points to the Z. Consider the following code segment: while (!q.isEmpty()) s.push(q.dequeue()); while (!s.isEmpty()) q.enqueue(s.pop()); Answer d: Stack s is empty and queue q contains Z, Y, X, W, in that order, with Z at the front of the queue. The method enqueue adds to the back of a queue and dequeue removes from the front of a queue. Push adds to the top of a stack and pop removes from the top of a stack.
34
Question 17 for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k = k * 2) { System.out.println(j + " " + k); } Of the following, which best characterizes the running time of the code segment? a)O(log n) b)O(n) c)O(n*n) (n squared) d)O(n log n) e)O(n!)
35
Question 17 Answer for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k = k * 2) { System.out.println(j + " " + k); } Of the following, which best characterizes the running time of the code segment? Answer d: O(n log n) The outer loop is n and the inner loop is log n due to k changing by a multiple of 2 each time
36
Question 18 What is the output from the following code? String s = “Georgia Tech”; String s1 = s.substring(0,7); String s2 = s1.substring(2); String s3 = s2.substring(0,3); System.out.println(s3); a)eor b)eorg c)org d)orgi e)You will get an index out of bounds exception
37
Question 18 Answer What is the output from the following code? String s = “Georgia Tech”; String s1 = s.substring(0,7); String s2 = s1.substring(2); String s3 = s2.substring(0,3); System.out.println(s3); Answer c: org Substring(a,b) means start at a and stop before b. substring(a) means start at a and go to the end of the string. The first character is at index 0. –Georgia –orgia –org
38
Question 19 public class Person implements Comparable { code for class } public class Player extends Person { code for class } Which declaration will result in a compile error? a)Person p = new Person(); b)Person p = new Player(); c)Comparable c = new Person(); d)Player p = new Person(); e)Comparable c = new Player();
39
Question 19 Answer public class Person implements Comparable { code for class } public class Player extends Person { code for class } Which declaration will result in a compile error? Answer d: Player p = new Person(); A variable can hold the declared type and any child of that type A class that implements an interface (or inherits from a class that implements the interface) can be declared to be of the interface type
40
Question 20 public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) == s.charAt(1) || check(s.substring(1))); } This will return true if and only if: a)s contains two or more of the same characters b)s contains two or more of the same characters in a row c)s starts with two or more of the same characters d)s ends with two or more of the same characters e)s.charAt(0) == s.charAt(1)
41
Question 20 Answer public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) == s.charAt(1) || check(s.substring(1))); } This will return true if and only if: Answer b: s contains two or more of the same characters in a row This will return true when s has at least 2 characters in it and the first two characters are the same or the recursive call to check with the substring starting with the 2 nd character returns true
42
Question 21 Which will cause the shortest execution of a binary search looking for a value in an array of integers sorted in ascending order? The array has an odd number of integers. 1.The value is the first in the array 2.The value is in the middle of the array 3.The value is the last in the array 4.The value is not in the array 5.The value is the third element in the array
43
Question 21 Answer Which will cause the shortest execution of a binary search looking for a value in an array of integers sorted in ascending order? The array has an odd number of integers. Answer b: The value is in the middle of the array A binary search starts at the middle of a sorted array so if that is the value you are searching for execution can stop then.
44
Question 22 Which will cause the longest execution of a sequential search looking for a value in an array of 10 integers? a)The value is the first one in the array b)The value is in the middle of the array c)The value is at position 3 in the array d)The value isn’t in the array e)The value is at position 6 in the array
45
Question 22 Answer Which will cause the longest execution of a sequential search looking for a value in an array of 10 integers? Answer d: The value isn’t in the array If the value isn’t in the array it will have to check every element in the array to be sure that it isn’t in the array
46
Question 23 If you have a parent class Animal that has a method speak() which returns “Awk” and you have children classes that do the following: –Cat has a speak method that returns “Meow” –Bird doesn’t have a speak method –Dog has a speak method that returns “Woof” –Pig doesn’t have a speak method –Cow has a speak method that returns “Moo” What is the output from looping through this array of animals and asking each to speak()? Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() } a)Awk Awk Awk Awk Awk b)This won’t compile c)Meow Moo Woof Awk Awk d)This will have runtime errors e)Meow Moo Woof Oink Awk
47
Question 23 Answer If you have a parent class Animal that has a method speak() which returns “Awk” and you have children classes that do the following: –Cat has a speak method that returns “Meow” –Bird doesn’t have a speak method –Dog has a speak method that returns “Woof” –Pig doesn’t have a speak method –Cow has a speak method that returns “Moo” Answer c: Meow Moo Woof Awk Awk Objects keep a reference to the class that created them. So, even if you put them in an array of Animals they know what they are and all methods are resolved starting with the class of the object. Bird and Pig do not override speak so the speak method in Animal will execute.
48
Question 24 public class Person { private String name; public Person(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } } public class Student extends Person { } Which are legal constructors? I.public Student() {} II.public Student(String name) { this.name = name; } III.public Student(String name) { super(name); } IV.public Student(String name) { this.setName(name); } a)III only b)III and IV c)I, III, and IV d)IV only e)I, II, III, and IV
49
Question 24 Answer public class Person { private String name; public Person(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } } public class Student extends Person { } Answer a: III only The field name is private to Person and can’t be directly set in Student so II can’t be correct. There is no no- argument constructor in Person so I and IV can’t be correct. In IV there is no explicit call to super so one will be added by the compiler to the no-arg constructor but since there is not a no-arg constructor in Person this will not be allowed. Only III has the correct call to the constructor in Person that takes a name.
50
Question 25 The following integers are inserted into an empty binary search tree in the following order: 26 20 37 31 22 18 25 29 19 Which traversal of the tree would produce the following output? 26 20 18 19 22 25 37 31 29 a)Level-by-level b)Preorder c)Inorder d)Postorder e)Reverse postorder
51
Question 25 Answer The following integers are inserted into an empty binary search tree in the following order: 26 20 37 31 22 18 25 29 19 Which traversal of the tree would produce the following output? 26 20 18 19 22 25 37 31 29 Answer b: Preorder A preorder traversal prints the node value then goes left and then right 26 20 37 18 22 31 19 25 29
52
Question 26 Question: Consider the following declarations. Integer valueOne, valueTwo; Assume that valueOne and valueTwo have been properly initialized. Which of the following is equivalent to the expression below? valueOne.intValue() == valueTwo.intValue() a)valueOne == valueTwo b)valueOne.compareTo( valueTwo) c)valueOne.equals(valueTwo) == 0 d)valueOne.intValue().equals( valueTwo.intValue()) e)valueOne.compareTo( valueTwo) == 0
53
Question 26 Answer Question: Consider the following declarations. Integer valueOne, valueTwo; Assume that valueOne and valueTwo have been properly initialized. Which of the following is equivalent to the expression below? valueOne.intValue() == valueTwo.intValue() Answer e: valueOne.compareTo( valueTwo) == 0 valueOne.intValue() == valueTwo.intValue() This will return true when the int values in valueOne and valueTwo are the same and false otherwise. The method compareTo returns 0 when the values are equal and not 0 when they are not so it will have the same result
54
Question 27 Consider the following data field and method. private int[] arr; // precondition: arr.length 0 public int checkArray() { int loc arr.length / 2; for (int k 0; k arr.length; k) { if (arr[k] arr[loc]) loc k; } return loc; } Which of the following is the best postcondition for checkArray? a)Returns the index of the first element in array arr whose value is greater than arr[loc] b)Returns the index of the last element in array arr whose value is greater than arr[loc] c)Returns the largest value in array arr d)Returns the index of the largest value in the second half of array arr e)Returns the index of the largest value in array arr
55
Question 27 Answer Consider the following data field and method. private int[] arr; // precondition: arr.length 0 public int checkArray() { int loc = arr.length / 2; for (int k = 0; k < arr.length; k++) { if (arr[k] > arr[loc]) loc = k; } return loc; } Answer e: Returns the index of the largest value in array arr This code sets the loc to the middle of the array and then loops through all the array elements. If the value at the current index is greater than the value at the stored loc then it changes loc to the index. It returns the loc which is the index to the largest value in the array.
56
Question 28 private int[] arr; public int mystery(int low, int high, int num) { int mid (low+high) / 2; if (low > high) { return -1; } else if (arr[mid] < num) { return mystery(mid +1, high, num); } else if (arr[mid] > num) { return mystery(low, mid - 1, num); } else return mid; } What is returned by the call: mystery(0, 4, 5) when arr = {1, 2, 3, 5, 7}? a)5 b)-1 c)3 d)0 e)4
57
Question 28 Answer private int[] arr; public int mystery(int low, int high, int num) { int mid (low+high) / 2; if (low > high) { return -1; } else if (arr[mid] < num) { return mystery(mid +1, high, num); } else if (arr[mid] > num) { return mystery(low, mid - 1, num); } else return mid; } What is returned by the call: mystery(0, 4, 5) ; when arr = {1, 2, 3, 5, 7}? Answer c: 3 This returns the index of the num if it is found in the array or else -1 if it isn’t found in the array. This is a binary search.
58
Question 29 printTypes(Object o) { if (o instanceof Object) System.out.print(“Object, “); if (o instanceof Base) System.out.print(“Base, ”); if (o instanceof Child1) System.out.print(“Child1, “); if (o instanceof Child2) System.out.print(“Child2“); } What is printed from printTypes(new Child2())? a)Child2 b)Base, Child2 c)Object, Base, Child1, Child2 d)Object, Base, Child1 e)Object, Base, Child2 Base Child1Child2
59
Question 29 Answer printTypes(Object o) { if (o instanceof Object) System.out.print(“Object, “); if (o instanceof Base) System.out.print(“Base, ”); if (o instanceof Child1) System.out.print(“Child1, “); if (o instanceof Child2) System.out.print(“Child2“); } Answer e: Object, Base, Child2 The method printTypes is classed with an object of the class Child2. From the UML class diagram we can see that Child2 is a child of Base so it will also be an instance of Base. All objects inherit from Object so it will also be an Object. Base Child1Child2
60
Question 30 In the Marine Biology Case Study what is the minimum number of steps it takes for a fish to change location to the location directly behind it? a)1 b)2 c)3 d)4 e)5
61
Question 30 Answer In the Marine Biology Case Study what is the minimum number of steps it takes for a fish to change location to the location directly behind it? Answer c: 3 A fish can not go backwards in the case study. So it would have to go left (or right) three times to get to the location behind the current location 21 30
62
Question 31 In the Marine Biology Case Study which of the following are interfaces? I.Fish II.Environment III.Location IV.Direction a)I only b)II only c)I and III d)II and III e)I, III and IV
63
Question 31 Answer In the Marine Biology Case Study which of the following are interfaces? I.Fish II.Environment III.Location IV.Direction Answer b: II only Only Environment is an interface. Location is a class that implements the Locatable interface.
64
Question 32 What is the best explanation for what is meant by overriding a method? a)Defining another method with the same name as another method but with a different number of parameters b)Defining another method with the same name as another method but with different types for the parameters c)Defining a method with the same name as an inherited method but with a different number of parameters d)Defining a method with the same name and parameter list as an inherited method e)Defining a method with the same precondition
65
Question 32 Answer What is the best explanation for what is meant by overriding a method? Answer d: Defining a method with the same name and parameter list as an inherited method
66
Question 33 I.public class Timer { public void start(); public void stop(); public int getTime(); } II.public interface Timer { private void start(); private void stop(); private int getTime(); } III.public class Timer {} IV.public interface Timer { public void start(); public void stop(); public int getTime(); } Which of the these correctly defines an interface? a)I only b)IV only c)II and IV d)III only e)I and IV
67
Question 33 Answer I.public class Timer { public void start(); public void stop(); public int getTime(); } II.public interface Timer { private void start(); private void stop(); private int getTime(); } III.public class Timer {} IV.public interface Timer { public void start(); public void stop(); public int getTime(); } Which of the these correctly defines an interface? Answer b: IV only An interface is declared using the keyword interface and all the methods must be public. They also have to be abstract methods but you don’t have to use the abstract keyword.
68
Question 34 Given the following declaration of a field in a class: public static final String GREETING = “Hi”; Which of these statements is FALSE? a)Each object of this class can access GREETING b)The value of greeting can’t be changed in any methods c)Each object of this class has it’s own copy of GREETING d)GREETING.length() = 2 e)GREETING.toUpperCas e() = “HI”
69
Question 34 Answer Given the following declaration of a field in a class: public static final String GREETING = “Hi”; Which of these statements is not true? Answer c: a)Each object of this class has it’s own copy of GREETING This is the only false statement. Static variables are not created for each object. They are created in the object that defines the class (class Class). All objects keep a reference to this Class object and can access fields in it.
70
Question 35 public abstract class Shape { // code for Shape } public class Oval extends Shape { // code for Oval } Which is valid? I.Shape s = new Oval(); II.Oval o = new Shape(); III.Shape s1 = new Shape(); IV.Oval o = new Oval(); a)I only b)I, III, and IV c)IV only d)I and IV e)III only
71
Question 35 Answer public abstract class Shape { // code for Shape } public class Oval extends Shape { // code for Oval } Which is valid? I.Shape s = new Oval(); II.Oval o = new Shape(); III.Shape s1 = new Shape(); IV.Oval o = new Oval(); Answer d: I and IV Shape is an abstract class and you can not create objects of an abstract class. You can declare an object to be of a type and it can refer to an object of that type or a subclass of that type.
72
Question 36 Method hasItem should return true if item is found in myList; otherwise, it should return false. private List myList; public boolean hasItem(Object item) { Iterator itr = myList.iterator(); while ( /* condition */ ) { if (itr.next().equals(item)) return true; } return false; } Which of the following expressions can be used to replace /* condition */ so that hasItem will work as intended? a)myList.hasNext() b)itr.hasNext() c)! itr.hasNext() d)! myList.hasNext() e)itr != null
73
Question 36 Answer Method hasItem should return true if item is found in myList; otherwise, it should return false. private List myList; public boolean hasItem(Object item) { Iterator itr = myList.iterator(); while ( /* condition */ ) { if (itr.next().equals(item)) return true; } return false; } Which of the following expressions can be used to replace /* condition */ so that hasItem will work as intended? Answer b: itr.hasNext() Iterators have a method hasNext that returns true while the iterator has more elements.
74
Question 37 See the following mail groups –Family: myMom@business.com, myDad@isp.net, mySis@school.edu myDad@isp.net mySis@school.edu –Friends: sue@yahoo.com, mary@aol.comsue@yahoo.com Which of the following describes the best choice of data structures for representing mail groups in an electronic mail application? a)Use a 2-d array of strings with the group names in the first column and the addresses in the second b)Use an array of linked lists with the group name the first element in the linked list c)Use an ArrayList of Maps, where each Map has one key, the group name, and a set of mail addresses d)Use a map with the group name as the key and a set of addresses as the value e)Use two arrays, one with the group name and the other the mail addresses as a String
75
Question 37 Answer See the following mail groups –Family: myMom@business.com, myDad@isp.net, mySis@school.edu myDad@isp.net mySis@school.edu –Friends: sue@yahoo.com, mary@aol.comsue@yahoo.com Which of the following describes the best choice of data structures for representing mail groups in an electronic mail application? Answer d: Use a map with the group name as the key and a set of addresses as the value You will need to lookup the group and a map is very quick for looking up the location given a unique key. You can store the mail addresses in a set because there shouldn’t be any duplicates.
76
Question 38 In the MBCS which is FALSE about SlowFish? a)It has a.2 chance of moving b)It can move to 3 empty neighboring locations c)It inherits the move method from Fish d)It overrides generateChild from Fish e)If it can’t move it reverses direction
77
Question 38 Answer In the MBCS which is FALSE about SlowFish? Answer e: If it can’t move it reverses direction Only objects of the DarterFish class do this, not objects of the SlowFish class
78
Question 39 In the MBCS which of the following is FALSE about DarterFish? a)When both spaces are full in front of it, it will reverse direction b)When both spaces are empty in front of it, it will move 2 spaces forward c)When one space is empty in front of it but the 2 nd is blocked it will turn left d)When one space is empty in front of it but the 2 nd is blocked it will move 1 space forward e)It overrides the move method that it inherited from Fish
79
Question 39 Answer In the MBCS which of the following is FALSE about DarterFish? Answer c: When one space is empty in front of it but the 2 nd is blocked it will turn left No, it will just move forward one space when the 2 nd space is blocked
80
Question 40 public List process1(int n) { ArrayList someList = new ArrayList(); for (int k = 0; k < n; k++) someList.add(new Integer(k)); return someList; } public List process2(int n) { ArrayList someList = new ArrayList(); for (int k = 0; k < n; k++) someList.add(k, new Integer(k)); return someList; a)Both methods produce the same result and take the same amount of time. b)Both methods produce the same result, and process1 is faster than process2. c)The two methods produce different results and take the same amount of time. d)The two methods produce different results, and process1 is faster than process2. e)The two methods produce different results, and process2 is faster than process1.
81
Question 40 Answer public List process1(int n) { ArrayList someList = new ArrayList(); for (int k = 0; k < n; k++) someList.add(new Integer(k)); return someList; } public List process2(int n) { ArrayList someList = new ArrayList(); for (int k = 0; k < n; k++) someList.add(k, new Integer(k)); return someList; } Answer a: Both methods produce the same result and take the same amount of time. The method process1 adds to the end of the ArrayList each time through the loop. The method process2 also adds to the end of the ArrayList each time through the loop. The only difference would be if there were values in the ArrayList in process2. Any existing values would be moved to the right. But, there are no existing values in the ArrayList at that index or past it.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.