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;
C, NullPointerException in line 7
Correct !
Answer C
NullPointerException on line 7