ap bowl practice exam a april 16, 2005 georgia tech
TRANSCRIPT
AP Bowl Practice Exam A
April 16, 2005
Georgia Tech
Question 1boolean temp = false; for ( int i = 0; i < a.length; i++) { temp = ( a[i] == val ); if (temp) return temp;} return temp;
• Which of the answers best describes the conditions needed for temp to be true when it is returned?
a) Whenever the first element in a is equal to val
b) Whenever more than 1 element in a is equal to val.
c) Whenever a contains any element which equals val.
d) Whenever exactly 1 element in a is equal to val.
e) Whenever the last element in a is equal to val.
Question 2 ArrayList list = new ArrayList();list.add(new Integer(1)); list.add(new Integer(2)); list.add(1, new Integer(5)); list.set(1, new Integer(4));list.add(new Integer(6)); list.add(new Integer(3));System.out.println(list);
• What is printed as a result of executing the code segment?
a) [1, 2, 5, 4, 6, 3]b) [6, 5, 4, 3, 2, 1]c) [1, 2, 3, 4, 5, 6]d) [1, 4, 2, 6, 3]e) [1, 2, 4, 6, 3]
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) 50b) 20c) 60d) 10e) 30
Question 4private int [] myStuff;
//precondition: myStuff contains// integers in no particular orderpublic 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.
Question 5//precondition: x >=0public 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) 43211234b) 1234c) 4321d) 12344321e) 32144123
Question 6 • After the execution of
this program, what is the output?
for (int j = 1; j <= 5; j++) { for (int k = 1; k < 5; k++) System.out.print(j * k + " "); System.out.println(); }
a) 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20
b) 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16
c) 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
d) 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 5 10 15 20
e) 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16
Question 7public 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
Question 8Integer int1 = new Integer(3);
Integer int2 = new Integer(3);
Integer int3 = int2;
• Which of the following would return true? I. (int3.equals(int2))
II. (int1.equals(int2))
III. (int3 == int2)
IV. (int1 == int2)
V. (int2 == int3)
a) I and II only
b) I, II, III, and V
c) All will return true
d) I, II, and III only
e) III, IV, and V only
Question 9public 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 onlyb) II onlyc) III onlyd) I and II onlye) I, II, and III
Question 10• Question: Assume
that x is an initialized int variable. The code segment:
if (x >= 1) x = x * 3;
if (x >= 3) x = 0; • is equivalent to which
of the following code segments?
a) if (x >= 1) x = 0;
b) if (x >= 1) x *= 3;
c) if (x > 3) x = 0;
d) x = 0;
e) x = x * 3;
Question 11• Question: Assume that
variable a is an array of k integers and that the following is true:
• a[0] <= a[k] for all k such that 1 <= k Which of the following statements is a valid conclusion?
a) The value at a[0] does not occur anywhere else in the array
b) Array a is sorted in ascending order
c) The value at a[0] is the smallest value in the array
d) The value at a[0] is the largest value in the array
e) Array a is sorted in descending order
Question 12public 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) Pizzac) Won’t compile since you are
creating a GradStudent, not a Student
d) Won’t compile since you use this.getFood()
e) Taco
Question 13public int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}• If a = {5, 3, 1}, what is
the value in a[1] after m1(a); is run?
1. 4
2. 2
3. 10
4. 6
5. 8
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 = 0b) a = 6 and b = 7c) a = 9 and b = 3d) a = 8 and b = 3e) a = 0 and b = 13
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 16• Consider the following
code segment
for(int i = 0; i < 4; i++) {
for(int j=0; j < 6; j++)
System.out.println("*");
} • How many stars are
output when this code is executed?
a) 4
b) 10
c) 24
d) 6
e) 36
Question 17• Consider the following:
String s1 = “Hi There”;String s2 = s1;String s3 = s2;String s4 = s1;s2 = s2.toLowerCase();s3 = s3.toUpperCase();s4 = null;
• What is in s1?
a) hi thereb) HI THEREc) nulld) hI tHEREe) Hi There
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
Question 19public 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 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)
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 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 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 Awkb) This won’t compilec) Meow Moo Woof Awk
Awkd) This will have runtime
errorse) Meow Moo Woof Oink
Awk
Question 24public 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?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 onlyb) III and IVc) I, III, and IVd) IV onlye) I, II, III, and IV
Question 25• What is the value of x
after the following?
int x = 2;
Int y = -1;
while (x < 30) {
x = (int) Math.pow(x,
Math.abs(y * 2));
}
• Answer b: 256• This will raise 2 to the
power of 2 each time through the loop and stop when x >= 30.
x = 2
x = 4
x = 16
x = 256
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
Question 27• Consider the following data
field and method.private int[] arr;// precondition: arr.length 0public 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
Question 28private 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) 5b) -1c) 3d) 0e) 4
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) Child2b) Base, Child2c) Object, Base, Child1, Child2d) Object, Base, Child1e) Object, Base, Child2
Base
Child1 Child2
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
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
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
Question 33I. 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 onlyb) IV onlyc) II and IVd) III onlye) I and IV
Question 34• Given the following
declaration of a field in a class:
public static final String GREETING = “Hi”;
• Which of these statements is not true?
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.toUpperCase() = “HI”
Question 35public 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 onlyb) I, III, and IVc) IV onlyd) I and IVe) III only
Question 36• Which is equivalent to
the following?
!(b || d)
a) !b && !d
b) b && d
c) !b || !d
d) (b && d) || (b || d)
e) !(b && d)
Question 37• In the MBCS which is
not in the simulation class?
a) A private field theEnv
b) A private field theDisplay
c) A public method step
d) A private field that is an array of Fish
e) A constructor that initializes theEnv and theDisplay
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
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 2nd is blocked it will turn left
d) When one space is empty in front of it but the 2nd is blocked it will move 1 space forward
e) It overrides the move method that it inherited from Fish
Question 40• Which of the
following is FALSE about the field nextAvailableID in the Fish class?
a) It is initialized to 1 when it is declared.
b) All fish objects have their own copy of this field.
c) A fish’s id is assigned to this value in the initialize method
d) After the fish’s id has been set to this value the initialize method increments nextAvailableID
e) It is inherited by subclasses