ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE
java void f(int x) { System.out.print("int"); } void f(Integer x) { System.out.print("Integer"); } void f(Object... x) { System.out.print("varargs"); } //…
void f(int x) { System.out.print("int"); }
void f(Integer x) { System.out.print("Integer"); }
void f(Object... x) { System.out.print("varargs"); }
// main: f(5);What is printed?- A. int
- B. Compile error: ambiguous
- C. varargs
- D. Integer
Correct answer: A. int
Java's overload resolution first looks for a match using only widening (no boxing, no varargs); f(int) matches 5 directly with no boxing needed, so it is chosen ahead of the autoboxed f(Integer) and the varargs f(Object...).
More OOP with C++ and Java questions
- Why does adding a brand-new virtual function to a base class not increase the size of each object in that hierarchy (assuming objects…
- Which java.util.function functional interface best fits a lambda that takes one argument of type T and returns a boolean?
- A base class method is declared final in Java. What does this guarantee about calls to it through a base-class reference, regardless of…
- cpp struct S { int a, b; S(int x) : b(x), a(b) { } }; int main() { S s(5); cout << s.a << " " << s.b; } Members a and b are declared in…
- What does List<? super Integer> allow, in contrast to List<? extends Integer>?
- java try { throw new ArithmeticException("x"); } catch (RuntimeException e) { System.out.print("R"); } catch (ArithmeticException e) {…