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?
-
#include
-
#include
-
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
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?
-
#include
-
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
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?
-
#include
-
#include
-
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
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
7. What will be the output of the following C++ code?
-
#include
-
#include
-
#include
-
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)
Answer: c
Clarification: 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?