Calling a Void Method with Parameters

AP Computer Science A· difficulty 3/5

public class Cell {
    public int v;
}
public class Helper {
    public static void bump(Cell c) {
        c.v++;
    }
}
// ...
Cell c = new Cell();
c.v = 10;
Helper.bump(c);
System.out.println(c.v);

What is printed?

  • A

    11

    check_circle
  • B

    10

  • C

    0

  • D

    Compile-time error

Explanation

The reference is passed by value; bump modifies the same object's field through that reference.

Want 10 more like this — adaptive to your weak spots?

Related questions