ProgrammingVery hardTCS NQT Prime · Infosys SP
What is printed?
What is printed?
static void f(Integer i) { System.out.print("I"); }
static void f(long l) { System.out.print("L"); }
static void f(Object o) { System.out.print("O"); }
static void f(int... a) { System.out.print("V"); }
// main:
f(5); f(Integer.valueOf(5)); f((short) 5); f(5, 6);- A. LILV
- B. IIIV
- C. OIOV
- D. LIOV
Correct answer: A. LILV
Phase 1 (no boxing) picks f(long) for int and short; Integer matches f(Integer) as most specific over f(Object); two ints can only match the varargs method.
More OOP with C++ and Java questions
- What is printed? java Integer a = 127, b = 127, c = 128, d = 128; System.out.print((a == b) + " " + (c == d) + " " + c.equals(d));
- What happens when this runs? java Integer n = null; boolean flag = false; Integer r = flag ? 1 : n; System.out.print(r);
- What is printed? java byte b = 10; b += 120; System.out.print(b + " " + (b >>> 1));
- What is printed? java try { try { throw new Exception("1"); } finally { throw new RuntimeException("2"); } } catch (Exception e) {…
- What is printed? java class R implements AutoCloseable { String n; R(String n) { this.n = n; System.out.print("o" + n); } public void…
- What is printed? java class A {} class B extends A {} static void f(A a) { System.out.print("A"); } static void f(B b) {…