The BASE for JAVA LEARNING

by PRO FE S S OR


QUIZ for SECTION IX

To find an answer, click on the desired question.


QUESTION 1:

What is a Constructor useful for ?

QUESTION 2:

In the following program, a Quiz class is instantiated and an Object 'test' is created. There is an 'int' Instance Variable 'numberQuestions' that can be initialized to any integer number at the time of instantiation.

Write the Constructor for this class that initializes that 'test' Object to a given 'numberQuestions' when created.
class Quiz
{ int numberQuestions ; // Instance variable representing number of Questions
public static void main(String[] args)
{ Quiz test = new Quiz(5); // Object 'test' initialized to numberQuestions = 5 }

///// The Constructor is placed here. //////
}

QUESTION 3:

In above Question 2, after you write the parameter Constructor requested, can you instantiate an Object using the default Constructor (no arguments passed), that is "Quiz exam = new Quiz(); " ?

What would you need to add in your code in order to do that ?

QUESTION 4:

CONSTRUCTOR OVERLOADING

Examine the class below:
class Student
{ int age;
String name;

// Constructors are placed here.
}

How many Constructors would you need in order to create different Object of Student class, each Object initialized to the following variables:

  • age
  • name
  • age and name (in that order)
  • aame and age (in that order)

Write those Constructor for each case above.
























ANSWER to QUESTION 1:

Constructors are used to initialize Objectw at creation.

Also Constructors are helpful for interaction among Objects.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 2:

Quiz(int tmpNumber)
{ numberQuestions = tmpNumber; }

// A value is passed as an argument in the Constructor: 'tmpNumber'
// Then the Instance variable 'numberQuestions' is set to that value.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 3:

You would need to write the default Constructor:
Quiz()
{

}


If the default Constructor is not explicitly written, a compiler error would occur. In this case, as we have already a parameter Constructor, the default Constructor is not automatically implemented. It would have to be explicitly written.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 4:

We would need 4 Constructors.

// Constructor for the 'age' Variable:
Student(int tmpAge)
{ age = tmpAge; // age assigned }


// Constructor for the 'name' Variable:
Student(String tmpName)
{ name = tmpName; // name assigned }


// Constructor for the 'age' and 'name' Variables (in that order):
Student(int tmpAge, String tmpName)
{ age = tmpAge; // age assigned
name = tmpName; // name assigned
}


// Constructor for the 'name' and 'age' Variables (in that order):
Student(String tmpName, int tmpAge)
{ age = tmpAge; // age assigned
name = tmpName; // name assigned
}



Click your Back Button on your Browser to go back to the QUIZ Section.





Click here to go to the JAVA Mini-Course page