Static Variables and Methods

AP Computer Science A· difficulty 2/5

public class Util {
    public static int base = 100;
    public static int boost(int x) {
        return base + x;
    }
}
// ...
System.out.println(Util.boost(5));
Util.base = 0;
System.out.println(Util.boost(5));

What is printed?

  • A

    105 then 5

    check_circle
  • B

    5 then 5

  • C

    Compile-time error

  • D

    105 then 105

Explanation

First call uses base=100 returning 105; after setting base to 0, the second call returns 5.

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

Related questions