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]);- A. ASE 1 2.5
- B. ASE 1 null
- C. 1 2.5
- 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
- What is printed? java String a = "ab"; String b = "a"; String c = b + "b"; final String d = "a"; String e = d + "b"; System.out.print((a…
- What is printed? java interface A { default String m() { return "A"; } } interface B extends A { default String m() { return "B"; } }…
- What is printed? java class P { String n = "P"; static String s() { return "Ps"; } String t() { return "Pt"; } } class C extends P {…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct M { const char n; M(const char s) : n(s) { } ~M() { cout <<…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct P { P(const char s) { cout << s; } }; struct Q { P y{"y"}…
- What is printed? java String s1 = "hi"; String s2 = new String("hi"); System.out.print((s1 == s2) + " " + s1.equals(s2));