ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
// static methods of the main class:
static void f(long x) { System.out.print("long"); }
static void f(Integer x) { System.out.print("Integer"); }
static void f(Object x) { System.out.print("Object"); }
static void f(int... x) { System.out.print("varargs"); }
// in main:
f(5);- A. long
- B. varargs
- C. Integer
- D. Object
Correct answer: A. long
Java resolves overloads in phases: first by widening only (int to long), then with boxing, then with varargs. f(long) is found in phase one, so boxing to Integer or Object is never considered.
More OOP with C++ and Java questions
- What is printed? java class P { P() { show(); } void show() { System.out.print("P "); } } class C extends P { int x = 10; void show() {…
- What is printed? java class T { static { System.out.print("S"); } { System.out.print("I"); } T() { System.out.print("C"); } } // in main…
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static int…
- What is printed? java System.out.print(1 + 2 + "3" + 4 + 5);
- What is printed? java // static methods of the main class: static void change(StringBuilder sb) { sb.append("X"); sb = new…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3)); l.remove(1); System.out.print(l + " ")…