Creating and Storing Objects (Instantiation)

AP Computer Science A· difficulty 3/5

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

What is printed?

  • A

    999

  • B

    10

    check_circle
  • C

    Compile-time error

  • D

    0

Explanation

Reassigning the parameter c inside replace only changes the local variable; the original reference is unchanged.

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

Related questions