What is the result?

Given:

public class Test {

Integer x; // line 2

public static void main(String[] args) {

new Test().go(5);

}

void go(Integer i) { // line 6

System.out.print(x + ++i); // line 7

}

}

What is the result?

Given:

public class Test {

Integer x; // line 2

public static void main(String[] args) {

new Test().go(5);

}

void go(Integer i) { // line 6

System.out.print(x + ++i); // line 7

}

}

What is the result?

A.
5

B.
6

C.
An exception is thrown at runtime

D.
Compilation fails due to an error on line 6

E.
Compilation fails due to an error on line 7

Explanation:
The code compile finem but ajava.lang.NullPointerExceptionis thrown at runtime. X has no value. The code would run if line 2 was changed to:
Integer x = 3;



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Peter

Peter

C, NullPointerException in line 7

Rudi

Rudi

Correct !

Answer C

NullPointerException on line 7