Sunday, August 6, 2023

Complete the code segment to call default constructor first and then any other constructor in the class.


 

Complete the code segment to call default constructor first and then any other constructor in the class.


Program :-


// This is the main class Question

public class Question214{

public static void main(String[] args){

Answer a = new Answer(10,"MCQ");

}

}

class Answer{

// This is the default constructor of the class Answer

Answer(){

System.out.println("You got nothing.");

}

// This is a parameterized constructor of the class Answer

Answer(int marks, String type){

//The 'this()' referene variable is able to call the default constructor of the class.

this();

//Print marks and type of the question

System.out.print("You got "+marks+" for an "+ type);

}

}



InputOutput
Test Case 1
You got nothing.
You got 10 for an MCQ

No comments:

Post a Comment