What will the following code print out to console?

public class Test {
    private Integer k;
    private int z;
    private int i;

    public void method() {
        i = k + z;                 // 1
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.method();
        System.out.println(t.i); // 2
    }
}
Explanation
i and z variables will be initialised as 0 since they are of primitive type. k, in turn, is an object and will, therefore, be initialised as null. Operations with null will result in a NullPointerException runtime error.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes