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
Explanation: 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
View Answer
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
Explanation: static is a type of class member is operator new.
4. What will be the output of the following C++ code?
#include <new>
#include <iostream>
using namespace std;
struct A
{
virtual ~A() { };
void operator delete(void* p)
{
cout << "A :: operator delete" << endl;
}
};
struct B : A
{
void operator delete(void* p)
{
cout << "B :: operator delete" << endl;
}
};
int main()
{
A* ap = new B;
delete ap;
}
a) A::operator delete
b) B::operator delete
c) Both A::operator delete & B::operator delete
d) A:operator new
View Answer
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?
#include <iostream>
using namespace std;
struct A
{
virtual ~A()
{
cout << "~A()" << endl;
}
void operator delete[](void* p, size_t)
{
cout << "A :: operator delete[]" << endl;
delete [] p;
}
};
struct B : A
{
void operator delete[](void* p, size_t)
{
cout << "B :: operator delete[]" << endl;
delete [] p;
}
};
int main()
{
A* bp = new B[3];
delete[] bp;
};
a) ~A()
b) A :: operator delete[]
c) B :: operator delete[]
d) Warning
View Answer
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?
#include <cstdlib>
#include <iostream>
using namespace std;
class X
{
public:
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz);
if (p == 0)
throw "malloc() failed";
return p;
}
void operator delete(void* p)
{
cout << "X :: operator delete(void*)" << endl;
free(p);
}
};
class Y
{
int filler[100];
public:
void operator delete(void* p, size_t sz) throw (const char*)
{
cout << "Freeing " << sz << " bytes" << endl;
free(p);
};
};
int main()
{
X* ptr = new X;
delete ptr;
Y* yptr = new Y;
delete yptr;
}
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
Explanation: The memory value allocated for the program depends on compiler only.
$ g++ free2.cpp $ a.out X :: operator delete(void*) Freeing 400 bytes
7. What will be the output of the following C++ code?
#include <new>
#include<cstdlib>
#include <iostream>
using namespace std;
class X;
struct Node
{
X* data;
bool filled;
Node() : filled(false) { }
};
class X
{
static Node buffer[];
public:
int number;
enum { size = 3};
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz);
if (sz == 0)
throw "Error: malloc() failed";
cout << "X :: operator new(size_t)" << endl;
return p;
}
void *operator new(size_t sz, int location) throw (const char*)
{
cout << "X :: operator new(size_t, " << location << ")" << endl;
void* p = 0;
if (location < 0 || location >= size || buffer[location].filled == true)
{
throw "Error: buffer location occupied";
}
else
{
p = malloc(sizeof(X));
if (p == 0)
throw "Error: Creating X object failed";
buffer[location].filled = true;
buffer[location].data = (X*) p;
}
return p;
}
static void printbuffer()
{
for (int i = 0; i < size; i++)
{
cout << buffer[i].data->number << endl;
}
}
};
Node X::buffer[size];
int main()
{
try
{
X* ptr1 = new X;
X* ptr2 = new(0) X;
X* ptr3 = new(1) X;
X* ptr4 = new(2) X;
ptr2->number = 10000;
ptr3->number = 10001;
ptr4->number = 10002;
X :: printbuffer();
X* ptr5 = new(0) X;
}
catch (const char* message)
{
cout << message << endl;
}
}
a) X::operator new(size_t)
b) Error
c) Runtime error
d) operator new(size_d)
View Answer
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?
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
const int bsize = 512;
int *pa;
bool allocate = true;
void get_memory()
{
cerr << "free store exhausted" << endl;
delete [] pa;
allocate = false;
}
void eat_memory(int size)
{
int *p = new int[size];
if (allocate)
eat_memory(size);
else
cerr << "free store addr = " << p << endl;
}
int main()
{
set_new_handler(get_memory);
pa = new int[bsize];
cerr << "free store addr = " << pa << endl;
eat_memory(bsize);
return 0;
}
a) free store addr
b) Error
c) Segmentation fault
d) free store exhausted
View Answer
Explanation: In this program, The memory will go beyond the limit, So there will be exhaustion in memory.
Output:
$ 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
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
Explanation: void* object can be allocated outside the object lifetime.
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.
- Practice Computer Science MCQs
- Check C++ Books
- Apply for Computer Science Internship
- Check Programming Books
- Apply for C++ Internship