This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which of the following is incorrect for a constructor?
Option C
A constructor is a special method in object-oriented programming used to initialize the state of an object when it is created. It is automatically called when an object is instantiated.
However, a constructor does not have any return type, not even void.
Q: 2A constructor is called whenever
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: 3A constructor which takes reference is called.
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: 4Consider 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:
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: 5Which of the following is a valid constructor declaration for the class Book?
Option D
In Object-Oriented Programming, a constructor is a special method used to initialize objects.
Q: 6Which of the following statements about destructors in C++ is TRUE?
Option C
Q: 7The _______ is a guaranteed function which executes when object of class is created.
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: 8In C++, a constructor without any argument is called a
Option C
In C++, a constructor is a special member function used to initialize objects. When a constructor is defined without any arguments or parameters, it is called a Default Constructor.
class Demo
{
public:
Demo()
{
cout<<"Object Created Successfully";
}
};
Q: 9
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;
}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: 10Which of the following statements is/are wrong about constructor in object-oriented programming?
1. The name of the constructor is same as class name.
2. It can have a return value.
3. Constructor is the first code invoked when an object is created.
4. Constructor name need not be the same as that of class name.
Option C
A constructor is a special member function of a class that is executed automatically when an object of the class is created. It is mainly used to initialize instance variables.
Q: 11What 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();
}
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: 12Which of the following constructors is provided by the C++ compiler if not defined in a class?
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: 13Which of the following is not a type of 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.
You have reached the end of this topic. Continue learning with the next topic below.
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.