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.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты