ProgrammingHardProduct-company placement · Java
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"); }
static void f(Integer i) { System.out.print("Integer"); }
// in main:
f(null);- A. Object
- B. Compile-time error: the call is ambiguous
- C. String
- D. A NullPointerException is thrown
Correct answer: B. Compile-time error: the call is ambiguous
null fits all three; the most specific method is chosen, but String and Integer are unrelated so neither is more specific than the other. With only f(Object) and f(String), String would win.
More OOP with C++ and Java questions
- What is printed? java // static method of the main class: static void f(Object... a) { System.out.print(a == null ? "N " : a.length + "…
- What is printed? java interface Shape { double area(); } class Sq implements Shape { double area() { return 4; } } // in main: Shape s =…
- Which declaration is LEGAL inside an abstract Java class?
- Which line fails to compile? java class Outer { int x = 1; class In { int get() { return x; } } static class SN { int get() { return 2; }…
- What is printed? java class Outer { int x = 1; class Inner { int x = 2; void show(int x) { System.out.print(x + " " + this.x + " " +…
- What is printed? java int count = 0; Runnable r = () -> System.out.print(count); count++; r.run();