Verityprep
Question Bank · Unit 3

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.

Questions 381–390 of 402
  1. 381
  2. 382
  3. 383
  4. 384
  5. 385
  6. 386
  7. 387
  8. 388
  9. 389
  10. 390

Question 381

Multiple choice Writing Classes
Suppose balance is declared private in class…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 382

Multiple choice Writing Classes
Consider the following class.
public class Lamp
{
    private boolean on;

    public Lamp(boolean start)
    {
        on = start;
    }

    public void toggle()
    {
        on = !on;
    }

    public boolean isOn()
    {
        return on;
    }
}
Assume the following code segment appears in another class.
Lamp a = new Lamp(true);
Lamp b = a;
b.toggle();
Lamp c = new Lamp(true);
c.toggle();
System.out.println(a.isOn() + " " + c.isOn());
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 383

Multiple choice Writing Classes
Consider the following class. public class Pair { private int left; private int right; public Pair(int l, int r) { left = l; right = r; } public void shift() {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 384

Multiple choice Writing Classes
Consider the following class. public class Record { private int score; private String label; } Assume no constructor…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 385

Multiple choice Writing Classes
Consider the following class.
public class Seat
{
    private static int num = 10;
    private int id;

    public Seat()
    {
        num++;
        id = num;
    }

    public int getId()
    {
        return id;
    }
}
If two Seat objects are constructed, what IDs do they receive, in order?
Pick an answer, then check it. The solution opens in a window.

Question 386

Multiple choice Writing Classes
Consider the following class.
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;
    }
}
Assume the following code segment appears in another class.
SwitchBox b = new SwitchBox(false);
System.out.println(b.flip() + " " + b.isOn());
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 387

Multiple choice Writing Classes
Consider the following class.
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;
    }
}
Assume the following code segment appears in another class.
Tag x = new Tag("AP");
Tag y = new Tag("C");
y = x;
x.add("CSA");
System.out.println(x.show() + " " + y.show());
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 388

Multiple choice Writing Classes
Consider the following class. public class ScoreBoard { private int wins; public ScoreBoard() { wins = 0; } public void recordWin() { wins++; } public void…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 389

Free response Writing Classes

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:

  • addStudent adds a student name to the end of the line if the line is not full and the name is not already present. The method returns true if the name is added and false otherwise.
  • boardNext removes and returns the name at the front of the line. If the line is empty, the method returns null.
  • lineSize returns 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.

The answer and the full solution open in a window.

Question 390

Free response Writing Classes
The TemperatureGrid class stores a rectangular grid of integer temperature readings. public class TemperatureGrid { private int[][] temps; public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 381 of 402
Page 39 of 41