ProgrammingModerateProduct-company placement · Java
What happens when this is compiled?
What happens when this is compiled?
interface I { void a(); void b(); }
abstract class P implements I { public void a() { } }
class C extends P { }- A. It compiles; P already implements I
- B. Compile-time error in P: it must implement b()
- C. Compile-time error: C must implement b()
- D. It compiles; calling b() throws AbstractMethodError
Correct answer: C. Compile-time error: C must implement b()
An abstract class may leave interface methods unimplemented, but the first concrete subclass must implement every remaining abstract method, here b().
More OOP with C++ and Java questions
- Which line fails to compile? cpp struct P { int x = 3; ostream& operator<<(ostream& os) { return os << x; } }; int main() { P p; cout <<…
- What is wrong with this program? cpp struct Buf { int p; Buf() : p(new int[4]) { } ~Buf() { delete[] p; } }; int main() { Buf a; Buf b =…
- What happens when this is compiled? cpp struct A { int v = 0; int& get() const { return v; } };
- A global object g1 defined in file1.cpp uses the global object g2 defined in file2.cpp inside g1's constructor. Both have dynamic…
- What is printed? cpp void f(int) { cout << "I"; } void f(char) { cout << "P"; } int main() { f(0); f(nullptr); }
- Which lines fail to compile? cpp void g(int& x) { } void h(const int& x) { } int main() { int a = 1; g(5); // L1 h(5); // L2 g(a); // L3…