Anatomy of a Class MCQ Practice

Question 1 of 10

A nursery program models one greenhouse tray. Which class header matches the AP CSA requirement for a class?

  1. A. public class Tray
  2. B. private class Tray
  3. C. protected class Tray
  4. D. class Tray with no access keyword

Question 2 of 10

A Locker class needs a constructor that receives an int combination. Which constructor header is required?

  1. A. private Locker(int combo)
  2. B. public Locker(int combo)
  3. C. Locker(int combo) with no access keyword
  4. D. protected Locker(int combo)

Question 3 of 10

The question specification does not mention a special access rule for humidity. How should that Greenhouse attribute be designated?

  1. A. public int humidity so clients can assign it directly
  2. B. protected int humidity so subclasses can share it
  3. C. int humidity with no keyword so the package can share it
  4. D. private int humidity to encapsulate the attribute

Question 4 of 10

Each Planter object keeps its own soilMoisture. What text does this Java code display?

public class Planter {
    private int soilMoisture;
    public Planter(int start) {
        soilMoisture = start;
    }
    public void water(int extra) {
        soilMoisture += extra;
    }
    public int getSoilMoisture() {
        return soilMoisture;
    }
}

Planter left = new Planter(4);
Planter right = new Planter(4);
left.water(3);
System.out.print(left.getSoilMoisture() + " " + right.getSoilMoisture());
  1. A. 7 7
  2. B. 7 4
  3. C. 4 7
  4. D. 3 4

Question 5 of 10

A client constructs a Mister. Which statement about calling its methods is correct?

public class Mister {
    private int bursts;
    public Mister() {
        bursts = 0;
    }
    private void addBurst() {
        bursts++;
    }
    public void mistOnce() {
        addBurst();
    }
    public int getBursts() {
        return bursts;
    }
}
  1. A. The client may call both mistOnce and addBurst.
  2. B. The client may call only addBurst because it is the helper.
  3. C. The client may call mistOnce, but addBurst is internal to Mister.
  4. D. The client may call neither method because bursts is private.

Question 6 of 10

A greenhouse tags one plant. What text does this Java code display?

public class Tag {
    private String code;
    public Tag(String start) {
        code = start;
    }
    public void stamp(String extra) {
        code = code + extra;
    }
    public String getCode() {
        return code;
    }
}

Tag t = new Tag("G");
t.stamp("12");
System.out.print(t.getCode());
  1. A. G
  2. B. 12
  3. C. G12
  4. D. stamp12

Question 7 of 10

A client already has Bench b = new Bench(6);. Which additional statement compiles?

public class Bench {
    private int seats;
    public Bench(int n) {
        seats = n;
    }
    public int getSeats() {
        return seats;
    }
    private void hide() {
        seats = 0;
    }
    public void fold() {
        hide();
    }
}
  1. A. int n = b.getSeats();
  2. B. b.seats = 8;
  3. C. b.hide();
  4. D. int n = b.seats;

Question 8 of 10

Two shade cloths are separate objects. What text does this Java code display?

public class Shade {
    private boolean down;
    public Shade() {
        down = false;
    }
    public void lower() {
        down = true;
    }
    public boolean isDown() {
        return down;
    }
}

Shade north = new Shade();
Shade south = new Shade();
north.lower();
System.out.print(north.isDown() + " " + south.isDown());
  1. A. true true
  2. B. true false
  3. C. false true
  4. D. false false

Question 9 of 10

A question specification states that client code should read and write the instance variable width of a Frame. Which declaration matches that specification?

  1. A. private int width; because attributes are never public
  2. B. width must be a local variable inside a method, not a field
  3. C. The Frame class must omit width and compute it each time
  4. D. public int width; because the specification says otherwise

Question 10 of 10

A valve uses a private helper. What text does this Java code display?

public class Valve {
    private int turns;
    public Valve() {
        turns = 2;
    }
    private void spin() {
        turns++;
    }
    public void open() {
        spin();
        spin();
    }
    public int getTurns() {
        return turns;
    }
}

Valve v = new Valve();
v.open();
System.out.print(v.getTurns());
  1. A. 2
  2. B. 3
  3. C. 4
  4. D. The code does not compile because spin is private.