Question : 01
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.
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.
Question : 02
int *ptr = new int;
delete ptr;
delete ptr;
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.
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.
Question : 03
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.
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.
Question : 04
int *ptr = NULL;
delete ptr;
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.
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.
Question : 05
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.
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.
Question : 06
#include <iostream>
using namespace std;
class A
{
int a;
A() { a = 5;}
};
int main()
{
A *obj = new A;
cout << obj->a;
}
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.
Explanation:
As Test() constructor is private member of the class therefore cannot be accessed from the outside world therefore the program gives error.
Question : 07
#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;
}
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.
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.
Question : 08
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
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.
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.
Question : 09
#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;
}
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.
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.
Question : 10
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];
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];
Question : 11
#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;
}
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.
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.
Question : 12
#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;
}
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.
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.