Java OCA OCP Practice Question 759

Question

What will be the result of attempting to compile and run the following code?

public class Main{ 
   public static void main (String args [] ){ 
      Main obj = new Main (5); 
    } // w w w.  ja va  2  s. c o  m
   int m; 
   static int i1 = 5; 
   static int i2 ; 
   int  j = 100; 
   int x; 
   public Main (int m){ 
      System.out.println (i1 + "  " + i2 + "   " + x + "  " + j + "  " + m); 
   } 
   { j = 30; i2 = 40;  }  // Instance Initializer 
   static  { i1++;  }      // Static Initializer 
} 

Select 1 option

  • A. The code will fail to compile, since the instance initializer tries to assign a value to a static member.
  • B. The code will fail to compile, since the member variable x will be uninitialized when it is used.
  • C. The code will compile without error and will print 6, 40, 0, 30, 5 when run.
  • D. The code will compile without error and will print 5, 0, 0, 100, 5 when run.
  • E. The code will compile without error and will print 5, 40, 0, 30, 0 when run.


Correct Option is  : C

Note

The value 5 is passed to the constructor to the local (automatic) variable m.

So the instance variable m is shadowed.




PreviousNext

Related