ProgrammingVery hardProduct-company placement · Java
Which lines fail to compile?
Which lines fail to compile?
class P { void f() { } static void g() { } }
class C extends P {
static void f() { } // L1
void g() { } // L2
}- A. L1 only
- B. L1 and L2
- C. L2 only
- D. Neither: L1 hides f and L2 overrides g
Correct answer: B. L1 and L2
A static method cannot hide an instance method (L1) and an instance method cannot override a static one (L2); both are compile-time errors.
More OOP with C++ and Java questions
- Which line fails to compile? java class P { Number get() { return 1; } int size() { return 0; } } class C extends P { Integer get() {…
- 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…