Calling Instance Methods MCQ Practice

Question 1 of 10

The Hopper class is supplied. What text does this caller display?

class Hopper {
    private int grain;
    public Hopper() {
        grain = 12;
    }
    public void pour(int n) {
        grain += n;
    }
    public int amount() {
        return grain;
    }
}

Hopper bin = new Hopper();
bin.pour(5);
System.out.print(bin.amount());
  1. A. 5
  2. B. 12
  3. C. 17
  4. D. 0

Question 2 of 10

The Gauge class is supplied. What text does this caller display?

class Gauge {
    private int psi;
    public Gauge(int start) {
        psi = start;
    }
    public void bump(int n) {
        psi += n;
    }
    public int reading() {
        return psi;
    }
}

Gauge tank = new Gauge(40);
Gauge also = tank;
also.bump(6);
System.out.print(tank.reading() + "," + also.reading());
  1. A. 40,46
  2. B. 46,46
  3. C. 46,40
  4. D. 40,40

Question 3 of 10

The StampPad class is supplied. What text does this caller display?

class StampPad {
    private int ink;
    public StampPad(int start) {
        ink = start;
    }
    public void stamp() {
        ink -= 2;
    }
    public int remaining() {
        return ink;
    }
}

StampPad desk = new StampPad(10);
StampPad hall = new StampPad(10);
desk.stamp();
System.out.print(desk.remaining() + "," + hall.remaining());
  1. A. 8,8
  2. B. 8,10
  3. C. 10,8
  4. D. 10,10

Question 4 of 10

The Ledger class is supplied. What text does this caller display?

class Ledger {
    private int cents;
    public Ledger(int start) {
        cents = start;
    }
    public void credit(int n) {
        cents += n;
    }
    public int balance() {
        return cents;
    }
}

Ledger drawer = new Ledger(50);
int saved = drawer.balance();
drawer.credit(12);
System.out.print(saved + "," + drawer.balance());
  1. A. 62,62
  2. B. 50,62
  3. C. 50,50
  4. D. 12,62

Question 5 of 10

The Hopper class is supplied. The table summarizes its instance methods. Which statement about pour is correct?

class Hopper {
    private int grain;
    public Hopper() {
        grain = 12;
    }
    public void pour(int n) {
        grain += n;
    }
    public int amount() {
        return grain;
    }
}
Method Result type Effect on grain
pour(int n) void adds n to the receiver
amount() int returns the current value
  1. A. int x = bin.pour(2); stores 2 if bin exists.
  2. B. pour is void, so a caller cannot assign its result to a variable.
  3. C. pour returns the new grain amount automatically.
  4. D. Calling pour on a Hopper always throws NullPointerException.

Question 6 of 10

The Gauge class is supplied. What happens when this caller runs?

class Gauge {
    private int psi;
    public Gauge(int start) {
        psi = start;
    }
    public void bump(int n) {
        psi += n;
    }
    public int reading() {
        return psi;
    }
}

Gauge tank = null;
System.out.print(tank.reading());
  1. A. It displays 0.
  2. B. It displays null.
  3. C. It throws NullPointerException because the receiver is null.
  4. D. It constructs a Gauge with psi 0 and displays 0.

Question 7 of 10

The StampPad class is supplied. What text does this caller display?

class StampPad {
    private int ink;
    public StampPad(int start) {
        ink = start;
    }
    public void stamp() {
        ink -= 2;
    }
    public int remaining() {
        return ink;
    }
}

StampPad pad = new StampPad(9);
pad.stamp();
pad.stamp();
System.out.print(pad.remaining());
  1. A. 9
  2. B. 7
  3. C. 5
  4. D. 2

Question 8 of 10

In the call bin.pour(5), which object is the receiver whose grain is updated?

class Hopper {
    private int grain;
    public Hopper() {
        grain = 12;
    }
    public void pour(int n) {
        grain += n;
    }
    public int amount() {
        return grain;
    }
}

Hopper bin = new Hopper();
Hopper spare = new Hopper();
bin.pour(5);
  1. A. spare, because it was declared second
  2. B. bin, the object named before the dot
  3. C. A newly constructed Hopper created by pour
  4. D. No object; pour only changes the literal 5

Question 9 of 10

The Gauge class is supplied. What text does this caller display?

class Gauge {
    private int psi;
    public Gauge(int start) {
        psi = start;
    }
    public void bump(int n) {
        psi += n;
    }
    public int reading() {
        return psi;
    }
}

Gauge tank = new Gauge(15);
tank.bump(4);
int now = tank.reading();
tank.bump(1);
System.out.print(now);
  1. A. 20
  2. B. 19
  3. C. 15
  4. D. 16

Question 10 of 10

The Ledger class is supplied. What text does this caller display?

class Ledger {
    private int cents;
    public Ledger(int start) {
        cents = start;
    }
    public void credit(int n) {
        cents += n;
    }
    public int balance() {
        return cents;
    }
}

Ledger a = new Ledger(20);
Ledger b = new Ledger(20);
b.credit(5);
System.out.print(a.balance());
  1. A. 25
  2. B. 20
  3. C. 5
  4. D. 0