VOOZH about

URL: https://www.geeksforgeeks.org/java/output-of-java-program-set-4/

⇱ Output of Java Program | Set 4 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Output of Java Program | Set 4

Last Updated : 23 Jul, 2025

Predict the output of the following Java Programs.

1. What is the output of the following program?

Options:

a) Program compiles and prints nothing
b) Program compiles and runs successfully
c) Compiler Error
d) Runtime Exception

Answer: (c) Compiler Error

Explanation: foo() is protected in Base and default in Derived. Default access is more restrictive. When a derived class overrides a base class function, more restrictive access can’t be given to the overridden function. If we make foo() public, then the program works fine without any error. The behavior in C++ is different. C++ allows to give more restrictive access to derived class methods.


2. What is the output of the following program?

Options:

a) (0.0 + 0.0i)
b) Compilation successful, prints default values
c) Runtime Error
d) Compiler Error in line “Complex c1 = new Complex();”

Answer: (d) Compiler Error in line “Complex c1 = new Complex();”

Explanation: In Java, if we write our own copy constructor or parameterized constructor, then compiler doesn’t create the default constructor. This behavior is same as C++.

Comment