Free Store(C++ Programming ) Questions and Answers

Question 1.

How can object be allocated outside the object lifetime?


  1.    int
  2.    float
  3.    void*
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> void*

None.



Question 2.

What must be an operand of operator delete?


  1.    Pointer
  2.    Array
  3.    Stack
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> Pointer

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



Question 3.


What is the output of this program?


1.
#include
2.
#include
3.
#include
4.
using namespace std;
5.
const int bsize = 512;
6.
int *pa;
7.
bool allocate = true;
8.
void get_memory()
9.
{
10.
cerr
  1.    free store addr
  2.    Error
  3.    Segmentation fault
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> Segmentation fault

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



Question 4.


What is the output of this program?


1.
#include
2.
#include
3.
#include
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
  1.    X::operator new(size_t)
  2.    Error
  3.    Runtime error
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> Runtime error

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



Question 5.


What is the output of this program?


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
  1.    X::operator delete(void*)
  2.    Freeing 400 bytes
  3.    Depends on the compiler
  4.    Both a & c
Explanation:-
Answer: Option D. -> Both a & c

The memory value allocated for the program depends on compiler ony.
$ g++ free2.cpp
$ a.out
X :: operator delete(void*)
Freeing 400 bytes



Question 6.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
struct A
4.
{
5.
virtual ~A()
6.
{
7.
cout
  1.    ~A()
  2.    A :: operator delete[]
  3.    B :: operator delete[]
  4.    Warning
Explanation:-
Answer: Option D. -> Warning

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[]




Question 7.

Which is used to allocate and deallocate storage for objects during the execution?


  1.    Stack
  2.    Heap
  3.    Freestore
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> Freestore

Free store is a pool of memory available for you to allocate and deallocate storage for objects 

during the execution of your program.



Question 8.

Which operators are used in the free store?


  1.    new
  2.    delete
  3.    Both a & b
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> Both a & b

new and delete operators is used to allocate and deallocate the memory for the program.



Question 9.

What type of class member is operator new?


  1.    static
  2.    dynamic
  3.    const
  4.    smart
Explanation:-
Answer: Option A. -> static

None.



Question 10.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
struct A
5.
{
6.
virtual ~A() { };
7.
void operator delete(void* p)
8.
{
9.
cout
  1.    A::operator delete
  2.    B::operator delete
  3.    Both a & b
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> B::operator delete

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