250+ TOP MCQs on Free Store and Answers

This section on C++ programming interview questions and answers on “Free Store”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language programming interview questions on “Free Store” along with answers, explanations and/or solutions:

1. Which is used to allocate and deallocate storage for objects during the execution?
a) Stack
b) Heap
c) Freestore
d) Queue
Answer: c
Clarification: Free store is a pool of memory available for you to allocate and deallocate storage for objects during the execution of your program.

2. Which operators are used in the free store?
a) new
b) delete
c) both new & delete
d) terminate
Answer: c
Clarification: new and delete operators are used to allocate and deallocate the memory for the program.

3. What type of class member is operator new?
a) static
b) dynamic
c) const
d) smart
Answer: a
Clarification: static is a type of class member is operator new.

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     struct A 
  5.     {
  6.         virtual ~A() {  };
  7.         void operator delete(void* p) 
  8.         {
  9.             cout << "A :: operator delete" << endl;
  10.         }
  11.     };
  12.     struct B : A 
  13.     {
  14.         void operator delete(void* p) 
  15.         {
  16.             cout << "B :: operator delete" << endl;
  17.         }
  18.     };
  19.     int main() 
  20.     {
  21.         A* ap = new B;
  22.         delete ap;
  23.     }

a) A::operator delete
b) B::operator delete
c) Both A::operator delete & B::operator delete
d) A:operator new
Answer: b
Clarification: In this program, We are passing the value to the B, So we are printing B::operator delete.
Output:

$ g++ free.cpp
$ a.out
B::operator delete

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

  1.     #include 
  2.     using namespace std;
  3.     struct A
  4.     {
  5.         virtual ~A()
  6.         { 
  7.             cout << "~A()" << endl; 
  8.         }
  9.         void operator delete[](void* p, size_t)
  10.         {
  11.             cout << "A :: operator delete[]" << endl;
  12.             delete [] p;
  13.         }
  14.     };
  15.     struct B : A 
  16.     {
  17.         void operator delete[](void* p, size_t) 
  18.         {
  19.             cout << "B :: operator delete[]" << endl;
  20.             delete [] p;
  21.         }
  22.     };
  23.     int main() 
  24.     {
  25.         A* bp = new B[3];
  26.         delete[] bp;
  27.     };

a) ~A()
b) A :: operator delete[]
c) B :: operator delete[]
d) Warning
Answer: d
Clarification: In this program, the behavior of the statement delete[] bp is undefined.

$ g++ a.cpp
a.cpp: In static member function ‘static void A::operator delete [](void*, size_t):
a.cpp:12: warning: deleting ‘void*’ is undefined
a.cpp: In static member function ‘static void B::operator delete [](void*, size_t):
a.cpp:20: warning: deleting ‘void*’ is undefined
 
$ a.out
~A()
~A()
~A()
A :: operator delete[].

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class X 
  5.     {
  6.         public:
  7.         void* operator new(size_t sz) throw (const char*)
  8.         {
  9.             void* p = malloc(sz);
  10.             if (p == 0) 
  11.                 throw "malloc() failed";
  12.             return p;
  13.         }
  14.         void operator delete(void* p) 
  15.         {
  16.             cout << "X :: operator delete(void*)" << endl;
  17.             free(p);
  18.         } 
  19.     };
  20.     class Y 
  21.     {
  22.         int filler[100];
  23.         public:
  24.         void operator delete(void* p, size_t sz) throw (const char*)
  25.         {
  26.             cout << "Freeing " << sz << " bytes" << endl;
  27.             free(p);
  28.         };
  29.     };
  30.     int main() 
  31.     {
  32.         X* ptr = new X;
  33.         delete ptr;
  34.         Y* yptr = new Y;
  35.         delete yptr;
  36.     }

a) X::operator delete(void*)
b) Freeing 400 bytes
c) Depends on the compiler
d) Both X::operator delete(void*) & Depends on the compiler
Answer: d
Clarification: The memory value allocated for the program depends on compiler only.

$ g++ free2.cpp
$ a.out
X :: operator delete(void*)
Freeing 400 bytes