Methods: Passing and Returning References of an Object MCQ Practice

Question 1 of 10

A booth shades a lamp through a parameter. What text does this Java code display?

public class Lamp {
    private int watts;
    public Lamp(int w) {
        watts = w;
    }
    public void dim(int n) {
        watts -= n;
    }
    public int getWatts() {
        return watts;
    }
}
public class Booth {
    public void shade(Lamp bulb) {
        bulb.dim(10);
    }
}

Lamp desk = new Lamp(60);
Booth b = new Booth();
b.shade(desk);
System.out.print(desk.getWatts());
  1. A. 60
  2. B. 50
  3. C. 10
  4. D. 0

Question 2 of 10

A booth reassigns its lamp parameter. What text does this Java code display?

public class Lamp {
    private int watts;
    public Lamp(int w) {
        watts = w;
    }
    public int getWatts() {
        return watts;
    }
}
public class Booth {
    public void swap(Lamp bulb) {
        bulb = new Lamp(0);
    }
}

Lamp desk = new Lamp(60);
Booth b = new Booth();
b.swap(desk);
System.out.print(desk.getWatts());
  1. A. 0
  2. B. 60
  3. C. null
  4. D. 10

Question 3 of 10

A clerk adds items to a cart through a parameter. What text does this Java code display?

public class Cart {
    private int items;
    public Cart(int n) {
        items = n;
    }
    public void add(int n) {
        items += n;
    }
    public int getItems() {
        return items;
    }
}
public class Clerk {
    public void pack(Cart c) {
        c.add(3);
    }
}

Cart basket = new Cart(4);
Clerk k = new Clerk();
k.pack(basket);
System.out.print(basket.getItems());
  1. A. 4
  2. B. 3
  3. C. 7
  4. D. 0

Question 4 of 10

A booth returns a lamp reference. What text does this Java code display?

public class Lamp {
    private int watts;
    public Lamp(int w) {
        watts = w;
    }
    public void dim(int n) {
        watts -= n;
    }
    public int getWatts() {
        return watts;
    }
}
public class Booth {
    public Lamp share(Lamp bulb) {
        return bulb;
    }
}

Lamp one = new Lamp(40);
Booth b = new Booth();
Lamp two = b.share(one);
two.dim(5);
System.out.print(one.getWatts() + "," + two.getWatts());
  1. A. 40,35
  2. B. 35,35
  3. C. 40,40
  4. D. 35,40

Question 5 of 10

A cart spec says reportWeight(Cart c) must return the item count and must not change c. Which method follows the spec?

Method Body
A c.add(1); return c.getItems();
B return c.getItems();
C c = new Cart(0); return 0;
D c.add(-c.getItems()); return 0;
  1. A. Method A
  2. B. Method B
  3. C. Method C
  4. D. Method D

Question 6 of 10

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

public class Cart {
    private int items;
    public Cart(int n) {
        items = n;
    }
    public int totalWith(Cart other) {
        return items + other.items;
    }
}

Cart a = new Cart(4);
Cart b = new Cart(7);
System.out.print(a.totalWith(b));
  1. A. 4
  2. B. 7
  3. C. 11
  4. D. The code does not compile

Question 7 of 10

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

public class Lamp {
    private int watts;
    public Lamp(int w) {
        watts = w;
    }
    public int getWatts() {
        return watts;
    }
}
public class Clerk {
    public int glowOf(Lamp lamp) {
        return lamp.watts;
    }
}
  1. A. watts = w; in the Lamp constructor
  2. B. return watts; in getWatts
  3. C. return lamp.watts; in glowOf
  4. D. the Lamp constructor header

Question 8 of 10

A shelf method mutates a bowl and returns it. What text does this Java code display?

public class Bowl {
    private int scoops;
    public Bowl(int n) {
        scoops = n;
    }
    public void add(int n) {
        scoops += n;
    }
    public int getScoops() {
        return scoops;
    }
}
public class Shelf {
    public Bowl refill(Bowl b) {
        b.add(2);
        return b;
    }
}

Bowl cup = new Bowl(1);
Shelf s = new Shelf();
Bowl also = s.refill(cup);
System.out.print(cup.getScoops() + "," + also.getScoops());
  1. A. 1,3
  2. B. 3,3
  3. C. 1,1
  4. D. 3,1

Question 9 of 10

A clerk mutates one cart parameter and reassigns the other. What text does this Java code display?

public class Cart {
    private int items;
    public Cart(int n) {
        items = n;
    }
    public void add(int n) {
        items += n;
    }
    public int getItems() {
        return items;
    }
}
public class Clerk {
    public void mix(Cart left, Cart right) {
        left.add(2);
        right = left;
    }
}

Cart red = new Cart(5);
Cart blue = new Cart(9);
Clerk k = new Clerk();
k.mix(red, blue);
System.out.print(red.getItems() + "," + blue.getItems());
  1. A. 7,9
  2. B. 7,7
  3. C. 5,9
  4. D. 7,5

Question 10 of 10

A booth sometimes returns a new lamp. What text does this Java code display?

public class Lamp {
    private int watts;
    public Lamp(int w) {
        watts = w;
    }
    public void dim(int n) {
        watts -= n;
    }
    public int getWatts() {
        return watts;
    }
}
public class Booth {
    public Lamp copyOrSame(Lamp bulb, boolean copy) {
        if (copy) {
            return new Lamp(bulb.getWatts());
        }
        return bulb;
    }
}

Lamp a = new Lamp(30);
Booth b = new Booth();
Lamp x = b.copyOrSame(a, true);
Lamp y = b.copyOrSame(a, false);
x.dim(4);
y.dim(1);
System.out.print(a.getWatts() + "," + x.getWatts() + "," + y.getWatts());
  1. A. 29,26,29
  2. B. 30,26,29
  3. C. 25,26,29
  4. D. 29,26,30