ProgrammingHardHCL · Tech Mahindra pattern
What happens when this Java code is compiled?
What happens when this Java code is compiled?
class P { P(int x) { } }
class C extends P { C() { } }- A. Compilation error: C() implicitly calls super(), and P has no no-argument constructor
- B. It compiles; Java adds P() automatically
- C. It compiles but throws an error when a C is created
- D. It compiles because C's constructor is empty
Correct answer: A. Compilation error: C() implicitly calls super(), and P has no no-argument constructor
Every constructor starts with super(...) and the compiler inserts super() if you write nothing. Since P declares a constructor, no default P() exists, so C() must call super(someInt) explicitly.
More OOP with C++ and Java questions
- What is printed? java // static methods of the main class: static void f(int... a) { System.out.print("V"); } static void f(int a, int b)…
- Which member function fails to compile? cpp class C { int a = 0; mutable int hits = 0; public: int get() const { hits++; return a; } void…
- Which C++ operator can be overloaded only as a non-static member function, never as a free function?
- What is printed? java List<Integer> l = Arrays.asList(1, 2, 3); try { l.set(0, 9); System.out.print(l); l.add(4); } catch…
- What is printed? java Map<String, Integer> m = new HashMap<>(); System.out.print(m.put("a", 1)); System.out.print(" " + m.put("a", 2))…
- What does sizeof print for an empty class in C++? cpp class Empty { }; int main() { cout << sizeof(Empty); }