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 ratio of the boat's speed in still water to the speed of the stream is 10:2. The boat takes time in the ratio 11:15 to travel...
The downstream speed of a boat is 48 km/hr, while its upstream speed is 32 km/hr. The boat takes 10 hours to travel a distance of (p + 40) km downstream...
A boat, whose speed in still water is 25% more than the speed of the current, covers 810 km downstream in _____ hours. The speed of the boat in still w...
A boat goes 65 km in upstream in 5 hours and takes 4 hours to complete a distance of 20 km in downstream. Find the speed of boat in still water.
The speed of current is 29 km/hr. If the speed of the boat in still water had been 17 km/hr more than the original speed then it would take 6.2 hours to...
If a man rows at the rate of 26 kmph in still water and his rate against the current is 3 kmph, then the man’s rate along the current is: Â
...Ratio of speed of boat in downstream and speed of stream is 5:2, if speed of current is 4 km/hr, then find distance travelled (in km) upstream in 6 hours.
A boat moves downstream at 45 km/h, which is (25/10) m/s faster than its upstream speed. If it covers (D + 24) km downstream and (D - 36) km upstream in...
The speed of current is 40 km/hr. If the speed of the boat in still water had been 10 km/hr more than the original speed then it would take 5.5 hours to...
A boat can cover 188 km in downstream in 4 hours and 140 km in upstream in 7 hours. Find the speed of boat in still water.