Writing Methods

AP Computer Science A· difficulty 3/5

public class T {
    private int v;
    public T(int x) { setV(x); }
    public void setV(int x) { v = x * 2; }
    public int getV() { return v; }
}
// ...
T t = new T(7);
System.out.println(t.getV());

What is printed?

  • A

    Compile-time error

  • B

    7

  • C

    0

  • D

    14

    check_circle

Explanation

The constructor calls setV(7) which sets v = 14.

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

Related questions