AP Computer Science A Multiple Choice Questions Packet
AP Computer Science A Multiple Choice Questions Packet
Directions: Determine the answer to each of the following questions or in complete statements, using the
available space for any necessary scratch work. Then decide which the best of the choices given is. Do not
spend too much time on any one problem.
Notes:
• Assume that the classes in the Quick Reference have been imported where needed.
• Assume that variables and methods are declared within the context of an enclosing class.
• Assume that method calls that have no object or class name prefixed, and that are not shown within a
complete class definition, appear within the context of an enclosing class.
• Assume that parameters in method calls are not null unless otherwise stated.
int num = 5;
num++;
num /= 3;
double p = 2.5;
p *= num;
System.out.println(p);
(A) 0
(B) 1
(C) 6
(D) 12
(E) 24
1
3) What will be printed by this code?
int x = 0;
int y = 0;
while(x < 2) {
y = 2;
while(x*y <= 2) {
System.out.print(x + " " + y + " ");
y++;
}
System.out.println();
x++;
}
(A) 1 2
(B) 0 2
(C) 0 2 1 3
(D) Nothing; the loop never begins.
(E) Nothing; this is an infinite loop.
(A) I only (B) II only (C) III only (D) I and III only (E) II and III only
Suppose that methods with the following headers are now added to SomeClass:
(A) None
(B) I only
(C) II only
(D) III only
(E) I and III only
2
6) Which of the following pieces of code will correctly calculate the area of a circle (𝐴 = 𝜋𝑟 2 )
I. int radius = 5;
final double area = Math.PI * Math.pow(radius, 2);
(A) I only
(B) II and III only
(C) I and III only
(D) II only
(E) I, II, and III
7) What are the values of a and b after the following code executes?
(A) a = 10 and b = 50
(B) a = 30 and b = 20
(C) a = 20 and b = 10
(D) a = 30 and b = 50
(E) a = 10 and b = 20
(A) stringList.add(space);
(B) stringList.add("Computer");
(C) stringList.add(“myInt”);
(D) stringList.add(space + 8);
(E) stringList.add(myInt + 8);
3
9) Consider the following statement:
Which of the following replacements for /*expression*/ creates in num a random integer from 2 to 50,
including 2 and 50?
10) Which of the following pairs of statements will cause an error message?
I. double x = 14.7;
int y = x;
(A) None
(B) I only
(C) II only
(D) III only
(E) I and III only
(A) 2
(B) 3
(C) 66
(D) 66.66666666
(E) 203
4
12) What output is produced by the following line of code?
int myNumber = 4;
System.out.println("myNumber/3 = 1");
System.out.println(myNumber/3);
(D) (E)
myNumber/3 = 1 myNumber/3 = 1
1.333333333333 1
(A) System.out.println("2/3");
(B) System.out.println("2" + "/" + "3");
(C) System.out.println(2/3);
(D) System.out.println(2 + "/" + 3);
(E) System.out.println((int)2 + "/" + (int)3);
int num = 0;
int score = 10;
if (num != 0 && score / num > SOME_CONSTANT)
statement1;
else
statement2;
5
15) What is the output from the following code?
int x = 5, y = 2;
System.out.println(x/y - (double)(x/y));
(A) 0.0
(B) 0.5
(C) −0.5
(D) −2.5
(E) None of the above.
myKids.add(“Emmie”);
myKids.add(“Gus”);
myKids.add(“Diana”);
System.out.println();
(A) Emmie Emmie Emmie (B) Emmie Emmie Emmie (C) Emmie Gus Diana
Emmie Emmie Emmie Emmie Gus Diana Emmie Emmie Emmie
(D) Emmie Gus Diana (E) Nothing is printed because the first print statement will cause an error.
Emmie Gus Diana
6
17) What are the values of u and v after the following code is executed?
int u = 3, v = 5;
u += v;
v += u;
u -= v;
v -= u;
(A) v is 0 and u is 0
(B) v is −5 and u is −3
(C) v is 3 and u is 5
(D) v is −5 and u is 18
(E) v is 5 and u is 3
int a = 100;
int b = 200;
int c = a + b;
String d = "100";
String e = "200";
String f = d + e;
System.out.println(c);
System.out.println(f);
(A) 300
300
(B) 300
100200
(C) 200
100100
(D) 100200
100200
7
19) Consider the code below.
int x = 14;
while(!(x%3 == 0 && x%5 == 0))
{
System.out.println(x);
x += 2;
}
(A) 30
(B) 28
(C) 14
(D) 16
(E) 18
8
21) Consider the following code segment.
System.out.println();
}
int count = 0;
(A) 4
(B) 8
(C) 10
(D) 16
(E) 20
9
23) Refer to the static method removeNegs shown below.
For which of the following lists will the method not work as intended?
(A) 6 -1 -2 5
(B) -1 2 -3 4
(C) 2 4 6 8
(D) -3
(E) 1 2 3 —8
10
24) Which of the following code segments will compile with no error?
25) Which of the following changes to SomeClass will allow other classes to access but not modify the value
of myC?
(A) Make myC public (B) Include the method: (C) Include the method:
private int getC(){ public void getC(int x){
return myC; x = myC;
} }
while(num > 0)
{
sum += num % 10;
num /= 10;
}
System.out.print(sum);
(A) Find the lowest common denominator of the number the user enters.
(B) Find the remainders of the number the user enters when divided by ten.
(C) Sums each digit of the number the user enters.
(D) Find the quotients of the number the user enters when divided by ten.
(E) Find the factorial of the number the user enters.
11
27) Consider the following code.
int x = 9;
if(x/2 <= 4)
{
x++;
}
if(x/2 >= 5)
{
x += 2;
}
System.out.println(x);
28) Consider a sorted array arr of n elements, where n is large and n is even. Under which conditions will a
sequential search of arr be faster than a binary search?
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only
What will be stored in the variable piece after the code segment executes?
12
30) Consider the following code segment.
int myNumber = 5;
boolean myFlag = true;
if(myFlag)
{
myNumber++;
}
if(myFlag || false)
{
myNumber++;
myFlag = false;
}
if(myFlag && false)
{
myNumber++;
}
31) Consider writing a program that produces statistics for long lists of numerical data. Which of the following
is the best reason to implement each list with an array of int (or double), rather than an ArrayList of
Integer (or Double) objects?
(A) An array of primitive number types is more efficient to manipulate than an ArrayList of wrapper objects
that contain numbers.
(B) Insertion of new elements into a list is easier to code for an array than for an ArrayList.
(C) Removal of new elements into a list is easier to code for an array than for an ArrayList.
(D) Accessing individual elements in the middle of a list is easier for an array than for an ArrayList.
(E) Accessing all the elements is more efficient in an array than in an ArrayList.
The following conditions have been proposed to replace /* condition*/ in the code segment.
I. x < 0
II. x <= 1
III. x < 10
A) I only
B) II only
C) I and II only
D) I and III only
E) I, II, and III
34) Which features can you use to recognize constructor methods in a class declaration?
(A) I only
(B) II only
(C) I & III only
(D) II & III only
(E) I, II & III
(A) 10
(B) 9
(C) 13
(D) 20
(E) 30
I. list.set(2, “6”);
II. list.add(4, “Steven”);
III. String s = list.get(4);
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
Which of the following could replace the body of compute, so that the new version returns the identical result
as the original for all n?
(A) return 4 * n;
(B) return 8 * n;
(C) return 64 * n;
(D) return (int) Math.pow(n, 4);
(E) return (int) Math.pow(n, 8);
38) The Boolean expression A <= B is equivalent to which of the following expressions?
int count = 1;
while ( /*condition goes here */ ) {
System.out.print( count + " " );
count = count + 1;
}
System.out.println();
12345678
Given that n and k are integers, and that the rewritten code performs the same task as the original code, which
of the following could be used as
p = true;
q = false;
System.out.println((!p&&!q)||!(p||q));
(A) true
(B) null
(C) false
(D) Compiler Error
(E) Not enough information to answer the problem
17
42) Consider the following method.
(A) 6
(B) 11
(C) 12
(D) 27
(E) 30
Assume that animals has been instantiated and initialized with the following contents.
You may assume that value1, value2, and value3 are int values. Which of the following is sufficient to
guarantee that /* body of loop */ will never be executed?
20
public static void whatsItDo(String str)
{
int len = str.length();
if(len > 1)
{
String temp = str.substring(0, len-1);
whatsItDo(temp);
System.out.println(temp);
}
}
(A) WATC
WAT
WA
W
(B) WATCH
WATC
WAT
WA
(C) W
WA
WAT
WATC
(D) W
WA
WAT
WATC
WATCH
(E) WATCH
WATC
WAT
WA
W
WA
WAT
WATC
WATCH
49) Which is true of the following Boolean expression, given that x is a variable of type double?
21
3.0 == x * (3.0 / x)
(A) COMSCI
(B) MSCI
(C) OMSCI
(D) O
(E) M
(A) EYBALL
(B) YBALL
(C) LEYBALL
(D) BALL
(E) LEYBALLYBALL
53) What does the following method return if a call to mystery(2,5) is made?
(A) 6
(B) 6.0
(C) 5.5
(D) 5
(E) Nothing. There is an error in the program.
String s = "ONION";
String first = s.substring(1,5);
String other = first.substring(1,4);
String last = other.substring(0,2);
System.out.println(last);
A) I
B) IO
C) ION
D) ONI
E) NION
55) Assume that an array of integer values has been declared as follows and has been initialized.
23
int list[] = new int[15];
Which of the following code correctly swaps the values of list[2] and list[3]?
(C) int n = 3;
list[3] = list[2];
list[2] = list[n];
String s = “goodbye”;
System.out.println(s.substring(s.substring(s.length() - s.indexOf(“d”)).length()));
(A) bye
(B) 3
(C) dbye
(D) 2
(E) odbye
(A) 1234
(B) 123
(C) 12
(D) 4
(E) 31234
58) A class of 30 students rated their computer science teacher on a scale of 1 to 10 (1 means awful and 10
means outstanding). The responses array is a 30-element integer array of the student responses. An 11-element
array freq will count the number of occurrences of each response. For example, freq[6] will count the
number of students who responded 6. The quantity freq[0] will not be used.
Here is a program that counts the students’ responses and outputs the results.
Suppose the last entry in the initializer list for the responses array was incorrectly typed as 12 instead of 2. What
would be the result of running the program?
Which of the following segments of code will NOT reverse the String that word is pointing to?
How many times would the body of the while loop execute when the following statement is executed:
midString(“responsibility”);
61) Given the following method declaration, what value is returned as the result of the call mystery(5)?
(A) 243
(B) 0
(C) 3
(D) 81
(E) 27
27
62) Consider a Monster class that has a default constructor. Suppose a list ArrayList<Monster>
minions is initialized. Which of the following will not cause an IndexOutOfBoundsException to be
thrown?
Which of the following best describes the value returned from a call to doWhat?
(A) num
(B) The sum of all integers between 1 and num inclusive.
(C) The sum of all even integers between 1 and num inclusive.
(D) The sum of all odd integers between 1 and num, inclusive.
(E) No value is returned because of an infinite loop.
28
64) Consider the following method. What is the output from conditionTest(-3,2)?
29
65) Consider the following method
return myList;
}
System.out.println(mystery(6));
I. for(String c: colors)
System.out.print(c + “ “);
III. System.out.print(colors);
(A) I only
(B) II only
(C) I and II only
(D) II and IV only
(E) III and IV only
30
67) Which of the following creates an array correctly?
(A) 2 4 6 8
(B) 2 4 6 8 10
(C) 2 4 6 8 10 1
(D) 2 4 6 8 10 1 3 5 7 9
(E) Nothing - there is a compiler error.
69) The following shuffle algorithm is used to shuffle an array of int values, nums.
Suppose the initial state of nums is 8, 7, 6, 5, 4, and when the method is executed, the values generated for
rand are 3, 2, 0, 0 in that order. What element will be contained in nums[2] after execution?
(A) 8
(B) 7
(C) 6
(D) 5
(E) 4
31
70) Refer to the following code segment.
71) What is returned from mystery when it is passed {10, 30, 30, 60}?
(A) 17.5
(B) 30.0
(C) 130
(D) 32
(E) 32.5
72) Given the following method declaration, what will redo(82, 3) return?
(A) 4
(B) 2
(C) 12
(D) 6
(E) 3
74) What is the values in the array numbers after the code is executed?
(A) 0 1 2 3 4 5
(B) 0 1 4 9 16 25
(C) 1 4 6 8 10 12
(D) 1 2 3 4 5 6
(E) There will be a compiler error because i is declared twice.
33
76) Consider the following method:
77) What is the value of a[1] after the following code is executed?
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
34
78) Consider this inheritance hierarchy, in which Werewolf and Vampire are subclasses of Monster.
Monster
Werewolf Vampire
(A) The Vampire class can have private instance variables that are in neither Monster or Werewolf.
(B) Each of the classes – Monster, Werewolf, Vampire – can have a method growl, whose code in
Monster and Werewolf is identical, but different from the code in Vampire.
(C) If the Monster class has private instance variables age and name, then Werewolf and Vampire
cannot directly access them.
(E) If the Monster class has a private method called wreakHavoc, this method may not be accessed in either
the Werewolf or Vampire classes.
79) The following method is intended to return the smallest int value in an array called arr. Assume that is at
least one value in arr.
35
80) The method fun is defined as:
What is the value of v[0] after the following code segment is executed?
(A) 2
(B) 3
(C) 4
(D) 5
(E) Compiler Error
82) Which of the following correctly initializes an array arr to contain four elements each with value 0?
36
83) What is returned by calling method(8,3)?
(A) 4
(B) 5
(C) 0
(D) 1
(E) Nothing, there is a run time error.
84) When designing a class hierarchy, which of the following is NOT true about the parent class?
(A) The parent class should hold any variables and methods common to all of the child classes.
(B) The parent class’ variables should be made public so the child class can access them.
(C) It is possible for a parent class to have a parent class itself.
(D) The parent class cannot access any of the methods in the child classes.
(E) The child class will have access to the parent class’ public methods and constructor via the super
keyword.
37
85) The following incomplete method is intended to reverse the array parameter.
Which of the following could be used to replace /*missing code */ so that executing the code would
reverse the array?
(A) i
(B) a.length - i - 1
(C) a.length - 1
(D) a.length – i
(E) a.length + 1
87) A class School and its subclass ThirdGrade both have a method calculateGrade(). If arya
refers to an object of type ThirdGrade, what will the following code do?
arya.calculateGrade();
38
88) A sorted list of 200 integers is to be searched to determine whether the value of 150 is in the list. What is
the most efficient sorting algorithm to use in this case and what is the maximum number of elements that would
be examined?
89) Which of the following correctly initializes an array arr to contain 5 elements each with a value of zero?
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
method2() is a non-static method in ClassA. Which of the following method calls will cause an error?
I. x.method2();
II. y.method2();
III. ((ClassB) x).method2();
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) I, II, and III
39
91) What is the output of the following code segment?
The following code segment appears in another method in the same class.
Which of the following represents the contents of arr as a result of executing the code segment?
(A) {6, 4, 2, 4}
(B) {1, 6, 3, 4}
(C) {4, 3, 6, 1}
(D) {4, 4, 2, 2}
(E) {2, 2, 4, 4}
40
93) Consider the following code segment:
int x;
x = 53;
if(x>10)
System.out.print(“A”);
if(x>30)
System.out.print(“B”);
if(x>40)
System.out.print(“C”);
if(x>50)
System.out.print(“D”);
if(x>70)
System.out.print(“E”);
(A) A
(B) D
(C) ABD
(D) ABCD
(E) ABCDE
(A) 4 3 2 1 5 6 7 8 9
(B) 4 3 5 2 6 1 7 8 9
(C) 9 8 7 6 5 1 2 3 4
(D) 9 8 7 1 6 2 5 3 4
(E) Nothing will be printed due to an infinite recursion
41
95) Which of the following statements shows correct syntax to create an object of a Piggy class?
96) Which features can you use to recognize constructor methods in a class declaration?
A) I only B) II only C) I & III only D) II & III only E) I, II & III
42
Now consider the following code in a separate class.
Which of the following code segments can go after the code above to switch the names of the objects?
I.
SomeClass temp = new SomeClass();
temp = x;
x = y;
y = temp;
II.
String temp;
temp = x.myName;
x.myName = y.myName;
y.myName = temp;
III.
String temp;
temp = x.getName();
x.setName(y.getName());
y.setName(temp);
A) I only B) III only C) I and III D) II and III E) I, II, and III
98) Declare and construct an ArrayList that hold integers and has the reference name numbers.
43
99) Assume the following variable declarations have been made:
double r = Math.random();
int c;
Which of the following assigns a value to c that is between -10 <= c < 10?
String s= "ABCDEFG";
System.out.println(s.substring(s.length()/2, s.length()));
(A) ABCD
(B) ABCDEFG
(C) DEFG
(D) DEF
(E) EFG
44
101) Consider the following class declarations.
45
102) Examine the following code:
Which of the following will replace the element "Brenden" with "Brandon" ?
46
104) Consider the following code segment. What value is in sum after this code executes?
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++) {
sum = sum + matrix[row][col];
}
(A) 4
(B) 8
(C) 9
(D) 12
(E) 10
(A) 12
(B) 16
(C) 20
(D) 23
(E) 25
(A) An ArrayList can grow or shrink as needed, while an array is always the same size.
(B) You can use a for-each loop on an ArrayList, but not in an array.
(C) You can store objects in an ArrayList, but not in an array.
(D) Primitive types are easier to manipulate with the wrappers in ArrayList.
(E) There is no benefit to use either; they work in the exact same way.
47
Use the following classes for questions 107 – 109.
48
107) A client method has this declaration followed by the code to initialize the classList;
for(Student s : classList)
/*line of code*/
(A) System.out.println(s[i].getID());
(B) System.out.println(Student[i].getID());
(C) System.out.println(Student.getID());
(D) System.out.println(s.getID());
(E) System.out.println(s.ID);
108) A client method has this declaration followed by the code to initialize the classList.
Here is a code segment to generate a list of student names and addresses only:
for(Student s : classList)
/*line of code*/
49
109) Here is a method in the Student class intended to return the highest grade for this Student. Assume
each Student has at least 1 grade.
Which of the following would replace /*method body*/ so that the method works as intended?
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I and III only
50
Use the following classes to answer questions 110 - 111.
II. super.computePay();
return getSalary() + BONUS;
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) I and II only
52
112) Consider an array arr that is initialized with int values. The following code fragment stores in count the
number of positive values in arr.
I. int count = 0;
for (int num : arr){
if (arr[num] > 0)
count++;
}
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I and III only
53
113) A square matrix is declared as
public static void mystery(int[][] mat, int value, int top, int left, int bottom, int right)
{
for (int i = left; i <= right; i++){
mat [top][i] = value;
mat [bottom][i] = value;
}
for (int i = top + 1; i <= bottom — 1; i++){
mat[i][left] = value;
mat[i][right] = value;
}
}
Assuming that there are no out-of-range errors, which best describes what method mystery does?
(A) Places value in corners of the rectangle with corners (top, left) and (bottom, right).
(B) Places value in the diagonals of the square with corners (top, left) and (bottom, right).
(C) Places value in each element of the rectangle with corners (top, left) and (bottom, right).
(D) Places value in each element of the border of the rectangle with corners (top, left) and (bottom, right).
(E) Places value in the topmost and bottommost rows of the rectangle with corners (top, left) and (bottom, right).
(A) 4
(B) 2
(C) 0
(D) -2
(E) -4
54
115) Consider the following method:
(A) 16
(B) 18
(C) 24
(D) 32
(E) 72
(A) 1 1
(B) 2 3
(C) 2
(D) 1 3 2
(E) 2 3 1
55
117) Consider the following code segment:
int x = 4;
int y = 142;
while (x <= y)
{
int m = Math.abs(x - y);
y = y/x;
System.out.print(m + " ");
}
(A) 2
(B) 4
(C) 138 31 4
(D) 35 8 2
(E) 138
(A) [1, 2, 3, 4, 5]
(B) [1, 2, 4, 5, 6]
(C) [1, 2, 5, 4, 6]
(D) [1, 5, 2, 4, 6]
(E) [1, 5. 4, 2, 6]
56
119) Consider the code segment
and that further statements (not shown) have put String references into some of the array cells.
Which of the following fragments prints out the cells of the array from last to first, skipping cells that contain
null?
120) Assume that x and y have been defined and initialized as int values. The expression
(A) (a != b) || (b < 7)
(B) (a != b) || (b <= 7)
(C) (a == b) || (b <= 7)
(D) (a != b) && (b <= 7)
(E) (a == b) && (b > 7)
57