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); }
}
  1. A. varargs
  2. B. fixed
  3. C. Compile-time error: call to f(1, 2) is ambiguous
  4. 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

Exams that ask this topic