AP® Computer Science A Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions unit by unit, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Question 1712
Question 1713
Question 1714
Question 1715
Question 1716
public class Lamp
{
private boolean on;
public Lamp(boolean start)
{
on = start;
}
public void toggle()
{
on = !on;
}
public boolean isOn()
{
return on;
}
}
Lamp a = new Lamp(true);
Lamp b = a;
b.toggle();
Lamp c = new Lamp(true);
c.toggle();
System.out.println(a.isOn() + " " + c.isOn());
Main Solution
Answer
Answer: D. Since b and a refer to the same object, b.toggle() changes a to false. The separate object c also starts as true and becomes false after toggle().
Answer: Correct: D
A
true true
B
true false
C
false true
D
false false
Answer images
Question 1717
Question 1718
Question 1719
public class Seat
{
private static int num = 10;
private int id;
public Seat()
{
num++;
id = num;
}
public int getId()
{
return id;
}
}
Seat objects are constructed, what IDs do they receive, in order?Main Solution
Answer
Answer: B. The static variable num starts at 10. The first constructor call increments it to 11 and stores that value, and the second call increments it to 12 and stores that value.
Answer: Correct: B
A
10, 11
B
11, 12
C
12, 13
D
10, 10
Answer images
Question 1720
public class SwitchBox
{
private boolean on;
public SwitchBox(boolean start)
{
on = start;
}
public boolean flip()
{
boolean old = on;
on = !on;
return old;
}
public boolean isOn()
{
return on;
}
}
SwitchBox b = new SwitchBox(false);
System.out.println(b.flip() + " " + b.isOn());
Main Solution
Answer
Answer: D. The method flip returns the old value, which is false, and then changes the field to true. Therefore the printed output is false true.
Answer: Correct: D
A
true true
B
true false
C
false false
D
false true
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

