ProgrammingHardProduct-company placement · Java
Which line fails to compile?
Which line fails to compile?
class P { Number get() { return 1; } int size() { return 0; } }
class C extends P {
Integer get() { return 2; } // L1
long size() { return 0; } // L2
}- A. L2 only
- B. L1 only
- C. Both L1 and L2
- D. Neither
Correct answer: A. L2 only
Covariant returns allow a subtype of a reference return type (Integer for Number). For primitive returns the type must match exactly, so long size() clashes with int size().
More OOP with C++ and Java questions
- What is printed? java class T { int n; { n += 5; } T() { this(1); n = 2; } T(int k) { n += k; } } // in main: System.out.print(new T().n);
- What is printed? java class A { public String toString() { return "A"; } } class B extends A { public String toString() { return "B" +…
- What happens when Z is compiled? java interface X { int V = 1; } interface Y { int V = 2; } class Z implements X, Y { int get() { return…
- What happens when this is compiled? java interface A { default void hello() { System.out.print("A"); } } interface B extends A { void…
- How many of these statements about Java exceptions are correct? (i) An overriding method may throw any unchecked exception, even if the…
- What happens when this code is compiled? java try { new java.io.FileReader("x.txt"); } catch (java.io.IOException | Exception e) {…