OCA Java SE 8 Mock Exam - OCA Mock Question 40








Question

What is the output of the following code:

         class Course { 
             String courseName; 
             Course() { 
                 Course c = new Course(); 
                 c.courseName = "Oracle"; 
             } 
         } 
         class Main { 
             public static void main(String args[]) { 
                 Course c = new Course(); 
                 c.courseName = "Java"; 
                 System.out.println(c.courseName); 
             } 
         } 
  1. The code will print Java.
  2. The code will print Oracle.
  3. The code will not compile.
  4. The code will throw an exception or an error at runtime.




Answer



D

Note

This class will throw StackOverflowError at runtime.

The constructor of the class Course creates an object of the class Course, which will call the constructor again.