Calling Instance Methods MCQ Practice

Question 1 of 10

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

class SiloBin {
    private int kg;
    public SiloBin(int start) {
        kg = start;
    }
    public void fill(int n) {
        kg += n;
    }
    public int stored() {
        return kg;
    }
}

SiloBin east = new SiloBin(20);
east.fill(7);
System.out.print(east.stored());
  1. A. 7
  2. B. 20
  3. C. 27
  4. D. 13

Question 2 of 10

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

class BadgePress {
    private int count;
    public BadgePress(int start) {
        count = start;
    }
    public void press() {
        count++;
    }
    public int made() {
        return count;
    }
}

BadgePress shop = new BadgePress(3);
BadgePress also = shop;
shop.press();
System.out.print(also.made());
  1. A. 3
  2. B. 4
  3. C. 1
  4. D. 0

Question 3 of 10

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

class Lantern {
    private int lumens;
    public Lantern(int start) {
        lumens = start;
    }
    public void dim(int n) {
        lumens -= n;
    }
    public int glow() {
        return lumens;
    }
}

Lantern porch = new Lantern(18);
Lantern trail = new Lantern(18);
porch.dim(5);
System.out.print(porch.glow() + "," + trail.glow());
  1. A. 13,13
  2. B. 13,18
  3. C. 18,13
  4. D. 5,18

Question 4 of 10

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

class CableSpool {
    private int meters;
    public CableSpool(int start) {
        meters = start;
    }
    public void cut(int n) {
        meters -= n;
    }
    public int left() {
        return meters;
    }
}

CableSpool reel = new CableSpool(50);
int saved = reel.left();
reel.cut(8);
System.out.print(saved + "," + reel.left());
  1. A. 42,42
  2. B. 50,42
  3. C. 50,50
  4. D. 8,42

Question 5 of 10

The SiloBin class is supplied. Which call is illegal?

class SiloBin {
    private int kg;
    public SiloBin(int start) {
        kg = start;
    }
    public void fill(int n) {
        kg += n;
    }
    public int stored() {
        return kg;
    }
}

SiloBin east = new SiloBin(20);
  1. A. east.fill(3);
  2. B. int n = east.stored();
  3. C. int n = east.fill(3);
  4. D. System.out.print(east.stored());

Question 6 of 10

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

class BadgePress {
    private int count;
    public BadgePress(int start) {
        count = start;
    }
    public void press() {
        count++;
    }
    public int made() {
        return count;
    }
}

BadgePress shop = null;
shop.press();
  1. A. It prints 1.
  2. B. It constructs a BadgePress and then presses once.
  3. C. It throws NullPointerException because shop is a null receiver.
  4. D. It leaves count at 0 and continues.

Question 7 of 10

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

class Lantern {
    private int lumens;
    public Lantern(int start) {
        lumens = start;
    }
    public void dim(int n) {
        lumens -= n;
    }
    public int glow() {
        return lumens;
    }
}

Lantern lamp = new Lantern(30);
lamp.dim(4);
lamp.dim(6);
System.out.print(lamp.glow());
  1. A. 30
  2. B. 26
  3. C. 20
  4. D. 10

Question 8 of 10

In east.fill(7), what is the receiver?

class SiloBin {
    private int kg;
    public SiloBin(int start) {
        kg = start;
    }
    public void fill(int n) {
        kg += n;
    }
    public int stored() {
        return kg;
    }
}

SiloBin east = new SiloBin(20);
SiloBin west = new SiloBin(20);
east.fill(7);
  1. A. west
  2. B. east
  3. C. The literal 7
  4. D. A hidden third SiloBin created by fill

Question 9 of 10

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

class CableSpool {
    private int meters;
    public CableSpool(int start) {
        meters = start;
    }
    public void cut(int n) {
        meters -= n;
    }
    public int left() {
        return meters;
    }
}

CableSpool reel = new CableSpool(16);
reel.cut(3);
int now = reel.left();
reel.cut(1);
System.out.print(now);
  1. A. 16
  2. B. 13
  3. C. 12
  4. D. 1

Question 10 of 10

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

class BadgePress {
    private int count;
    public BadgePress(int start) {
        count = start;
    }
    public void press() {
        count++;
    }
    public int made() {
        return count;
    }
}

BadgePress a = new BadgePress(2);
BadgePress b = new BadgePress(2);
a.press();
System.out.print(b.made());
  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4