Java OCA OCP Practice Question 2694

Question

Consider the following program and predict the output:

class Main {//  ww  w.  j  av  a2 s.co  m
       Integer I;
       int i;
       public Main(int i) {
               this.i = I + i;
               System.out.println(this.i);
       }
       public static void main(String args[]) {
               Integer I = new Integer(1);
               Main test = new Main(I);
       }
}
  • a) The program will print the following: 2.
  • b) The program will print the following: 1.
  • c) The program will report a compiler error.
  • d) The program will report a runtime exception.


d)

Note

The member variable I is not initialized and accessed in the constructor, which results in a NullPointerException.

The main() method declares a local variable named I whose scope is limited to the main() method.




PreviousNext

Related