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 "); }
}
- C P
- C
- P
- 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
- Which statement about abstract classes and interfaces in Java is correct?
- In C++, how is the "diamond problem" (two copies of a common base class…
- Which pair of method declarations CANNOT both appear in the same Java class?
- What is the output of the following Java code?
- What is the output of the following Java code?
- What is the output of the following Java code?
- Which SOLID principle states that objects of a subclass should be usable…
- A Logger class keeps a list of log entries in a private field and offers only…
- Inside an instance method or constructor in Java, which keyword refers to the…
- Inheritance, as in 'SavingsAccount extends Account', models which kind of…
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