AP® Computer Science A Class Creation Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions on Class Creation, 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.
Subtopic: Writing Classes Clear filter
Question 342
Question 343
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 344
Question 345
Question 346
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 347
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
Question 348
public class Tag
{
private String text;
public Tag(String t)
{
text = t;
}
public void add(String s)
{
text = text + s;
}
public String show()
{
return text;
}
}
Tag x = new Tag("AP");
Tag y = new Tag("C");
y = x;
x.add("CSA");
System.out.println(x.show() + " " + y.show());
Main Solution
Answer
Answer: A. After y = x, both variables refer to the same Tag object. Appending "CSA" through x changes that shared object, so both method calls return "APCSA".
Answer: Correct: A
A
APCSA APCSA
B
APCSA C
C
AP APCSA
D
C APCSA
Answer images
Question 349
Question 350
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.

