1 / 81

AP Bowl Practice Exam AB

AP Bowl Practice Exam AB. April 16, 2005 Georgia Tech.

veta
Télécharger la présentation

AP Bowl Practice Exam AB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AP Bowl Practice Exam AB April 16, 2005 Georgia Tech

  2. 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? Create one class PublishedMaterial with the requested fields plus type Create classes Book, Movie, and AudioTape and each class has the requested fields Create the class PublishedMaterial and have Book, Movie, and AudioTape inherit from it all the listed fields Create one class BookStore with the requested fields plus type Create classes for PublishedMaterial, Books, Movies, AudioTape, Title, Price, ID, Authors, DatePublished Question 1

  3. 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. Question 1 Answer

  4. 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? [0, 4, 2, 5, 3] [3, 5, 2, 4, 0, 0, 0, 0] [0, 0, 0, 0, 4, 2, 5, 3] [4, 2, 5, 3] [0, 0, 4, 2, 5, 0, 3, 0] Question 2

  5. 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. Question 2 Answer

  6. 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? 50 20 60 10 30 Question 3

  7. 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 Question 3 Answer

  8. 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); All values in positions m+1 through myStuff.length-1 are greater than or equal to n. All values in position 0 through m are less than n. All values in position m+1 through myStuff.length-1 are less than or equal to n. The smallest value is at position m. The largest value that is smaller than n is at position m. Question 4

  9. 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). Question 4 Answer

  10. //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)? 43211234 1234 4321 12344321 32144123 Question 5

  11. //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 5 Answer

  12. Which of the following best describes the data structure represented by java.util.LinkedList? A doubly linked list with references to the first and last nodes only. A singly linked list with a reference to the first node only. A singly linked list with references to the first and last nodes. A doubly linked list with a reference to the first node only. A doubly linked list with reference to the first, middle, and last nodes. Question 6

  13. Which of the following best describes the data structure represented by java.util.LinkedList? Answer a: A doubly linked list with references to the first and last nodes only. Question 6 Answer

  14. 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)? 128 256 64 0 2 Question 7

  15. 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 7 Answer

  16. 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? {{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

  17. 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 8 Answer

  18. 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; } I only II only III only I and II only I, II, and III Question 9

  19. 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. Question 9 Answer

  20. 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 b c bd a b c ad a b c cd ad c b a a b bd Question 10

  21. 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. Question 10 Answer

  22. 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? pq.add pq.isEmpty pq.removeMin itr.remove theList.add Question 11

  23. 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). Question 11 Answer

  24. 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(); Won’t compile since GradStudent doesn’t have a getInfo method Pizza Won’t compile since you are creating a GradStudent, not a Student Won’t compile since you use this.getFood() Taco Question 12

  25. 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. Question 12 Answer

  26. 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? Implementation 1 will cause an ArrayIndexOutOfBoundsException. 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 ArrayIndexOutOfBoundsException Question 13

  27. 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 ArrayIndexOutOfBoundsException. When j is 0 sum[j-1] will be sum[-1] which will cause an ArrayIndexOutOfBoundsException. Question 13 Answer

  28. 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 = 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

  29. 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 14 Answer

  30. 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? (s == m – 5) && (s – 3 == 2 * (m – 3)) (s == (m + 5)) && ((s + 3) == (2 * m + 3)) s == (m – 5) && (2 * s – 3) == (m – 3) s == m + 5 && s + 3 == 2 * m + 6 None of the above is correct Question 15

  31. 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 15 Answer

  32. 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: 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

  33. 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. Question 16 Answer

  34. 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? O(log n) O(n) O(n*n) (n squared) O(n log n) O(n!) Question 17

  35. 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 17 Answer

  36. 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); eor eorg org orgi You will get an index out of bounds exception Question 18

  37. 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 stopbeforeb. substring(a) means start at a and go to the end of the string. The first character is at index 0. Georgia orgia org Question 18 Answer

  38. public class Person implements Comparable { code for class } public class Player extends Person { code for class } Which declaration will result in a compile error? Person p = new Person(); Person p = new Player(); Comparable c = new Person(); Player p = new Person(); Comparable c = new Player(); Question 19

  39. 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 19 Answer

  40. 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: s contains two or more of the same characters s contains two or more of the same characters in a row s starts with two or more of the same characters s ends with two or more of the same characters s.charAt(0) == s.charAt(1) Question 20

  41. 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 2nd character returns true Question 20 Answer

  42. 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. The value is the first in the array The value is in the middle of the array The value is the last in the array The value is not in the array The value is the third element in the array Question 21

  43. 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 21 Answer

  44. Which will cause the longest execution of a sequential search looking for a value in an array of 10 integers? The value is the first one in the array The value is in the middle of the array The value is at position 3 in the array The value isn’t in the array The value is at position 6 in the array Question 22

  45. 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 22 Answer

  46. 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() } Awk Awk Awk Awk Awk This won’t compile Meow Moo Woof Awk Awk This will have runtime errors Meow Moo Woof Oink Awk Question 23

  47. 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. Question 23 Answer

  48. 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 { <constructors> } Which are legal constructors? public Student() {} public Student(String name) { this.name = name;} public Student(String name) { super(name); } public Student(String name) { this.setName(name);} III only III and IV I, III, and IV IV only I, II, III, and IV Question 24

  49. 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 { <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 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. Question 24 Answer

  50. 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 Level-by-level Preorder Inorder Postorder Reverse postorder Question 25

More Related