Constructors in Java
Constructors in Java
Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection
of statements(i.e. instructions) that are executed at time of Object creation.
Need of Constructor
Think of a Box. If we talk about a box class then it will have some class variables (say length, breadth, and
height). But when it comes to creating its object(i.e Box will now exist in computer’s memory), then can
a box be there with no value defined for its dimensions. The answer is no.
So constructors are used to assign values to the class variables at the time of object creation, either
explicitly done by the programmer or by Java itself (default constructor).
Constructor(s) of a class must has same name as the class name in which it resides.
Access modifiers can be used in constructor declaration to control its access i.e which other class
can call the constructor.
Types of constructor
There are no “return value” statements in constructor, but constructor returns current class instance. We
can write ‘return’ inside a constructor.
Constructor Overloading
Like methods, we can overload constructors for creating objects in different ways. Compiler
differentiates constructors on the basis of numbers of parameters, types of the parameters and order of
the parameters.
Constructor(s) must have the same name as the class within which it defined while it is not
necessary for the method in java.
Constructor(s) do not return any type while method(s) have the return type or void if does not
return any value.
Constructor is called only once at the time of Object creation while method(s) can be called any
numbers of time.
Answer: d
Explanation: The object that has to be copied to new object must be previously created. The
new object gets initialized with the same values as that of the object mentioned for being
copied. The exact copy is made with values.
Answer: a
Explanation: The copy constructor has the most basic function to initialize the members of an
object with same values as that of some previously created object. The object must be of same
class.
3. If two classes have exactly same data members and member function and only they differ by
class name. Can copy constructor be used to initialize one class object with another class
object?
a) Yes, possible
b) Yes, because the members are same
c) No, not possible
d) No, but possible if constructor is also same
Answer: c
Explanation: The restriction for copy constructor is that it must be used with the object of same
class. Even if the classes are exactly same the constructor won’t be able to access all the
members of another class. Hence we can’t use object of another class for initialization.
Answer: b
Explanation: When an object is passed to a function, actually its copy is made in the function. To
copy the values, copy constructor is used. Hence the object being passed and object being used
in function are different.
Answer: d
Explanation: While returning an object we can use the copy constructor. When we assign the
return value to another object of same class then this copy constructor will be used. And all the
members will be assigned the same values as that of the object being returned.
Answer: a
Explanation: The compiler provides an implicit copy constructor. It is not mandatory to always
create an explicit copy constructor. The values are copied using implicit constructor only.
7. If a class implements some dynamic memory allocations and pointers then _____________
a) Copy constructor must be defined
b) Copy constructor must not be defined
c) Copy constructor can’t be defined
d) Copy constructor will not be used
Answer: a
Explanation: In the case where dynamic memory allocation is used, the copy constructor
definition must be given. The implicit copy constructor is not capable of manipulating the
dynamic memory and pointers. Explicit definition allows to manipulate the data as required.
Answer: c
Explanation: The syntax must contain the class name first, followed by the classname as type
and &object within parenthesis. Then comes the constructor body. The definition can be given
as per requirements.
Answer: a
Explanation: This is mandatory to pass the object by reference. Otherwise the object will try to
create another object to copy its values, in turn a constructor will be called, and this will keep
on calling itself. This will cause the compiler to give out of memory error.
10. Out of memory error is given when the object _____________ to the copy constructor.
a) Is passed with & symbol
b) Is passed by reference
c) Is passed as
d) Is not passed by reference
Answer: d
Explanation: All the options given, directly or indirectly indicate that the object is being passed
by reference. And if object is not passed by reference then the out of memory error is
produced. Due to infinite constructor call of itself.
Answer: c
Explanation: Whenever the compiler creates a temporary object, copy constructor is used to
copy the values from existing object to the temporary object.
12. The deep copy is possible only with the help of __________
a) Implicit copy constructor
b) User defined copy constructor
c) Parameterized constructor
d) Default constructor
Answer: b
Explanation: While using explicit copy constructor, the pointers of copied object point to the
intended memory location. This is assured since the programmers themselves manipulate the
addresses.
Answer: a
Explanation: The copy constructor can be defined private. If we make it private then the objects
of the class can’t be copied. It can be used when a class used dynamic memory allocation.
Answer: a
Explanation: The copy constructors are always overloaded constructors. They has to be. All the
classes have a default constructor and other constructors are basically overloaded constructors.
Answer: b
Explanation: There are 3 types of constructors in general, namely, default constructors,
parameterized constructors and copy constructors. Default one is called whenever an object is
created without arguments.
class student
{
int marks;
}
student s1;
student s2=2;
Answer: d
Explanation: The object s2 can be assigned with one value only if a single argument constructor
is defined in class, but here, it can’t be done as no constructor is defined. Hence every object
must be declare or created without using arguments.
Answer: c
Explanation: Copy constructor is used while an object is assigned with another. This is
mandatory since we can’t decide which member should be assigned to which member value. By
using copy constructor, we can assign the values in required form.
Answer: a
Explanation: Object must be passed by reference to copy constructor because constructor is not
called in pass by reference. Otherwise, in pass by value, a temporary object will be created
which in turn will try to call its constructor that is already being used. This results in creating
infinite number of objects and hence memory shortage error will be shown.
Answer: d
Explanation: The keyword explicit can be used while defining the constructor only. This is used
to suppress the implicit call to the constructor. It ensures that the constructors are being called
with the default syntax only (i.e. only by using object and constructor name).
Answer: c
Explanation: Default constructors can be called explicitly anytime. They are specifically used to
allocate memory space for the object in memory, in general. It is not necessary that these
should always be called implicitly.
Answer: d
Explanation: Constructors don’t return any value. Those are special functions, whose return
type is not defined, not even void. This is so because the constructors are meant to initialize the
members of class and not to perform some task which can return some value to newly created
object.
Answer: a
Explanation: Static constructors help in initializing the static members of the class. This is
provided because the static members are not considered to be property of the object, rather
they are considered as the property of class.
Answer: b
Explanation: Those are called at very first call of object creation. That is called only one time
because the value of static members must be retained and continued from the time it gets
created.
10. Which among the following is true for static constructor?
a) Static constructors are called with every new object
b) Static constructors are used initialize data members to zero always
c) Static constructors can’t be parameterized constructors
d) Static constructors can be used to initialize the non-static members also
Answer: c
Explanation: Static constructors can’t be parameterized constructors. Those are used to initialize
the value of static members only. And that must be a definite value. Accepting arguments may
make it possible that static members loses their value with every new object being created.
Answer: a
Explanation: Since the static constructors are can’t be parameterized, they can’t be overloaded.
Having this case, only one constructor will be possible to be created in a local scope, because
the signature will always be same and hence it will not be possible to overload static
constructor.
Answer: d
Explanation: Default constructor, which even the programmer doesn’t define, always initialize
the values as zero if numeric and null if string. This is done so as to avoid the accidental values
to change the conditional statements being used and similar conditions.
Answer: c
Explanation: The static constructor is called before creation of first instance of that class. This is
done so that even the first instance can use the static value of the static members of the class
and manipulate as required.
Answer: a
Explanation: If the constructors are defined in private access, then the class can’t be inherited
by other classes. This is useful when the class contains static members only. The instances can
never be created.
15. Which among the following is correct, based on the given code below:
class student
{
int marks;
public : student()
{
cout<<”New student details can be added now”;
}
};
student s1;
Answer: c
Explanation: This program will work fine. This is because it is not mandatory that a constructor
must contain only initialization only. If you want to perform a task on each instance being
created, that code can be written inside the constructor.
Answer: b
Explanation: The constructors are special type of functions which are called whenever an object
is created. This is to initialize the data members of the class. The constructor allocates memory
space for all the data members.
Answer: a
Explanation: The Destructors are special functions which are called just before an object is
destroyed. This functions is responsible to free all the allocated resources to the object. Objects
are destroyed whenever those go out of scope.
Answer: d
Explanation: The constructors must contain only the class name. The class name is followed by
the blank parenthesis or we can have parameters if some values are to be passed.
Answer: c
Explanation: The destructor must have same name as that of the corresponding class. The class
name should be preceded by the tilde symbol (~).
5. Which among the following is true?
a) First the constructor of parent classes are called in sequence of inheritance
b) First the constructor of child classes are called in the sequence of inheritance
c) First constructor called is of the object being created
d) Constructors are called randomly
Answer: a
Explanation: First the constructor of parent class are called. The order in which the parent class
constructors are called is same in the sequence of inheritance used.
Answer: d
Explanation: The destructors are called in the reverse order as that of the constructors being
called. This is done to ensure that all the resources are released in sequence. That is, the
derived class destructors will be called first.
Answer: b
Explanation: The destructors doesn’t have any arguments. The destructors have to be called
implicitly whenever an object goes out of scope. The user can’t pass argument to the implicit
call.
Answer: c
Explanation: The destructors are usually called implicitly whenever an object goes out of scope.
The destructors can also be called explicitly if required. The call must be made, implicitly or
explicitly.
Answer: a
Explanation: Destructor will be called only to free the resources allocated for an object. The
resources are allocated only the constructor for an object is called.
Answer: b
Explanation: The destructor must be public for explicit calls. If it is made private or protected
then it won’t be accessible outside the class. There is no restriction of definition the destructor
outside the class.
Answer: b
Explanation: Even if the class have 4 constructors, only one would be used. And only one
destructor is allowed.
Answer: c
Explanation: The destructors can never be overloaded. The destructors doesn’t have arguments.
And to get overloaded, they must have different signature. This is not possible if arguments are
not allowed.
Answer: d
Explanation: The constructors doesn’t have any return type. The constructors are intended to
allocate the resources for the object. Memory spaces are to be finalized.
Answer: d
Explanation: The destructors are intended to free the memory space. And all the resources that
were allocated for the object. The return value is not supported since only memory have to be
made free.
15. The destructor can be called before the constructor if required. (True/False)
a) True
b) False
Answer: b
Explanation: The destructors can be called only after the constructor calls. It is not a mandatory
rule but the deletion can only take place if there is something created using the constructor.