Q: 1 A constructor is called whenever
An object is created
A class is created
A class is declared
More than one of the above
[ Option A ]
A constructor is a special function inside a class that runs automatically when an object of that class is created. Its main purpose is to initialize the object data members.
A class is just a blueprint or logical design, and it does not do anything on its own. Only when an object is created from that class does memory get allocated, and at that moment, the constructor is automatically called to set initial values.
Q: 2 A constructor which takes reference is called.
Default constructor
Dummy constructor
Copy constructor
Reference constructor
[ Option C ]
A copy constructor is a special constructor that is used to create a new object using an existing object. It takes a reference to an object of the same class as its parameter.
Syntax:
ClassName(ClassName &obj)
{
// copy data members
}
Q: 3 Consider the below code (read very carefully)
class myclass
{
int a,b;
public:
void setData(int p)
{
a=b=p;
}
};
void main()
{
myclass m1,m2;
m1.setData(10);
m2=m1;
myclass m3=m1;
}
What happen when the statement myclass m3=m1; is executes:
Executes implicit assignment overloaded function and copy member of m1 to m3.
Executes copy constructor and copy member of m1 to m3.
Executes default constructor and no members are copied.
Error occurred because we cannot copy one object member to another.
[ Option B ]
In C++, when an object is created and initialized using another object of the same class, the copy constructor is called.
In statement myclass m3=m1;
Since no user-defined copy constructor is provided, the compiler generates a default copy constructor, which performs a member-wise copy of a and b from m1 to m3.
Q: 4 Which of the following statements about destructors in C++ is TRUE?
A destructor must have a return type, typically void.
A destructor can take parameters to customize object cleanup.
If a class does not define a destructor, the compiler provides a default public destructor.
Destructors can be overloaded if needed.
[ Option C ]
Q: 5 The _______ is a guaranteed function which executes when object of class is created.
Static function
Constructor function
Inline function
Default function
[ Option B ]
In C++, a constructor is a special member function of a class that is automatically and guaranteed to execute whenever an object of that class is created.
Q: 6
Consider the program below. What will be the output?
#include<iostream>
#include<string>
using namespace std;
class Sample {
string name;
public:
Sample() {
cout<<”s”<<” ”;
}
Sample (string s) : name (s) {
cout<<name<<” ”;
}
};
int main() {
Sample s1;
Sample *s2=new Sample (“s2”);
Sample *s3;
new Sample (“s4”);
return 0;
} s s2 s s4
s2 s s4
s s2 s4
s2 s4 s
[ Option C ]
The line Sample (string s) : name (s), is a parameterized constructor of the class Sample, and : name(s) is an example of a member initializer list in C++. It means that the data member name will be initialized with the value of s before the constructor body runs.
Q: 7 What is output of the following program?
class myclass
{
int a,b;
public:
myclass(){ }
void setData(int p)
{
a=b=p;
}
myclass(myclass &m)
{
b=m.b;
}
void showData()
{
cout<<a<<" "<<b;
}
};
void main()
{
myclass m1;
m1.setData(10);
myclass m2=(m1);
m2.showData();
}
10 10
Garbage Garbage
10 Garbage
Garbage 10
[ Option D ]
Here a custom copy constructor is defined, but it copies only one data member.
In this object m1 is created using the default constructor, which does not initialize the data members a and b. When m1.setData(10) is called, both a and b of m1 are assigned the value 10.
Next, the statement myclass m2=(m1); creates a new object m2 using the copy constructor. However, the copy constructor copies only the member b from m1 to m2 and does not copy a. As a result, in object m2, the value of b becomes 10, while a remains uninitialized and contains a garbage value.
When m2.showData() is executed, it prints the value of a followed by b, resulting in Garbage 10.
Q: 8 Which of the following constructors is provided by the C++ compiler if not defined in a class?
Copy constructor
Default constructor
Parameterized constructor
None of the above
[ Option B ]
In C++, if a class does not explicitly define a constructor, the compiler automatically provides a default constructor. A default constructor is a constructor that takes no arguments. It does not initialize members to specific values unless explicitly coded.
Q: 9 Which of the following is not a type of constructor?
Copy constructor
Friend constructor
Default constructor
Parameterized constructor
[ Option B ]
In Object-Oriented Programming System (OOPS), a constructor is a special member function that automatically gets called when an object of a class is created. Its main purpose is to initialize newly created objects. There are several types of constructors, each serving a distinct purpose in object initialization.
Default Constructor: This is a constructor that takes no arguments and initializes objects with default values.
class Demo {
int x;
public:
Demo() { x = 0; } // Default Constructor.
};Parameterized Constructor: It takes one or more parameters, allowing the object’s data members to be initialized with user-defined values at the time of object creation.
class Demo {
int x;
public:
Demo(int a) { x = a; } // Parameterized Constructor.
};Copy Constructor: The constructor which takes object as an argument of its own types, called copy constructor. It creates a new object as a copy of an existing object.
class Demo {
int x;
public:
Demo(int a) { x = a; } // Parameterized Constructor.
Demo(Demo &obj) { x = obj.x; } // Copy Constructor.
};There is no such thing as a 'friend constructor' in mainstream object-oriented programming languages like C++, Java. We can have friend functions or friend classes in C++, but constructors cannot be declared as friend.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.