Object-Oriented Programming · Question 11 · Medium

What is printed when new C() is executed?

class P {
    P() { System.out.print("P "); }
}
class C extends P {
    C() { System.out.print("C "); }
}
  1. C P
  2. C
  3. P
  4. P C
Answer and explanation

Answer: D. P C

Explanation: A subclass constructor first calls the superclass constructor (an implicit super()). P's constructor therefore prints first, followed by C's.

Previous: Which pair of method declarations CANNOT both appear in the same Java class? · Next: What is the output of the following Java code?

Practise object-oriented programming

Try a fresh set of object-oriented programming questions with an explanation after every answer, free and without signing up: Object-Oriented Programming practice. Or sit a full placement mock test.

More object-oriented programming questions

More technical & programming practice

DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics · Pseudo-code

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge