ProgrammingVery hardGATE CS · TCS Digital · Advanced
What is printed?
What is printed?
static void f(long x) { System.out.print("L"); }
static void f(Integer x) { System.out.print("I"); }
static void f(Object x) { System.out.print("O"); }
static void f(int... x) { System.out.print("V"); }
// in main:
f(5);
f(Integer.valueOf(5));
f('a');
f(5.0);- A. OIOO
- B. LILO
- C. LIOO
- D. LIIO
Correct answer: B. LILO
Overload resolution tries subtyping/widening only first: f(5) and f('a') widen to long; the Integer argument matches f(Integer) exactly. For 5.0 no widening works, so the second phase (boxing to Double, then Object) selects f(Object).
More OOP with C++ and Java questions
- What is printed? java System.out.print(true ? 1 : 2.0); System.out.print(true ? 'a' : 0); int i = 0; System.out.print(true ? 'a' : i);
- What is printed? java static int f() { try { throw new RuntimeException(); } finally { return 1; } } static int g() { int x = 1; try {…
- What is printed? java class K { int v; K(int v) { this.v = v; } public int hashCode() { return v; } public boolean equals(Object o) {…
- What is printed? java Number[] n = new Integer[2]; n[0] = 1; try { n[1] = 2.5; } catch (ArrayStoreException e) { System.out.print("ASE ")…
- 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"; } }…