Questions on Constructors
1. What is constructer? When is it invoked? Or Write two characteristics of a
constructor. Give example.
Ans.
Constructor is a special method(or function) defined with the same name as of the class.
It has no return type, not even void.
It is automatically invoked wherever an object of that class is created.
// Example: A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass() {
x = 0;
}
}
2. What is the difference between Constructor and method/function?
Ans.
Constructor Method/function
1. It is always defined by the same name as 1. It is defined by any name except class
of the class name and keyword.
2. It has no return type not even void data 2. It must begin with either void or a valid
type return data type (such as int, float, char
etc.).
3. It initializes the data members of the 3. A method contains one or more than one
class. valid Java statements as its body.
4. It can not be invoked/called like a 4. A method must be invoked or called
method because a constructor either by using object of the class or
automatically runs whenever an object directly.
of the class is created.
3. What are different types of constructors?
Ans. There are 3 different types of constructors
Default Constructor
Parameterized Constructor
Copy Constructor
4. What is Java default constructor?
Ans. There are 2 types of Default constructor one provided by Java itself and one implemented
by the programmer.
1
If a class does not have any constructor, the Java compiler will automatically provide one
non-parameterized-constructor (or default-constructor) for the class in the byte code.
The default constructor initializes the member variables to 0.
The access modifier of the default constructor is the same as the class itself.
A programmer can give his own implementation of default constructor and in that case it
overrides they system provided constructor.
5. What is the difference between default and parameterized constructor?
Ans.
Default Constructor Parameterized Constructor
It does not have any parameters It has one or more parameters
It is automatically generated if programmer It has to be written by programmer
does not write a constructor
If no constructor is written then default Default constructor is not available if
constructor is called parameterized constructor is written
6. What is the difference between parameterized and copy constructor?
Ans.
Parameterized constructor: When a constructor takes in parameters within the parenthesis it is
known as parameterized constructor.
Copy constructor: Copy Constructor is a special type of constructor that takes same class as
argument. Copy constructor is used to provide a copy of the specified object.
Example:
public class simply
{
int a;
char c;
simply( ){ // Default Constructor
a= 10;
}
simply(char n){ // Parameterized Constructor
c=n;
}
simply(simply s) // Copy Constructor
{
2
int a= s.a;
}
}
7. Explain the concept of constructor overloading with an example .
Ans. Presence of two or more constructors in a class is known as constructor overloading. To
overload two constructors, they must differ either in number or type of parameters.
Example:
class stud
{
static int year;
int Roll_No;
stud( ){ // constructor 1. This is default or non-parameterized constructor
year= 2019;
}
stud( int n){ // constructor 2. This is a parameterized constructor
Roll-No= n;
}
}
8. What is this keyword in Java?
Ans. this keyword in Java is a special keyword which can be used to represent current object or
instance of any class in Java.It stores the address of current calling object. It is also known as
reference or pointer to current object.
Any instance method can be called as this.method() and any instance variable var as this.var.
public class Actor
{
string lastName;
public Actor(String lastName)
{
this.lastName = lastName;
}
}
9. What is parameterized constructor with example?
Ans. When a constructor takes in parameters within the parenthesis it is known as parameterized
constructor.
Example:
public class simply
{
3
int roll_no;
char grade;
simply( ){ // Default Constructor
roll_no= 10;
}
simply(char n){ // Parameterized Constructor
grade =n;
}
}
10. What are temporary instance of a class and how it is created? Give one example also.
Ans.
It is an anonymous instance that is created to perform only one particular job within a
single statement.
Temporary instances are created by calling or invoking constructor of a class using
explicit method within the statement which performs some specific job.
Example:
new Obj().Show();
11. Fill in the blanks
a. A member function having the same name as that of the class name is called ____.
b. A constructor has ____ return type.
c. A constructor is used when ______ is created
d. A constructor without any argument is known as ___________
e. ____________ constructor creates objects by passing value to it.
f. The ______ keyword refers to the current object.
g. The argument of a copy constructor is always passed by __________.
h. The constructor generated by the compiler is known as ___________.
Ans.
a. A member function having the same name as that of the class name is
called constructor.
b. A constructor has return no type.
c. A constructor is used when an object is created
d. A constructor without any argument is known as non-parameterised constructor.
e. Parameterised constructor creates objects by passing value to it.
f. The this keyword refers to the current object.
g. The argument of a copy constructor is always passed by reference.
h. The constructor generated by the compiler is known as default constructor.
4
12. State True or False
a. The compiler supplies a special constructor in a class that does not have any constructor.
b. A constructor is not defined with any return type.
c. Every class must have all types of constructors.
d. A constructor is a member function of a class.
e. Constructor is used to initialize the data members of a class.
f. A constructor may have different name than the class name.
g. A constructor is likely to be defined after the class declaration.
h. Copy constructor copies functions from one object to another.
Ans.
a. True
b. True
c. False
d. True
e. True
f. False
g. False
h. False