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.
The European Space Agency (ESA) is launching the JUICE mission, which aims to explore Jupiter's moons. What is the primary objective of the JUICE mission?
Art of cultivation of forest trees is called __________.
Which of the following National Parks is not situated in Uttarakhand?
What is the essential element that is suitable during the synthesis of proteins and other compounds in plants?
Which of the following is not correctly matched?
As of 2020, which country has the largest proven coal reserves in the world?
How much minimum CAR(Capital AdequacyRatio) is required to be maintained by Small Finance Banks?
For what purpose is the PRAN number used, a 12-digit unique identifier assigned to individuals?
World's highest waterfall is the
Which of the following is an Indian microblogging site?