Questions On Constructors and Destructors
Questions On Constructors and Destructors
Questions On Constructors and Destructors
sanfoundry.com
Answer: b
Explanation: Constructors are the member functions which
are called automatically whenever an object is created. It is
a mandatory functions to be called for an object to be
created as this helps in initializing the object to a legal initial
value for the class.
1 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
for constructors?
a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a definition body
d) It can contains arguments
View Answer
Answer: c
Explanation: Constructors are predefined implicitly, even if
the programmer doesn’t define any of them. Even if the
programmer declares a constructor, it’s not necessary that
it must contain some definition.
Answer: d
Explanation: The constructors must not have any return
type. Also, the body may or may not contain any body.
Defining default constructor is optional, if you are not using
any other constructor.
2 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
View Answer
Answer: a
Explanation: Constructor function should be available to all
the parts of program where the object is to be created.
Hence it is advised to define it in public access, so that any
other function is able to create objects.
Answer: a
Explanation: Two types of constructors are defined
generally, namely, default constructor and parameterized
constructor. Default constructor is not necessary to be
defined always.
3 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
not defined )
d) It will create the object ( as at least some constructor is
defined )
View Answer
Answer: b
Explanation: It will create the object without any problem,
because the default arguments use the default value if no
value is passed. Hence it is equal to default constructor with
zero parameters. But it will not create the object if signature
doesn’t match.
Answer: a
Explanation: If the object is create without arguments and
only parameterized constructors are used, compiler will give
an error as there is no default constructor defined. And
some constructor must be called so as to create an object
in memory.
4 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: While creating the object of class C, its
constructor would be called by default. But, if the class is
inheriting some other class, firstly the parent class
constructor will be called so that all the data is initialized
that is being inherited.
class A{ };
class B{ };
class C: public A, public B{ };
a) A()
b) B()
c) C()
d) Can’t be determined
View Answer
Answer: a
Explanation: Constructor of class A will be called first. This
is because the constructors in multiple inheritance are
called in the sequence in which they are written to be
inherited. Here A is written first, hence it is called first.
5 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: It can’t be defined with zero number of
arguments. This is because to copy one object to another,
the object must be mentioned so that compiler can take
values from that object.
Answer: c
Explanation: Compiler runs out of memory. This is because
while passing the argument by value, a constructor of the
object will be called. That in turn called another object
constructor for values, and this goes on. This is like a
constructor call to itself, and this goes on infinite times,
hence it must be passed by reference, so that the
constructor is not called.
class student
{
int marks;
};
student s1, s2, s3;
6 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) All are created at same time
View Answer
Answer: a
Explanation: The objects are created in the sequence of
how they are written. This happens because the
constructors are called in the sequence of how the objects
are mentioned. This is done in sequence.
Answer: d
Explanation: Explicit call to a constructor can let you create
temporary instance. This is because the temporary
instances doesn’t have any name. Those are deleted from
memory as soon as their reference is removed.
class student
{
int marks;
public: student(){}
7 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
student(int x)
{
marks=x;
}
};
main()
{
student s1(100);
student s2();
student s3=100;
return 0;
Answer: c
Explanation: It is a special case of constructor with only 1
argument. While calling a constructor with one argument,
you are actually implicitly creating a conversion from the
argument type to the type of class. Hence you can directly
specify the value of that one argument with assignment
operator.
8 of 9 3/19/18, 9:29 AM
Constructors - Object Oriented Programming Q... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: Each constructor must differ in the number of
arguments it accepts and the type of arguments. This
actually defines the constructor signature. This helps to
remove the ambiguity and define a unique constructor as
required.
9 of 9 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
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;
1 of 8 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
}
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.
2 of 8 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
constructor because:
a) Constructor is not called in pass by reference
b) Constructor is called in pass by reference only
c) It passes the address of new constructor to be created
d) It passes the address of new object to be created
View Answer
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).
3 of 8 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
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.
4 of 8 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
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.
Answer: c
Explanation: Static constructors can’t be parameterized
5 of 8 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
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
6 of 8 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
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.
7 of 8 3/19/18, 9:29 AM
Object Oriented Programming Quiz Online about:reader?url=https://www.sanfoundry.com/...
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.
8 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
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
1 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
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.
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
2 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
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.
3 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
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.
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.
4 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
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.
Answer: d
Explanation: All the options given, directly or indirectly
5 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
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.
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.
6 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
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 object should not be modified in the copy
constructor. Because the object itself is being copied. When
the object is returned from a function, the object must be a
constant otherwise the complier creates a temporary object
which can die anytime.
Answer: a
Explanation: The copy constructors are always overloaded
constructors. They has to be. All the classes have a default
7 of 8 3/19/18, 9:29 AM
Basic Object Oriented Programming Questions ... about:reader?url=https://www.sanfoundry.com/...
8 of 8 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: c
Explanation: If more than one constructors are defined in a
class with same signature, then that results in error. The
signatures must be different. So that the constructors can
be differentiated.
1 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
a) Yes, always
b) Yes, if derived class has no constructor
c) No, programmer can’t do it
d) No, never
View Answer
Answer: d
Explanation: The constructor must be having the same
name as that of a class. Hence a constructor of one class
can’t even be defined in another class. Since the
constructors can’t be defined in derived class, it can’t be
overloaded too, in derived class.
Answer: d
Explanation: The constructors doesn’t have any return type.
When we can’t have return type of a constructor,
overloading based on the return type is not possible. Hence
only parameters can be different.
2 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: All the constructors defined in a class must
have different signature in order to be overloaded. Here one
default and other parameterized constructors are used,
wherein one is of only one parameter and other accepts
two. Hence overloading is possible.
class A
{
int i;
A()
{
i=0; cout<<i;
}
A(int x=0)
{
i=x; cout<<I;
}
};
A obj1;
a) Default constructor
3 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
b) Parameterized constructor
c) Compile time error
d) Run time error
View Answer
Answer: c
Explanation: When a default constructor is defined and
another constructor with 1 default value argument is
defined, creating object without parameter will create
ambiguity for the compiler. The compiler won’t be able to
decide which constructor should be called, hence compiler
time error.
Answer: b
Explanation: The constructors are not always user defined.
The construct will be provided implicitly from the compiler if
the used doesn’t defined any constructor. The implicit
constructor makes all the string values null and allocates
memory space for each data member.
4 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: The constructor is called as soon as the object
is created. The overloading comes into picture as to identify
which constructor have to be called depending on
arguments passed in creation of object.
Answer: a
Explanation: The function this() can be used to call the
default constructor from inside any other constructor. This
helps to further reuse the code and not to write the
redundant data in all the constructors.
Answer: c
Explanation: The constructors are overloaded to initialize
the objects of a class in different ways. This allows us to
5 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: When the programmer doesn’t specify any
default constructor and only defines some parameterized
constructor. The compiler doesn’t provide any default
constructor implicitly. This is because it is assumed that the
programmer will create the objects only with constructors.
Answer: b
Explanation: Java doesn’t provide the feature to recursively
call the constructor. This is to eliminate the out of memory
6 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
class A
{
int i;
A()
{
i=0;
}
A(int x)
{
i=x+1;
}
A(int y, int x)
{
i=x+y;
}
};
A obj1(10);
A obj2(10,20);
A obj3;
a) A(int x)
b) A(int y)
c) A(int y, int x)
d) A(int y; int x)
View Answer
7 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The two argument constructor will be called as
we are passing 2 arguments to the object while creation.
The arguments will be passed together and hence compiler
resolves that two argument constructor have to be called.
Answer: d
Explanation: The object becomes a reference object which
can be initialized will another object. Then this object will
indirectly become another name of the object being
assigned. If not assigned, it simply points to null address.
Answer: a
Explanation: Kotlin language allows constructor
overloading. This is a basic feature for the constructors.
The constructor overloading allows the object to be
8 of 9 3/19/18, 9:29 AM
Overloading Constructors - Object Oriented Pro... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The syntax for object creating in java with
calling a constructor for is it is as in option c. The syntax
must contain the classname followed by the object name.
The kwyword new must be used and then the constructor
call with or without the parameters as required.
9 of 9 3/19/18, 9:29 AM
Object Oriented Programming Questions and A... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: b
Explanation: The constructors are special type of functions
which are called whenever an object is created. This is to
1 of 7 3/19/18, 9:30 AM
Object Oriented Programming Questions and A... about:reader?url=https://www.sanfoundry.com/...
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
2 of 7 3/19/18, 9:30 AM
Object Oriented Programming Questions and A... about:reader?url=https://www.sanfoundry.com/...
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 (~).
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.
3 of 7 3/19/18, 9:30 AM
Object Oriented Programming Questions and A... about:reader?url=https://www.sanfoundry.com/...
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
4 of 7 3/19/18, 9:30 AM
Object Oriented Programming Questions and A... about:reader?url=https://www.sanfoundry.com/...
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.
5 of 7 3/19/18, 9:30 AM
Object Oriented Programming Questions and A... about:reader?url=https://www.sanfoundry.com/...
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.
6 of 7 3/19/18, 9:30 AM
Object Oriented Programming Questions and A... about:reader?url=https://www.sanfoundry.com/...
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.
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.
7 of 7 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
sanfoundry.com
Answer: a
Explanation: It is used to free the resources that the object
might had used in its lifespan. The destructors are called
implicitly whenever an object’s life ends.
1 of 8 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: d
Explanation: The destructor is called just before the object
go out of scope or just before its life ends. This is done to
ensure that all the resources reserved for the object are
used and at last, are made free for others.
Answer: a
Explanation: It doesn’t have destructors. Since an abstract
class don’t have constructors, and hence can’t have
instances. Having this case, the abstract classes doesn’t
have destructors too, because that would be of no use
here.
2 of 8 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: c
Explanation: The destructors are always called in the
reverse order of how the constructors were called. Here
class A constructor would have been created first if Class C
object is declared. Hence class A destructor is called at last.
Answer: d
Explanation: In multiple inheritance, the constructors are
called in the sequence of how they are written in inheritance
sequence. And the destructors will be called in the reverse
order. This can be cross verified just by printing a message
from each destructor defined in classes.
3 of 8 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: This is because the lifespan of global object is
from start of the program, till the end of the program. And
hence program end is the end of global object too. Just
before the end of program, the destructor will be called to
free the acquired resources by the objects.
Answer: c
Explanation: The destructors are preceded with the tilde (~)
symbol. The name is same as that of the class. These also
doesn’t have any return type.
Answer: a
Explanation: The destructors doesn’t accept the arguments.
Those are just used to free up the resources.
4 of 8 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: b
Explanation: The destructors can be virtual. It is actually
advised to keep the destructors virtual always. This is done
to suppress the problems that may arise if inheritance is
involved.
Answer: c
Explanation: The destructors are always called in reverse
order no matter which destructor it is. This is done to
ensure that all the resources are able to get free. And no
resource is kept busy.
5 of 8 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: This is always advised to have user defined
destructor when pointers are involved in class. This is
usually done to ensure that the memory, that was allocated
dynamically, gets free after use and doesn’t cause memory
leak.
Answer: d
Explanation: This is so because the destructors can’t be
overloaded. And the destructor must have the same name
as that of class with a tilde symbol preceding the name of
destructor. Hence there can be only one destructor in a
class. Since more than one function with same name and
signature can’t be present in same scope.
class A{ };
class B{ };
class C: public B{ };
6 of 8 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
A a;
B b;
C c;
a) ~A()
b) ~B()
c) ~C()
d) ~B() and ~C()
View Answer
Answer: c
Explanation: The constructor that would have created at
last, its destructor will be called first when the code goes
out of scope. This will help the program to manage the
resources more efficiently.
Answer: a
Explanation: When an object is passed to a function, its
copy is made in the function. This copy acts as a real object
till the function is live. When the function is returned, the
copy’s destructor is called to free the resources held by it.
7 of 8 3/19/18, 9:30 AM
Destructors - Object Oriented Programming Que... about:reader?url=https://www.sanfoundry.com/...
Answer: a
Explanation: The destructor is never called in this situation.
The concept is that when an object is passed by reference
to the function, the constructor is not called, but only the
main object will be used. Hence no destructor will be called
at end of function.
8 of 8 3/19/18, 9:30 AM