ProgrammingVery hardGATE CS · TCS Digital · Advanced

What is printed?

What is printed?
Number[] n = new Integer[2];
n[0] = 1;
try { n[1] = 2.5; }
catch (ArrayStoreException e) { System.out.print("ASE "); }
n[1] = null;
System.out.print(n[0] + " " + n[1]);
  1. A. ASE 1 2.5
  2. B. ASE 1 null
  3. C. 1 2.5
  4. D. ASE null null

Correct answer: B. ASE 1 null

Arrays are covariant and checked at run time: the runtime type is Integer[], so storing a Double throws ArrayStoreException (the compiler accepts it); storing null is always allowed.

More OOP with C++ and Java questions

Exams that ask this topic