ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
// static methods of the main class:
static void f(double d) { System.out.print("D"); }
static void f(Object o) { System.out.print("O"); }
// in main:
Integer x = 5;
f(5);
f(x);- A. DO
- B. DD
- C. OO
- D. OD
Correct answer: A. DO
Phase 1 allows only widening and subtyping, no boxing or unboxing. int 5 widens to double. Integer x is already an Object (subtype), while reaching double would need unboxing, so f(Object) wins.
More OOP with C++ and Java questions
- 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)…
- Which line fails to compile? java List<String>[] a = new List<String>[2]; // L1 List<?>[] b = new List<?>[2]; // L2 List<String>[] c = new…