Java OCA OCP Practice Question 1546

Question

What will be the result of trying to compile and execute of the following program?

public class Main{
   public static void main (String args [] ){
      int i = 0 ;
      int [] myArray =  {10, 20} ;
      myArray [i] = i = 30 ;
      System.out.println (""+ myArray [ 0 ] + " " + myArray [ 1 ] + "  "+i) ;
   }
}

Select 1 option

  • A. It will throw ArrayIndexOutofBoundsException at Runtime.
  • B. Compile time Error.
  • C. It will prints 10 20 30
  • D. It will prints 30 20 30
  • E. It will prints 0 20 30


Correct Option is  : D

Note

The statement myArray[i] = i = 30 ; will be processed as follows:

   myArray[i] = i = 30;
=> myArray[0] = i = 30 ;
=> i = 30; 
   myArray[0] = i ;
=> myArray[0] = 30 ;

First, the dimension expressions are evaluated, left-to-right.

If any of the expression evaluations completes abruptly, the expressions to the right of it are not evaluated.




PreviousNext

Related