ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
// static methods of the main class:
static void f(int... a) { System.out.print("V"); }
static void f(Integer a, Integer b) { System.out.print("II"); }
// in main:
f(1, 2);
f(1, 2, 3);- A. VV
- B. IIII
- C. IIV
- D. Compile-time error: ambiguous call f(1, 2)
Correct answer: C. IIV
Overload resolution tries boxing (phase 2) before varargs (phase 3), so f(1, 2) picks f(Integer, Integer). Three arguments fit only the varargs method.
More OOP with C++ and Java questions
- What is printed? java // static methods of the main class: static void f(double d) { System.out.print("D"); } static void f(Object o) {…
- Which line fails to compile? java // static methods of the main class: static void g(long l) { } static void h(Long l) { } // in main…
- Which lines fail to compile? java String s = "x"; Number n = 1; Runnable r = () -> { }; Object o = s; boolean a = s instanceof Integer; //…
- What is printed? java // Java 16 or later record R(int[] a) { } record S(List<Integer> l) { } // in main: System.out.print(new R(new…
- What is printed? java enum Op { ADD { int apply(int a, int b) { return a + b; } }, MUL { int apply(int a, int b) { return a b; } }…
- What is printed? java enum Day { MON, TUE, WED } // in main: Map<Day, Integer> m = new EnumMap<>(Day.class); m.put(Day.WED, 3)…