Java OCA OCP Practice Question 1361

Question

What will be the output when the following class is compiled and run?

public class Main{ 
   static int x = 5; 
   public static void main (String [] args){ 
      int x  =  ( x=3 ) * 4;  // 1 
      System .out.println (x); 
   } 
} 

Select 1 option

  • A. It will not compile because line // 1 cannot be parsed correctly.
  • B. It will not compile because x is used before initialization.
  • C. It will not compile because there is an ambiguous reference to x.
  • D. It will print 12.
  • E. It will print 3 .


Correct Option is  : D

Note

x is first initialized by x = 3, then the value of this expression (i.e. "x = 3"), which is 3, is multiplied by 4 and is again assigned to x. So it prints 12.




PreviousNext

Related