1. What is the difference between constructors and destructors?
A. They have a different function name
B. Constructors does not have return type whereas destructors do have
C. Constructors allow function parameters whereas destructors do not
D. Constructors does not function parameters
Answer: C
Explanation:
Both the constructors and destructors have the same function name and both of them do not have return type but constructors allow function parameters whereas destructors do not.
2. How many Destructors are allowed in a Class?
A. 1
B. 2
C. 3
D. Any number
Answer: A
Explanation:
A class in C++ allows only one destructor, which is called whenever the lifetime of an object ends.
3. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"A's Constructor called\n";
}
~A(){
cout<<"A's Destructor called\n";
}
};
class B{
A a;
public:
B(){
cout<<"B's Constructor called\n";
}
~B(){
cout<<"B's Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
B b1;
}
A.
A’s Constructor called
B’s Constructor called
B.
A’s Destructor called
B’s Destructor called
C.
A’s Constructor called
B’s Constructor called
B’s Destructor called
A’s Destructor called
D.
A’s Constructor called
B’s Constructor called
A’s Destructor called
B’s Destructor called
Answer: C
Explanation:
The destructors for an object is called before the destructor of its data members or bases.
4. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"A's Constructor called\n";
}
~A(){
cout<<"A's Destructor called\n";
}
};
class B: public A{
public:
B(){
cout<<"B's Constructor called\n";
}
~B(){
cout<<"B's Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
B b1;
}
A.
A’s Constructor called
B’s Constructor called
B.
A’s Destructor called
B’s Destructor called
C.
A’s Constructor called
B’s Constructor called
B’s Destructor called
A’s Destructor called
D.
A’s Constructor called
B’s Constructor called
A’s Destructor called
B’s Destructor called
Answer: C
Explanation:
Though B class have no data member of the class but as class B is derived from class A, the destructor of class A will be called to destroy the data inherited from class A to class B.
5. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"A's Constructor called\n";
}
~A(){
cout<<"A's Destructor called\n";
}
};
class B: public A{
A a;
public:
B(){
cout<<"B's Constructor called\n";
}
~B(){
cout<<"B's Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
B b1;
}
A.
A’s Constructor called
B’s Constructor called
B.
A’s Destructor called
B’s Destructor called
C.
A’s Constructor called
A’s Constructor called
B’s Constructor called
B’s Destructor called
A’s Destructor called
A’s Destructor called
D.
A’s Constructor called
B’s Constructor called
A’s Destructor called
B’s Destructor called
Answer: C
Explanation:
There are two calls to constructor of class A, one is for the data member of class B and second because class B is derived from class A. Similarly two destructor calls.
6. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"A's Constructor called\n";
}
~A(){
cout<<"A's Destructor called\n";
}
};
class B{
static A a;
public:
B(){
cout<<"B's Constructor called\n";
}
~B(){
cout<<"B's Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
B b1;
}
A.
A’s Constructor called
B’s Constructor called
B.
B’s Constructor called
B’s Destructor called
C.
A’s Constructor called
B’s Constructor called
B’s Destructor called
A’s Destructor called
D.
A’s Constructor called
B’s Constructor called
A’s Destructor called
B’s Destructor called
Answer: B
Explanation:
Here as ‘a’ is a static member of class B and as all static members should be initialized separately as no object creation initializes static member and as ‘a’ is not initialized, hence no call will be made to the constructor of class A.
7. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"A's Constructor called\n";
}
~A(){
cout<<"A's Destructor called\n";
}
};
class B{
static A a;
public:
B(){
cout<<"B's Constructor called\n";
}
~B(){
cout<<"B's Destructor called\n";
}
};
A B::a;
int main(int argc, char const *argv[])
{
return 0;
}
A.
A’s Constructor called
A’s Destructor called
B.
B’s Constructor called
B’s Destructor called
C.
A’s Constructor called
B’s Constructor called
B’s Destructor called
A’s Destructor called
D.
A’s Constructor called
B’s Constructor called
A’s Destructor called
B’s Destructor called
Answer: A
Explanation:
Here as no object of B is declared so no call to B’s constructor but as we have initialised the static member ‘a’ of class B, hence A’s constructor and destructor will be called once.
8. Which of the following represents the correct explicit call to a constructor of class A?
class A{
int a;
public:
A(int i)
{
a = i;
}
}
A. A a(5);
B. A a;
C. A a = A(5);
D. A a = A();
Answer: C
Explanation:
Explicit call represents the programmer by himself mentioning the type name. So A a = A(5); is the correct explicit call as we are mentioning typename A(5) from our side, whereas A a = A(); is not the correct call because no such constructor is there in class A.
- 10+ TOP C++ MCQs on OOPs Concept – 2
- 10+ TOP C++ MCQs on OOPs Concept – 3
- 10+ TOP C++ MCQs on OOPs Concept – 4
- 10+ TOP C++ MCQs on Integer Types
- 10+ TOP C++ MCQs on Floating Point Types
- 10+ TOP C++ MCQs on References – 2
- 10+ TOP C++ MCQs on References – 3
- 10+ TOP C++ MCQs on Objects