Question
What will be the output of the following code snippet
demonstrating composition in Java? class Engine { void start() { System.out.println("Engine started."); }} class Car { private Engine engine; Car() { engine = new Engine(); // Composition } void start() { engine.start(); }} public class Test { public static void main(String[] args) { Car car = new Car(); car.start(); }}Solution
In the provided Java code, the Car class has a composition relationship with the Engine class, meaning that an Engine instance is created within the Car class. When the start method of the Car class is invoked, it calls the start method of the Engine instance, which outputs "Engine started." Therefore, the output of the code is Engine started. Why Other Options Are Wrong: B) Car started: This option is incorrect as there is no start method in the Car class that prints Car started; it delegates to the Engine. C) No output: This option is incorrect because the code clearly produces an output when the start method is called. D) Compilation error: This option is incorrect as the code is syntactically correct and will compile successfully. E) Runtime error: This option is incorrect because there are no conditions in the code that would lead to a runtime error; it executes as intended.
- Which international bank released the report titled “World Economic Outlook: Securing Low Inflation and Strong Growth” in March 2025?
Incentives paid by the government to banks for promoting RuPay debit cards and low-value BHIM-UPI transactions will not attract _______.
How much did the India Maritime Week 2025 event in Mumbai expect to attract in total investment value?
How much stake has the RBI approved Sumitomo Mitsui Banking Corporation (SMBC) to acquire in Yes Bank?
Who has been appointed as the Chairperson of the 8th Central Pay Commission (CPC)?
Which organization signed an MoU with the Department of Posts for physical verification of PMEGP units?
How much investment has NTPC Green Energy Ltd (NGEL) committed for renewable energy development in Andhra Pradesh?
Read the statement and complete the blanks:
"Air India has expanded its international connectivity by entering a codeshare agreement with All N...
Which entities have joined forces to promote start-up collaboration on recycling of E-Vehicles batteries under the Trade and Technology Council?
- Which Indian city will host the Asian Youth and Junior Weightlifting Championships 2025?