Output of Java Programs | Set 14 (Constructors)
Prerequisite – Java Constructors
1) What is the output of the following program?
a) Compilation error b) 5 c) Runtime error d) None of these
Explanation: A private constructor cannot be used to initialize an object outside the class that it is defined within because it is no longer visible to the external class.
2) What is the output of the following program?
a) GFG Thread’s running b) Thread’s running GFG c) Compilation error d) Runtime error
Explanation: A constructor cannot be enclosed inside a try/catch block.
3) What is the output of the following program?
a) Constructor called Method called b) Compilation error c) Runtime error d) None of the above
Explanation: When a constructor is marked as private, the only way to create a new object of that class from some external class is using a method that creates a new object, as defined above in the program. The method create() is responsible for creation of Temp object from some other external class. Once the object is created, its method can be invoked from the class in which the object is created.
4) What is the output of the following program?
a) 12345 b) Compilation error c) 15 d) Runtime error
Explanation: Constructors can be chained and overloaded. When Test() is called, it creates another Test object calling the constructor Test(int temp).
5) What is the output of the following program?
a) 21 Super Class b) Super Class 21 c) Compilation error d) 12 Super Class
Explanation: Constructor call to super class must be the first statement in the constructor of the Derived class. private
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...