C++ Programming Questions and Answers – OOPs – 4

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “OOPs – 4”.

1. Which of the following is correct about new and malloc?
a) Both are available in C
b) Pointer object initialization of a class with both new and malloc calls the constructor of that class
c) Pointer object initialization of a class using new involves constructor call whereas using malloc does not involve constructor call
d) Pointer object initialization of a class using malloc involves constructor call whereas using new does not involve constructor call
View Answer

Answer: c
Explanation: Object initialization using new keyword involves constructor call whereas malloc does not involve constructor call. That’s why new is explicitly added in C++. Also, malloc is used to assign memory to any pointer hence it assigns memory equals to the size of the class however new keyword involves initialization also hence calls the constructor of that class.

2. What is virtual inheritance?
a) C++ technique to avoid multiple copies of the base class into children/derived class
b) C++ technique to avoid multiple inheritances of classes
c) C++ technique to enhance multiple inheritance
d) C++ technique to ensure that a private member of the base class can be accessed somehow
View Answer

Answer: a
Explanation: Virtual inheritance is a C++ technique with which it ensures that a derived class contains only one copy of the base class’s variables. Refer Wikipedia for more info.

3. What is the difference between delete and delete[] in C++?
a) delete is used to delete normal objects whereas delete[] is used to pointer objects
b) delete is a keyword whereas delete[] is an identifier
c) delete is used to delete single object whereas delete[] is used to multiple(array/pointer of) objects
d) delete is syntactically correct but delete[] is wrong and hence will give an error if used in any case
View Answer

Answer: c
Explanation: delete is used to delete a single object initiated using new keyword whereas delete[] is used to delete a group of objects initiated with the new operator.
advertisement
advertisement

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

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

a) “Constructor called” five times and then “Destructor called” five times
b) “Constructor called” five times and then “Destructor called” once
c) Error
d) Segmentation fault
View Answer

Answer: d
Explanation: The program will result in segmentation fault as we are trying to delete only one pointer variable and leaving other variables as it is which will result in segmentation fault i.e. improper handling of memory.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

a) “Constructor called” five times and then “Destructor called” five times
b) “Constructor called” five times and then “Destructor called” once
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: In the above program we have first initiated five-pointer variables using new keyword hence fives time constructor will be called after that as we using delete[](used for deleting multiple objects) to delete variables hence all the five objects created will be destroyed and hence five times destructor will be called.
advertisement

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

#include<iostream> 
using namespace std; 
class Base { 
public: 
	Base()	 
	{ cout<<"Constructing Base \n"; } 
	~Base() 
	{ cout<<"Destructing Base \n"; }	 
}; 
class Derived: public Base { 
public: 
	Derived()	 
	{ cout<<"Constructing Derived \n"; } 
	~Derived() 
	{ cout<<"Destructing Derived \n"; } 
}; 
 
int main(void) 
{ 
	Derived *d = new Derived(); 
	Base *b = d; 
	delete b; 
	return 0; 
}

a)

Constructing Base 
Constructing Derived 
Destructing Base

b)

Constructing Base 
Constructing Derived 
Destructing Derived
Destructing Base

c)

Constructing Base 
Constructing Derived 
Destructing Base
Destructing Derived

d)

Constructing Derived 
Constructing Base 
Destructing Base
Destructing Derived
View Answer
Answer: a
Explanation: As we are storing a derived class object into base class pointer therefore when the object is destroyed the program has not called the Derived class destructor which shows that the object is not destroyed therefore the program may give unusual behaviour.
 
 

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

#include<iostream> 
using namespace std; 
class Base { 
public: 
	Base()	 
	{ cout<<"Constructing Base \n"; } 
	virtual~Base() 
	{ cout<<"Destructing Base \n"; }	 
}; 
class Derived: public Base { 
public: 
	Derived()	 
	{ cout<<"Constructing Derived \n"; } 
	~Derived() 
	{ cout<<"Destructing Derived \n"; } 
}; 
 
int main(void) 
{ 
	Derived *d = new Derived(); 
	Base *b = d; 
	delete b; 
	return 0; 
}

a)

Constructing Base 
Constructing Derived 
Destructing Base

b)

Constructing Base 
Constructing Derived 
Destructing Derived
Destructing Base

c)

Constructing Base 
Constructing Derived 
Destructing Base
Destructing Derived

d)

Constructing Derived 
Constructing Base 
Destructing Base
Destructing Derived
View Answer
Answer: b
Explanation: In this case, we have made the destructor of base class virtual which will ensure that any derived class object which is pointed by a base class pointer object on deletion should call both base and derived class destructor.
 
 

8. What is the correct syntax of declaring array of pointers of integers of size 10 in C++?
a) int arr = new int[10];
b) int **arr = new int*[10];
c) int *arr = new int[10];
d) int *arr = new int*[10];
View Answer

Answer: b
Explanation: As we have to declare an array of pointers of integers therefore we need double pointer array in which each element is collection pointers to integers. Therefore the correct syntax is int **arr = new int*[10];

9. Which of the following is correct about new and malloc?
i) new is an operator whereas malloc is a function
ii) new calls constructor malloc does not
iii) new returns required pointer whereas malloc returns void pointer and needs to be typecast
a) i and ii
b) ii and iii
c) i and iii
d) i, ii and iii
View Answer

Answer: d
Explanation: All the statements about the new and malloc are correct. new is an operator whereas malloc() is a function. The constructor is called when new is used and new returns required type memory pointer.

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

#include <iostream>
using namespace std;
 
class A 
{
   int a;
   A() { a = 5;}
};
 
int main()
{
    A *obj = new A;
    cout << obj->a;
}

a) 5
b) Garbage value
c) Compile-time error
d) Run-time error
View Answer

Answer: c
Explanation: As Test() constructor is private member of the class therefore cannot be accessed from the outside world therefore the program gives error.

11. What happens if the following C++ statement is compiled and executed?

int *ptr = NULL;
delete ptr;

a) The program compiled successfully but throws an error during run-time
b) The program gives a compile-time error
c) The program is not semantically correct
d) The program is compiled and executed successfully
View Answer

Answer: d
Explanation: The above statement is syntactically and semantically correct as C++ allows the programmer to delete a NULL pointer, therefore, the program is compiled and executed successfully.

12. What happens if a pointer is deleted twice in a program as shown in the following C++ statements?

int *ptr = new int;
delete ptr;
delete ptr;

a) Undefined behaviour
b) Syntactically incorrect
c) Semantically incorrect
d) The program runs perfectly
View Answer

Answer: a
Explanation: Deleting a pointer twice in a program may lead to run-time error or may run perfectly. It depends on the compiler how it handles the situation so the program may compile and run successfully but actually the program should give a run-time error(segmentation fault) as you are trying to access the unauthorized memory of the system.

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.