Java OCA OCP Practice Question 632

Question

What will be the output of the following code snippet?

public class Main {
   public static void main(String[] args) {
      int a = 1;
      int[] myArray = new int[10];
      int b = myArray[a];
      int c = b + a;
      System.out.println(b = c);
   }
}

Select 1 option

  • A. 0
  • B. 1
  • C. 2
  • D. true
  • E. false


Correct Option is  : B

Note

All the elements of an array of primitives are automatically initialized by default values, which is 0 for numeric types and false for boolean.

Therefore, myArray[1] is 0.

= is not same as ==.

The statement b = c assigns c (whose value is 1) to b. which is then printed.




PreviousNext

Related