AP Bowl Practice Exam AB April 16 2005
AP Bowl Practice Exam AB April 16, 2005 Georgia Tech
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 Published. Material with the requested fields plus type b) Create classes Book, Movie, and Audio. Tape and each class has the requested fields c) Create the class Published. Material and have Book, Movie, and Audio. Tape inherit from it all the listed fields d) Create one class Book. Store with the requested fields plus type e) Create classes for Published. Material, Books, Movies, Audio. Tape, Title, Price, ID, Authors, Date. Published
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 Published. Material and have Book, Movie, and Audio. Tape 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 Audio. Tape. They have common fields so we should put these in a common superclass Published. Material.
Question 2 private Array. List nums; // precondition: nums. size() > 0; // nums contains Integer objects public void num. Quest() { int k = 0; Integer zero = new Integer(0); while (k < nums. size()) { if (nums. get(k). equals(zero)) nums. remove(k); else k++; } } • Array. List nums = [0, 0, 4, 2, 5, 0, 3, 0] • What will Array. List nums contain as a result of executing num. Quest? a) b) c) d) e) [0, 4, 2, 5, 3] [3, 5, 2, 4, 0, 0] [0, 0, 4, 2, 5, 3] [0, 0, 4, 2, 5, 0, 3, 0]
Question 2 Answer private Array. List nums; // precondition: nums. size() > 0; // nums contains Integer objects public void num. Quest() { int k = 0; Integer zero = new Integer(0); while (k < nums. size()) { if (nums. get(k). equals(zero)) nums. remove(k); else k++; } } • Array. List nums = [0, 0, 4, 2, 5, 0, 3, 0] What will Array. List nums contain as a result of executing num. Quest ? • 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.
Question 3 • Consider the following method: public int m 1 (int a) { if (a == 1) return 10; else return 10 + m 1 (a – 1); } • What is the output when m 1(5) is called? a) b) c) d) e) 50 20 60 10 30
Question 3 Answer • Consider the following method: public int m 1 (int a) { if (a == 1) return 10; else return 10 + m 1 (a – 1); } • What is the output when m 1(5) is called? • Answer a: 50 = 10 * 5 m 1(5) return 10 + m 1(4) return 10 + m 1(3) return 10 + m 1(2) return 10+ m 1(1) return 10 return 20 return 30 return 40 return 50
Question 4 private int [] my. Stuff; //precondition: my. Stuff contains // integers in no particular order public int mystery(int num) { for (int k = my. Stuff. length - 1; k >= 0; k--) { if (my. Stuff[k] > num) return k; } return -1; } • Which of the following best describes the contents of my. Stuff after the following statement has been executed? int m = mystery(n); a) All values in positions m+1 through my. Stuff. 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 my. Stuff. 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.
Question 4 Answer private int [] my. Stuff; //precondition: my. Stuff contains // integers in no particular order public int mystery(int num) { for (int k = my. Stuff. length - 1; k >= 0; k--) { if (my. Stuff[k] > num) return k; } return -1; } • Which of the following best describes the contents of my. Stuff after the following statement has been executed? int m = mystery(n); • Answer c: All values in position m+1 through my. Stuff. 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).
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) b) c) d) e) 43211234 4321 12344321 32144123
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)
Question 6 • Which of the following a) A doubly linked list with references to the first and best describes the last nodes only. data structure b) A singly linked list with a represented by reference to the first node java. util. Linked. List? 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.
Question 6 Answer • Which of the following best describes the data structure represented by java. util. Linked. Li st? • • Answer a: A doubly linked list with references to the first and last nodes only.
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) b) c) d) e) 128 256 64 0 2
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
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) b) c) d) e) {{2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}} {{2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}} {{2 1 1 1}, {3 2 1 1}, {3 3 2 1}} {{2 3 3 3}, {1 2 3 3}, {1 1 2 3}} {{1 1 1 1}, {2 2 2 2}, {3 3 3 3}}
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.
Question 9 public class Point 2 D { public int x; public int y; public Point 2 D() {} public Point 2 D(int x, int y) { this. x = x; this. y = y; } // other methods } public class Point 3 D extends Point 2 D { public int z; // other code } • Which of the following constructors would be valid for Point 3 D? I. public Point 3 D() {} II. public Point 3 D(int x, int y, int z) { super(x, y); this. z = z; } III. public Point 3 D(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
Question 9 Answer public class Point 2 D { public int x; public int y; public Point 2 D() {} public Point 2 D(int x, int y) { this. x = x; this. y = y; } // other methods } public class Point 3 D extends Point 2 D { public int z; // other code } • Which of the following constructors would be valid for Point 3 D? I. public Point 3 D() {} II. public Point 3 D(int x, int y, int z) { super(x, y); this. z = z; } III. public Point 3 D(int x, int y) { this. x = x; this. y = y; this. z = 0; } • • Answer e: I, II, and III Point 2 D does have a no-arg constructor so I is okay. Point 2 D does have a constructor that takes x and y so II is okay. And x and y are public so III is okay.
Question 10 Queue que = new List. Queue(); // List. Queue implements Queue Object obj; que. enqueue("a"); que. enqueue("b"); que. enqueue("c"); obj = que. peek. Front(); que. enqueue(obj + "d"); while (! que. is. Empty()) { System. out. print(que. dequeue() + " "); } • What is printed as a result of executing this code segment? a) b) c) d) e) a b c bd a b c ad a b c cd ad c b a a b bd
Question 10 Answer Queue que = new List. Queue(); // List. Queue implements Queue Object obj; que. enqueue("a"); que. enqueue("b"); que. enqueue("c"); obj = que. peek. Front(); que. enqueue(obj + "d"); while (! que. is. Empty()) { 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 peek. Front returns the first element in the queue. And dequeue removes and returns the first element in the queue.
Question 11 private static void sort(List the. List) { Iterator itr = the. List. iterator(); Priority. Queue pq = new Priority. Queue. Impl(); // Priority. Queue. Impl implements // Priority. Queue while (itr. has. Next()) { pq. add(itr. next()) itr. remove(); // removes last // item just seen by itr } while ( !pq. is. Empty()) the. List. add(pq. remove. Min()); } • If the parameter is an Array. List, and the Priority. Queue. Impl is implemented as a min-heap, which data structure operation dominates the running time of the sort? a) b) c) d) e) pq. add pq. is. Empty pq. remove. Min itr. remove the. List. add
Question 11 Answer private static void sort(List the. List) { Iterator itr = the. List. iterator(); Priority. Queue pq = new Priority. Queue. Impl(); // Priority. Queue. Impl implements // Priority. Queue while (itr. has. Next()) { pq. add(itr. next()) itr. remove(); // removes last // item just seen by itr } while ( !pq. is. Empty()) the. List. add(pq. remove. Min()); } • • If the parameter is an Array. List, and the Priority. Queue. Impl 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).
Question 12 public class Student { public String get. Food() { return “Pizza”; } public String get. Info() { return this. get. Food(); } } public class Grad. Student extends Student { public String get. Food() { return “Taco”; } } • What is the output from this: Student s 1 = new Grad. Student(); s 1. get. Info(); a) Won’t compile since Grad. Student doesn’t have a get. Info method b) Pizza c) Won’t compile since you are creating a Grad. Student, not a Student d) Won’t compile since you use this. get. Food() e) Taco
Question 12 Answer public class Student { public String get. Food() { return “Pizza”; } public String get. Info() { return this. get. Food(); } } public class Grad. Student extends Student { public String get. Food() { return “Taco”; } } • What is the output from this: Student s 1 = new Grad. Student(); s 1. get. Info(); • 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 get. Info in Student and then execute the get. Food in Grad. Student and print Taco.
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[] partial. Sum() { 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]; • a) b) c) d) e) Which of the following statements is true? Implementation 1 will cause an Array. Index. Out. Of. Bounds. Exception. Both work but 1 is faster than 2 Both work but 2 is faster than 1 Both work and are equally fast Implementation 2 will cause an Array. Index. Out. Of. Bounds. Exception
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[] partial. Sum() { 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 Array. Index. Out. Of. Bounds. Excep tion. • When j is 0 sum[j-1] will be sum[-1] which will cause an Array. Index. Out. Of. Bounds. Excep tion.
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) b) c) d) e) a = 13 and b = 0 a = 6 and b = 7 a = 9 and b = 3 a = 8 and b = 3 a = 0 and b = 13
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
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 (<condition>) System. out. println(“Susan is “ + s + “ and Matt is “ + m); } } • What should be in <condition> 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
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 (<condition>) System. out. println(“Susan is “ + s + “ and Matt is “ + m); } } • What should be in <condition> 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.
Question 16 Stack s = new List. Stack(); // List. Stack implements Stack Queue q = new List. Queue(); // List. Queue implements Queue • a) b) • 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: c) d) while (!q. is. Empty()) s. push(q. dequeue()); while (!s. is. Empty()) q. enqueue(s. pop()); e) After execution which is true: Stack s is empty and queue q contains W, X, Y, Z, in that order, with W at the front of the queue. Stack s contains Z, Y, X, W, in that order, with Z at the top of the stack, and queue q is empty. 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. Stack s is empty and queue q contains Z, Y, X, W, in that order, with Z at the front of the queue. 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.
Question 16 Answer Stack s = new List. Stack(); // List. Stack implements Stack Queue q = new List. Queue(); // List. Queue 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. is. Empty()) s. push(q. dequeue()); while (!s. is. Empty()) 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.
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) b) c) d) e) O(log n) O(n*n) (n squared) O(n log n) O(n!)
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
Question 18 • What is the output from the following code? String s = “Georgia Tech”; String s 1 = s. substring(0, 7); String s 2 = s 1. substring(2); String s 3 = s 2. substring(0, 3); System. out. println(s 3); a) b) c) d) e) eorg orgi You will get an index out of bounds exception
Question 18 Answer • What is the output from the following code? String s = “Georgia Tech”; String s 1 = s. substring(0, 7); String s 2 = s 1. substring(2); String s 3 = s 2. substring(0, 3); System. out. println(s 3); • 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 – org
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();
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
Question 20 public boolean check(String s) { return s. length() >= 2 && (s. char. At(0) == s. char. At(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. char. At(0) == s. char. At(1)
Question 20 Answer public boolean check(String s) { return s. length() >= 2 && (s. char. At(0) == s. char. At(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
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
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.
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
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
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 b) This won’t compile c) Meow Moo Woof Awk d) This will have runtime errors e) Meow Moo Woof Oink Awk
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 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.
Question 24 public class Person { private String name; public Person(String name) { this. name = name; } public void set. Name(String name) { this. name = name; } public String get. Name() { return name; } } public class Student extends Person { <constructors> } • I. II. Which are legal constructors? public Student() {} public Student(String name) { this. name = name; } III. public Student(String name) { super(name); } IV. public Student(String name) { this. set. Name(name); } a) b) c) d) e) III only III and IV I, III, and IV IV only I, III, and IV
Question 24 Answer public class Person { private String name; public Person(String name) { this. name = name; } public void set. Name(String name) { this. name = name; } public String get. Name() { return name; } } public class Student extends Person { <constructors> } • 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 noargument 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.
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) b) c) d) e) Level-by-level Preorder Inorder Postorder Reverse postorder
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
Question 26 • Question: Consider the following declarations. Integer value. One, value. Two; • Assume that value. One and value. Two have been properly initialized. • Which of the following is equivalent to the expression below? value. One. int. Value() == value. Two. int. Value() a) value. One == value. Two b) value. One. compare. To( value. Two) c) value. One. equals(value. Two) == 0 d) value. One. int. Value(). equals( value. Two. int. Value()) e) value. One. compare. To( value. Two) == 0
Question 26 Answer • Question: Consider the following declarations. Integer value. One, value. Two; • Assume that value. One and value. Two have been properly initialized. • Which of the following is equivalent to the expression below? value. One. int. Value() == value. Two. int. Value() • Answer e: • value. One. compare. To( value. Two) == 0 • value. One. int. Value() == value. Two. int. Value() • This will return true when the int values in value. One and value. Two are the same and false otherwise. • The method compare. To returns 0 when the values are equal and not 0 when they are not so it will have the same result
Question 27 • Consider the following data field and method. private int[] arr; // precondition: arr. length 0 public int check. Array() { int loc arr. length / 2; for (int k 0; k arr. length; k) { if (arr[k] arr[loc]) loc k; } return loc; } • a) b) c) d) e) Which of the following is the best postcondition for check. Array? Returns the index of the first element in array arr whose value is greater than arr[loc] Returns the index of the last element in array arr whose value is greater than arr[loc] Returns the largest value in array arr Returns the index of the largest value in the second half of array arr Returns the index of the largest value in array arr
Question 27 Answer • Consider the following data field and method. private int[] arr; // precondition: arr. length 0 public int check. Array() { 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.
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
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.
Question 29 Base Child 1 Child 2 print. Types(Object o) { if (o instanceof Object) System. out. print(“Object, “); if (o instanceof Base) System. out. print(“Base, ”); if (o instanceof Child 1) System. out. print(“Child 1, “); if (o instanceof Child 2) System. out. print(“Child 2“); } • What is printed from print. Types(new Child 2())? a) Child 2 b) Base, Child 2 c) Object, Base, Child 1, Child 2 d) Object, Base, Child 1 e) Object, Base, Child 2
Question 29 Answer Base Child 1 Child 2 print. Types(Object o) { if (o instanceof Object) System. out. print(“Object, “); if (o instanceof Base) System. out. print(“Base, ”); if (o instanceof Child 1) System. out. print(“Child 1, “); if (o instanceof Child 2) System. out. print(“Child 2“); } • Answer e: Object, Base, Child 2 • The method print. Types is classed with an object of the class Child 2. From the UML class diagram we can see that Child 2 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.
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) b) c) d) e) 1 2 3 4 5
Question 30 Answer • In the Marine Biology • Answer c: 3 • A fish can not go Case Study what is backwards in the case the minimum number study. So it would have of steps it takes for a to go left (or right) three fish to change location times to get to the location directly location behind the behind it? current location 2 3 1 0
Question 31 • In the Marine Biology Case Study which of the following are interfaces? I. Fish II. Environment III. Location IV. Direction a) b) c) d) e) I only I and III II and III I, III and IV
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.
Question 32 • What is the best explanation for what is meant by overriding a method? a) b) c) d) e) Defining another method with the same name as another method but with a different number of parameters Defining another method with the same name as another method but with different types for the parameters Defining a method with the same name as an inherited method but with a different number of parameters Defining a method with the same name and parameter list as an inherited method Defining a method with the same precondition
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
Question 33 I. III. IV. public class Timer { public void start(); public void stop(); public int get. Time(); } public interface Timer { private void start(); private void stop(); private int get. Time(); } public class Timer {} public interface Timer { public void start(); public void stop(); public int get. Time(); } • a) b) c) d) e) Which of these correctly defines an interface? I only IV only II and IV III only I and IV
Question 33 Answer I. III. IV. public class Timer { public void start(); public void stop(); public int get. Time(); } public interface Timer { private void start(); private void stop(); private int get. Time(); } public class Timer {} public interface Timer { public void start(); public void stop(); public int get. Time(); } • Which of 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.
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. to. Upper. Cas e() = “HI”
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.
Question 35 public abstract class Shape { // code for Shape } public class Oval extends Shape { // code for Oval } Which is valid? I. III. IV. Shape s = new Oval(); Oval o = new Shape(); Shape s 1 = new Shape(); Oval o = new Oval(); a) b) c) d) e) I only I, III, and IV IV only I and IV III only
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 s 1 = 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.
Question 36 • Method has. Item should return true if item is found in my. List; otherwise, it should return false. private List my. List; public boolean has. Item(Object item) { Iterator itr = my. List. 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 has. Item will work as intended? a) b) c) d) e) my. List. has. Next() itr. has. Next() ! my. List. has. Next() itr != null
Question 36 Answer • Method has. Item should return true if item is found in my. List; otherwise, it should return false. private List my. List; public boolean has. Item(Object item) { Iterator itr = my. List. 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 has. Item will work as intended? • Answer b: • itr. has. Next() • Iterators have a method has. Next that returns true while the iterator has more elements.
Question 37 • See the following mail groups – Family: my. Mom@business. com, my. Dad@isp. net, my. Sis@school. edu – Friends: sue@yahoo. com, mary@aol. com • Which of the following describes the best choice of data structures for representing mail groups in an electronic mail application? a) b) c) d) e) Use a 2 -d array of strings with the group names in the first column and the addresses in the second Use an array of linked lists with the group name the first element in the linked list Use an Array. List of Maps, where each Map has one key, the group name, and a set of mail addresses Use a map with the group name as the key and a set of addresses as the value Use two arrays, one with the group name and the other the mail addresses as a String
Question 37 Answer • See the following mail groups – Family: my. Mom@business. com, my. Dad@isp. net, my. Sis@school. edu – Friends: sue@yahoo. com, mary@aol. 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.
Question 38 • In the MBCS which is FALSE about Slow. Fish? 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 generate. Child from Fish e) If it can’t move it reverses direction
Question 38 Answer • In the MBCS which is FALSE about Slow. Fish? • Answer e: • If it can’t move it reverses direction • Only objects of the Darter. Fish class do this, not objects of the Slow. Fish class
Question 39 • In the MBCS which of the following is FALSE about Darter. Fish? 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
Question 39 Answer • In the MBCS which of the following is FALSE about Darter. Fish? • • • 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
Question 40 public List process 1(int n) { Array. List some. List = new Array. List(); for (int k = 0; k < n; k++) some. List. add(new Integer(k)); return some. List; } public List process 2(int n) { Array. List some. List = new Array. List(); for (int k = 0; k < n; k++) some. List. add(k, new Integer(k)); return some. List; a) Both methods produce the same result and take the same amount of time. b) Both methods produce the same result, and process 1 is faster than process 2. c) The two methods produce different results and take the same amount of time. d) The two methods produce different results, and process 1 is faster than process 2. e) The two methods produce different results, and process 2 is faster than process 1.
Question 40 Answer public List process 1(int n) { Array. List some. List = new Array. List(); for (int k = 0; k < n; k++) some. List. add(new Integer(k)); return some. List; } public List process 2(int n) { Array. List some. List = new Array. List(); for (int k = 0; k < n; k++) some. List. add(k, new Integer(k)); return some. List; } • • • Answer a: Both methods produce the same result and take the same amount of time. The method process 1 adds to the end of the Array. List each time through the loop. The method process 2 also adds to the end of the Array. List each time through the loop. The only difference would be if there were values in the Array. List in process 2. Any existing values would be moved to the right. But, there are no existing values in the Array. List at that index or past it.
- Slides: 81