Verityprep
Question Bank · Unit 3

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

Questions 341–350 of 359
  1. 341
  2. 342
  3. 343
  4. 344
  5. 345
  6. 346
  7. 347
  8. 348
  9. 349
  10. 350

Question 341

Multiple choice Writing Classes
Consider the following class definition. public class Badge { private static int next = 1; private int id; public Badge() { id = next; next = next + 2; } public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 342

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 343

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 344

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 345

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 346

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 347

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 348

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 349

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 350

Multiple choice Writing Classes
Consider the following class. public class Bank { private int balance; public Bank(int b) { balance = b; } public int update(int amount) { int old = balance;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 341 of 359
Page 35 of 36