ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
// static methods of the main class:
static void f(long x) { System.out.print("L"); }
static void f(Integer x) { System.out.print("I"); }
static void f(int... x) { System.out.print("V"); }
static void g(Long x) { System.out.print("G"); }
static void g(Object x) { System.out.print("O"); }
// in main:
f(5);
g(5);- A. IG
- B. LG
- C. LO
- D. IO
Correct answer: C. LO
Phase 1 of overload resolution allows widening but not boxing, so f(long) wins over f(Integer). For g, int cannot widen to Long and boxing gives Integer, which is not a Long; Integer is an Object, so g(Object) is chosen.
More OOP with C++ and Java questions
- What is printed? java String x = "a"; final String y = "a"; System.out.print(("ab" == x + "b") + " "); System.out.print(("ab" == y + "b")…
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static…
- What is printed? java class A { static int x = f(); static int y = 10; static int f() { return y 2; } } // in main: System.out.print(A.x +…
- What is printed? java class P { P() { show(); } void show() { System.out.print("P"); } } class C extends P { int v = 7; String s = "x"…
- Which statement about this code is correct? java try { new java.io.FileReader("a.txt").close(); } catch (java.io.IOException | Exception…
- What is printed? java class A { void m(A a) { System.out.print("AA"); } } class B extends A { void m(A a) { System.out.print("BA"); } void…