C++ Programming MCQ – Constructors and Destructors – 3

This set of C++ Programming written test Questions & Answers focuses on “Constructors and Destructors – 3”.

1. Which of the following constructors are provided by the C++ compiler if not defined in a class?
a) Default constructor
b) Assignment constructor
c) Copy constructor
d) All of the mentioned
View Answer

Answer: d
Explanation: If a programmer does not define the above constructors in a class the C++ compiler by default provides these constructors to avoid error on basic operations.

2. When a copy constructor is called?
a) When an object of the class is returned by value
b) When an object of the class is passed by value to a function
c) When an object is constructed based on another object of the same class
d) All of the mentioned
View Answer

Answer: d
Explanation: Copy constructor is called in all the above-mentioned criteria because in all the above cases we are somehow trying to copy one object into another.

3. What will be the output of the following C++ code?

advertisement
advertisement
#include <iostream>  
using namespace std;
class A{
	A(){
		cout<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A a;
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: No constructor should be made private because objects need to call them and as by default all the members of a class are private therefore constructor defined in the above program is private which is wrong therefore the compiler gives the error.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
 
public:
	A(){
		cout<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A *a;
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: As we have declared a pointer variable for class A but we have not initialized the memory to that pointer and until the memory is not initialized the constructor for the pointer variable will not be called hence nothing is printed on the screen.
advertisement

5. What will be the output of the following C++ code?

advertisement
#include <iostream>  
using namespace std;
class A{
public:
	int a;
};
int main(int argc, char const *argv[])
{
	A a1 = {10};
	A a2 = a1;
	cout<<a1.a<<a2.a;
	return 0;
}

a) 1010
b) 87368746
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: Although the declaration of object a1 looks erroneous but actually it is acceptable by the C++ compiler to take values for class objects as mentioned above. Next value of a1 is copied to a2 hence 1010 is printed.
Output:
$ ./a.out
1010

6. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(int a){
		this->a = a;
	}
};
int main(int argc, char const *argv[])
{
	A a1, a2(10);
	cout<<a2.a;
	return 0;
}

a) 10
b) Compile time error
c) Run-time error
d) No output
View Answer

Answer: b
Explanation: The declaration of object a1 needs a constructor without any arguments which is not available in the class as we have overwritten the default constructor, therefore, the program gives the error.

7. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(int a=0){
		this->a = a;
	}
};
int main(int argc, char const *argv[])
{
	A a1, a2(10);
	cout<<a2.a;
	return 0;
}

a) 010
b) 100
c) 001
d) Error
View Answer

Answer: a
Explanation: As constructor is accepting default parameter therefore the declaration of a1 and a2 both are valid hence the program runs successfully.

8. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A *a1 = (A*)malloc(sizeof(A));
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: Unlike new malloc never calls the constructor of a class hence when we are assigning memory to an object of class A using malloc constructor is not called hence nothing is printed.

9. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor called";
	}
} a;
int main(int argc, char const *argv[])
{
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: In this program, we have defined a global variable an outside main function for which constructor will be called hence the output is printed.

10. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor called\n";
	}
} a;
int main(int argc, char const *argv[])
{
	cout<<"Main function\n";
	return 0;
}

a)

Constructor called
Main function

b)

Main function
Constructor called

c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: In this program, we have defined a global variable an outside main function for which constructor will be called. Now as a is a global variable, therefore, the call for constructor will be before the main function hence constructor called will be printed before the Main function.

11. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
	A(){
		cout<<"A's Constructor called\n";
	}
};
class B
{
public:
	A a;
	B(){
		cout<<"B's constructor called\ns";
	}
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

a)

A's Constructor called
B's constructor called

b)

B's Constructor called
A's constructor called

c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: As constructor of class A is defined private and we are trying to define an object of class A which cannot call this constructor as it is private therefore the program gives an error.

12. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
	A(){
		cout<<"A's Constructor called\n";
	}
	friend class B;
};
class B
{
public:
	A a;
	B(){
		cout<<"B's constructor called\ns";
	}
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

a)

A's Constructor called
B's constructor called

b)

B's Constructor called
A's constructor called

c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: Now still the constructor of a class is private but class B is friend class of A hence it can access the private members of class A and as in the above program we defining an object of class A in class B only, therefore, the program runs fine.

13. What will be the output of the following C++ code?

#include <iostream>  
using namespace std;
class A{
	~A(){}
};
class B
{
public:
	A a;
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

a)

A's Constructor called
B's constructor called

b)

B's Constructor called
A's constructor called

c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: In this program, the destructor of class A is private therefore the destructor for object a cannot be called hence the program gives an error.

14. How destructor overloading is done?
a) By changing the number of parameters
b) By changing the parameters type
c) By changing both the number of parameters and their type
d) No chance for destructor overloading
View Answer

Answer: d
Explanation: A class is allowed to have only one destructor. Therefore there is no point of destructor overloading.

15. Which of the following is correct?
a) Destructors can be virtual
b) There can be more than one destructor in a class
c) Destructor definition starts with !
d) Destructor is used to initialize objects
View Answer

Answer: a
Explanation: Destructors can be virtual. They are used to destroy objects. Only one destructor is allowed per class. Destructor definition starts with a tilde(~).

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.