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.
A seller marked an article 25% above its cost price and sold it after giving a discount of Rs. 60. If the seller earned a profit of 20% in the transacti...
The combined cost price of two products, P and Q, is Rs. 5000. Product P is marked up by 25% on its cost and Product Q is marked up by 40%. After giving...
A seller sold some pens at a 24% discount, resulting in a loss of 5%. If he had sold the pens for Rs. 30 more, he would have made...
An article was marked 25% above the cost price and sold after a discount of Rs. 200. If the selling price of the article is Rs. 1300, then find the cost...
‘A’ and ‘B’ started a business by investing certain sum in the ratio 13:5, respectively for 6 years. If 10% of the total profit is donated in an...
A smartphone was sold for Rs. 36,300 at a profit of 10%. If the smartphone was instead sold at a loss of 8%, what would have been its selling price?
What will be the percentage profit after selling an article at a certain price if there is a loss of 30% if the article is sold at 1/4th of t...
A shopkeeper offers a 10% discount on an article and still manages to earn a 12% profit. If the marked price of the article is Rs. 2800, calculate the c...
An item is tagged 20% over its cost price and sold after offering a 10% discount. What is the profit or loss percentage?
A shopkeeper purchased 35 kg of sugar at Rs. 22 per kg. He sold some of it at a profit of 8% and the rest at a profit of 16%. If the total profit he mad...