ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
// static methods of the main class:
static void f(Object o) { System.out.print("Object"); }
static void f(String s) { System.out.print("String"); }
// in main:
f(null);- A. NullPointerException at run time
- B. Compilation error: ambiguous call
- C. Object
- D. String
Correct answer: D. String
null fits both parameter types, and Java picks the most specific applicable overload. String is a subclass of Object, so f(String) wins. Adding an unrelated overload such as f(Integer) would make the call ambiguous.
More OOP with C++ and Java questions
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("long"); } static void f(Integer x) {…
- 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…