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
}- A. L1 and L2
- B. L3 only
- C. L2 only
- 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
- What is printed? java Object[] objs = {1, "a", 2.0, 'c'}; for (Object o : objs) System.out.print(o.getClass().getSimpleName());
- What is printed? java class K { static int made = 0; K(int v) { made++; if (v < 0) throw new IllegalArgumentException(); } } // in main…
- What is printed? java interface Named { default String n() { return getClass().getSimpleName(); } } class Dog implements Named { } // in…
- What is printed? java Runnable r = new Runnable() { public void run() { } }; System.out.print("[" + r.getClass().getSimpleName() + "]" +…
- Which line fails to compile? java class P { final void f(int x) { } } class C extends P { void f(long x) { } // L1 void f(int y) { } // L2 }
- What is printed? java class P { int x; P(int x) { this.x = x; } public boolean equals(Object o) { return o != null && getClass() ==…