C++ Programming Questions and Answers – Free Store

«
»
This section on C++ programming interview questions and answers focuses 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
View Answer

Answer: c
Explanation: Free store is a pool of memory available for you to allocate and deallocate storage for objects during the execution of your program.
advertisement

2. Which operators are used in the free store?
a) new
b) delete
c) both new & delete
d) terminate
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: static is a type of class member is operator new.

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

  1.     #include <new>
  2.     #include <iostream>
  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
View Answer

Answer: b
Explanation: 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?

advertisement
  1.     #include <iostream>
  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
View Answer

Answer: d
Explanation: 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 <cstdlib>
  2.     #include <iostream>
  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
View Answer

Answer: d
Explanation: The memory value allocated for the program depends on compiler only.

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

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

  1.     #include <new>
  2.     #include<cstdlib>
  3.     #include <iostream>
  4.     using namespace std;
  5.     class X;
  6.     struct Node 
  7.     {
  8.         X* data;
  9.         bool filled;
  10.         Node() : filled(false) { }
  11.     };
  12.     class X 
  13.     {
  14.         static Node buffer[];
  15.         public:
  16.         int number;
  17.         enum { size = 3};
  18.         void* operator new(size_t sz) throw (const char*)
  19.         {
  20.             void* p = malloc(sz);
  21.             if (sz == 0)
  22.                 throw "Error: malloc() failed";
  23.             cout << "X :: operator new(size_t)" << endl;
  24.             return p;
  25.         }
  26.         void *operator new(size_t sz, int location) throw (const char*) 
  27.         {
  28.             cout << "X :: operator new(size_t, " << location << ")" << endl;
  29.             void* p = 0;
  30.             if (location < 0 || location >= size || buffer[location].filled == true)
  31.             {
  32.                 throw "Error: buffer location occupied";
  33.             }
  34.             else 
  35.             {
  36.                 p = malloc(sizeof(X));
  37.                 if (p == 0) 
  38.                     throw "Error: Creating X object failed";
  39.                 buffer[location].filled = true;
  40.                 buffer[location].data = (X*) p;
  41.             }
  42.             return p;
  43.         }
  44.         static void printbuffer() 
  45.         {
  46.             for (int i = 0; i < size; i++) 
  47.             {
  48.                 cout << buffer[i].data->number << endl;
  49.             }
  50.         } 
  51.     };
  52.     Node X::buffer[size];
  53.     int main()
  54.     {
  55.         try 
  56.         {
  57.             X* ptr1 = new X;
  58.             X* ptr2 = new(0) X;
  59.             X* ptr3 = new(1) X;
  60.             X* ptr4 = new(2) X;
  61.             ptr2->number = 10000;
  62.             ptr3->number = 10001;
  63.             ptr4->number = 10002;
  64.             X :: printbuffer();
  65.             X* ptr5 = new(0) X;
  66.         }
  67.         catch (const char* message) 
  68.         {
  69.             cout << message << endl;
  70.         }
  71.     }

a) X::operator new(size_t)
b) Error
c) Runtime error
d) operator new(size_d)
View Answer

Answer: c
Explanation: In this program, We are giving a location to two variables in the program, So it is arising an exception.
Output:

$ g++ free3.cpp
$ a.out
X::operator new(size_t)
X::operator new(size_t, 0)
X::operator new(size_t, 1)
X::operator new(size_t, 2)
10000
10001
10002
X::operator new(size_t, 0)
Error: buffer location occupied

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

  1.     #include <iostream>
  2.     #include <new>
  3.     #include <cstdlib>
  4.     using namespace std;
  5.     const int bsize = 512;
  6.     int *pa;
  7.     bool allocate = true;
  8.     void get_memory() 
  9.     {
  10.         cerr << "free store exhausted" << endl;
  11.         delete [] pa;
  12.         allocate = false;
  13.     }
  14.     void eat_memory(int size) 
  15.     {
  16.         int *p = new int[size];
  17.         if (allocate)
  18.             eat_memory(size);
  19.         else
  20.             cerr << "free store addr = " << p << endl;
  21.     }
  22.     int main()
  23.     {
  24.         set_new_handler(get_memory);
  25.         pa = new int[bsize];
  26.         cerr << "free store addr = " << pa << endl;
  27.         eat_memory(bsize);
  28.         return 0;
  29.     }

a) free store addr
b) Error
c) Segmentation fault
d) free store exhausted
View Answer

Answer: c
Explanation: In this program, The memory will go beyond the limit, So there will be exhaustion in memory.
Output:

advertisement
$ g++ free4.cpp
$ a.out
free store addr = 0x80a8008
Segmentation fault

9. What must be an operand of operator delete?
a) Pointer
b) Array
c) Stack
d) Queue
View Answer

Answer: a
Explanation: The operand of delete must be a pointer returned by new.

10. How can object be allocated outside the object lifetime?
a) int
b) float
c) void*
d) char$
View Answer

Answer: c
Explanation: void* object can be allocated outside the object lifetime.

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn