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.
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.