ProgrammingHardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
class P { void hi() { } }
class C extends P {
    void a()        { super.hi(); }      // L1
    static void b() { super.hi(); }      // L2
    static void c() { new P().hi(); }    // L3
}
  1. A. L1 and L2
  2. B. L3 only
  3. C. L2 only
  4. D. None

Correct answer: C. L2 only

super, like this, refers to the current object, and a static method has no current object. Calling an instance method on an explicitly created object is fine.

More OOP with C++ and Java questions

Exams that ask this topic