ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
// static methods of the main class:
static void f(Object o) { System.out.print("O"); }
static void f(int... a) { System.out.print("V"); }
static void g(long x) { System.out.print("L"); }
static void g(int... a) { System.out.print("V"); }
// in main:
f(5);
g(5);
g();- A. VLV
- B. OVV
- C. VVV
- D. OLV
Correct answer: D. OLV
Varargs are tried only in the last phase. f(5) boxes to Integer and matches f(Object) in phase 2 (O); g(5) widens to long in phase 1 (L). g() has no fixed-arity candidate, so it uses varargs (V).
More OOP with C++ and Java questions
- What is printed? java char c = 'a'; c += 1; System.out.print(('a' + 1) + "" + (char) ('a' + 1) + c);
- What is printed? java Object o = null; String s = (String) o; System.out.print(s + (o instanceof Object) + (s == null));
- What is printed? java List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi"))…
- Why does this class fail to compile? java class Util { static void f(List<String> l) { } static void f(List<Integer> l) { } }
- What is printed? java record Pt(int x, int y) { } // in main: Set<Pt> s = new HashSet<>(); s.add(new Pt(1, 2)); s.add(new Pt(1, 2))…
- Which line fails to compile? java class Animal { Animal make() { return new Animal(); } } class Dog extends Animal { Dog make() { return…