250+ TOP MCQs on OOPs Concept – 4 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “OOPs Concept – 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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.

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

#include 
using namespace std;
class A{
public:
	A(){
		cout<<"Constructor calledn";
	   }
	~A(){
		cout<<"Destructor calledn";
	    }
};
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
Answer: d
Clarification: 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.

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

#include 
using namespace std;
class A{
public:
	A(){
		cout<<"Constructor calledn";
	   }
	~A(){
		cout<<"Destructor calledn";
	    } 
};
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
Answer: a
Clarification: 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.

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

#include 
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
Clarification: 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 
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
Clarification: 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];
Answer: b
Clarification: 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
Answer: d
Clarification: 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 
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
Answer: c
Clarification: 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
Answer: d
Clarification: 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
Answer: a
Clarification: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *