Methods: Passing and Returning References of an Object MCQ Practice

Question 1 of 10

A stage dims a torch through a parameter. What text does this Java code display?

public class Torch {
    private int flame;
    public Torch(int n) {
        flame = n;
    }
    public void lower(int n) {
        flame -= n;
    }
    public int getFlame() {
        return flame;
    }
}
public class Stage {
    public void hush(Torch t) {
        t.lower(6);
    }
}

Torch post = new Torch(18);
Stage s = new Stage();
s.hush(post);
System.out.print(post.getFlame());
  1. A. 18
  2. B. 12
  3. C. 6
  4. D. 0

Question 2 of 10

A stage points its parameter at a new torch. What text does this Java code display?

public class Torch {
    private int flame;
    public Torch(int n) {
        flame = n;
    }
    public int getFlame() {
        return flame;
    }
}
public class Stage {
    public void replace(Torch t) {
        t = new Torch(1);
    }
}

Torch post = new Torch(18);
Stage s = new Stage();
s.replace(post);
System.out.print(post.getFlame());
  1. A. 1
  2. B. 18
  3. C. 0
  4. D. 19

Question 3 of 10

A packer fills a basket through a parameter. What text does this Java code display?

public class Basket {
    private int pears;
    public Basket(int n) {
        pears = n;
    }
    public void put(int n) {
        pears += n;
    }
    public int getPears() {
        return pears;
    }
}
public class Packer {
    public void fill(Basket b) {
        b.put(4);
    }
}

Basket bin = new Basket(2);
Packer p = new Packer();
p.fill(bin);
System.out.print(bin.getPears());
  1. A. 2
  2. B. 4
  3. C. 6
  4. D. 8

Question 4 of 10

A packer returns a basket reference. What text does this Java code display?

public class Basket {
    private int pears;
    public Basket(int n) {
        pears = n;
    }
    public void put(int n) {
        pears += n;
    }
    public int getPears() {
        return pears;
    }
}
public class Packer {
    public Basket pass(Basket b) {
        return b;
    }
}

Basket first = new Basket(5);
Packer p = new Packer();
Basket second = p.pass(first);
second.put(2);
System.out.print(first.getPears() + "," + second.getPears());
  1. A. 5,7
  2. B. 7,7
  3. C. 5,5
  4. D. 7,5

Question 5 of 10

A mug spec says peek(Mug m) must return the sip count and must not change m. Which method follows the spec?

Method Body
W m.sip(); return m.getSips();
X return m.getSips();
Y m = new Mug(0); return m.getSips();
Z m.sip(); m.sip(); return 0;
  1. A. W
  2. B. X
  3. C. Y
  4. D. Z

Question 6 of 10

Two parcels are the same class type. What text does this Java code display?

public class Parcel {
    private int grams;
    public Parcel(int n) {
        grams = n;
    }
    public int combined(Parcel other) {
        return grams + other.grams;
    }
}

Parcel p = new Parcel(8);
Parcel q = new Parcel(5);
System.out.print(p.combined(q));
  1. A. 8
  2. B. 5
  3. C. 13
  4. D. The code does not compile

Question 7 of 10

A rack method is written next to Torch. Which line causes a compile-time error?

public class Torch {
    private int flame;
    public Torch(int n) {
        flame = n;
    }
    public int getFlame() {
        return flame;
    }
}
public class Rack {
    public int peek(Torch t) {
        return t.flame;
    }
}
  1. A. flame = n; in the Torch constructor
  2. B. return flame; in getFlame
  3. C. return t.flame; in peek
  4. D. the Torch constructor header

Question 8 of 10

A rack mutates a token and returns it. What text does this Java code display?

public class Token {
    private int marks;
    public Token(int n) {
        marks = n;
    }
    public void stamp() {
        marks += 1;
    }
    public int getMarks() {
        return marks;
    }
}
public class Rack {
    public Token punch(Token t) {
        t.stamp();
        return t;
    }
}

Token chip = new Token(4);
Rack r = new Rack();
Token also = r.punch(chip);
System.out.print(chip.getMarks() + "," + also.getMarks());
  1. A. 4,5
  2. B. 5,5
  3. C. 4,4
  4. D. 5,4

Question 9 of 10

A packer mutates one basket and reassigns the other. What text does this Java code display?

public class Basket {
    private int pears;
    public Basket(int n) {
        pears = n;
    }
    public void put(int n) {
        pears += n;
    }
    public int getPears() {
        return pears;
    }
}
public class Packer {
    public void shift(Basket left, Basket right) {
        left.put(3);
        right = new Basket(0);
    }
}

Basket oak = new Basket(1);
Basket pine = new Basket(8);
Packer p = new Packer();
p.shift(oak, pine);
System.out.print(oak.getPears() + "," + pine.getPears());
  1. A. 4,8
  2. B. 4,0
  3. C. 1,8
  4. D. 4,1

Question 10 of 10

A stage returns either a new torch or the same torch. What text does this Java code display?

public class Torch {
    private int flame;
    public Torch(int n) {
        flame = n;
    }
    public void lower(int n) {
        flame -= n;
    }
    public int getFlame() {
        return flame;
    }
}
public class Stage {
    public Torch fork(Torch t, boolean fresh) {
        if (fresh) {
            return new Torch(t.getFlame());
        }
        return t;
    }
}

Torch a = new Torch(10);
Stage s = new Stage();
Torch x = s.fork(a, true);
Torch y = s.fork(a, false);
x.lower(2);
y.lower(3);
System.out.print(a.getFlame() + "," + x.getFlame() + "," + y.getFlame());
  1. A. 7,8,7
  2. B. 10,8,7
  3. C. 5,8,7
  4. D. 7,8,10