AP® Computer Science A Class Creation Practice Questions
Work through AP Computer Science A practice 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.
Question 382
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 383
Question 384
Question 385
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 386
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 387
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 388
Question 389
A school activity office keeps a line of students waiting to board a shuttle for an event. A BoardingLine object stores the names of students currently waiting.
A BoardingLine object is created with a maximum capacity. The class supports the following behavior:
addStudentadds a student name to the end of the line if the line is not full and the name is not already present. The method returnstrueif the name is added andfalseotherwise.boardNextremoves and returns the name at the front of the line. If the line is empty, the method returnsnull.lineSizereturns the number of students currently waiting.
The following table contains a sample code execution sequence and the corresponding results.
| Statements and Expressions | Value Returned (blank if no value) | Comment |
|---|---|---|
BoardingLine line = new BoardingLine(3); |
Creates a new line that can hold at most three students. | |
line.lineSize(); |
0 |
No students are waiting yet. |
line.boardNext(); |
null |
There is nobody to board. |
line.addStudent("Ava"); |
true |
"Ava" is added to the end of the line. |
line.addStudent("Noah"); |
true |
The line now contains "Ava", "Noah", in that order. |
line.addStudent("Ava"); |
false |
Duplicate names are not added. |
line.addStudent("Mia"); |
true |
The line reaches capacity. |
line.addStudent("Leo"); |
false |
The line is full, so "Leo" is not added. |
line.boardNext(); |
"Ava" |
The front student is removed and returned. |
line.lineSize(); |
2 |
The remaining students are "Noah" and "Mia". |
Write the complete BoardingLine class, including the constructor, instance variables, and all methods described above. Your implementation must conform to all specifications and must behave correctly for all valid inputs.
Main Solution
Sample Solution
public class BoardingLine
{
private ArrayList<String> students;
private int capacity;
public BoardingLine(int maxCapacity)
{
students = new ArrayList<String>();
capacity = maxCapacity;
}
public boolean addStudent(String name)
{
if (students.size() >= capacity)
{
return false;
}
for (int i = 0; i < students.size(); i++)
{
if (students.get(i).equals(name))
{
return false;
}
}
students.add(name);
return true;
}
public String boardNext()
{
if (students.size() == 0)
{
return null;
}
return students.remove(0);
}
public int lineSize()
{
return students.size();
}
}
Scoring (9 pts): +1 appropriate private instance variables, +1 constructor initializes line and capacity, +1 rejects additions when full, +1 rejects duplicate names, +1 adds a valid name at the end and returns true, +1 returns null when empty, +1 removes and returns the front name correctly, +1 lineSize returns the current size, +1 class compiles using only allowed constructs.
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.

