ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
class Picker {
static void f(int a, int b) { System.out.print("fixed"); }
static void f(int... a) { System.out.print("varargs"); }
public static void main(String[] args) { f(1, 2); }
}- A. varargs
- B. fixed
- C. Compile-time error: call to f(1, 2) is ambiguous
- D. Both methods execute
Correct answer: B. fixed
Java's overload resolution tries fixed-arity (non-varargs) applicable methods before considering varargs methods. f(int, int) applies exactly, so it is chosen; the varargs method is only used as a fallback when no fixed-arity overload matches.
More OOP with C++ and Java questions
- What happens when this is compiled? cpp struct Base final { virtual void f() { cout << "Base"; } }; struct Derived : Base { };
- What is printed? cpp struct Base {}; struct Derived : Base {}; void f(Base b) { cout << "Base"; } void f(Derived d) { cout << "Derived"; }…
- What is printed? cpp struct Shape { virtual double area() const = 0; virtual ~Shape() = default; }; struct Square : Shape { double s…
- What is printed? java abstract class Shape { abstract double area(); public String toString() { return "Shape(area=" + area() + ")"; } }…
- What is printed? java class Vehicle { void start() { System.out.print("Vehicle-start "); honk(); } void honk() { System.out.print("beep")…
- What is printed? cpp struct Base { virtual void f() const { cout << "Base "; } }; struct Derived : Base { void f() const override { cout…