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 country is going to host Club World Cup in February 2023?
Which component of India's outward FDI rose significantly in May 2025 compared to May 2024?
Fitch Ratings revised India's GDP growth forecast for the fiscal year 2023-24 to:
Which of the following cube in the answer figure can be made based on the unfolded cube in the question figure?
Pro-active Divyang Pension Service ,Tau se Poochho’ WhatsApp chatbot and online booking of government rest houses are the three online services launch...
Which city has the highest number of companies in the 2025 GROHE-HURUN India Real Estate 150 list?
Who from India has been nominated for the 2023 men's World Athlete of the Year award, as announced alongside 10 other nominees?
The Reserve Bank of India has approved _______ of surplus transfer to the government for 2022-23 and has maintained the contingency risk buffer 50 basis...
- DPS Flamingo Lake, recently declared a conservation reserve, is located in which Indian city?
Which organization has recently launched 'Jaldost Airboat'?