Exercise: Questions on new and delete


Questions on new and delete : Question 1 :
How to create a dynamic array of pointers (to integers) of size 10 using new in C++? Hint: We can create a non-dynamic array using int *arr[10]

int *arr = new int *[10];
int **arr = new int *[10];
int *arr = new int [10];
Not Possible
Show Answer
Questions on new and delete : Question 2 :
Which of the following is true about new when compared with malloc. 1) new is an operator, malloc is a function 2) new calls constructor, malloc doesn't 3) new returns appropriate pointer, malloc returns void * and pointer needs to typecast to appropriate type.

1 and 3
2 and 3
1 and 2
All 1, 2 and 3
Show Answer
Questions on new and delete : Question 3 :
Predict the output?
#include <iostream>
using namespace std;

class Test 
{
  int x;
  Test() { x = 5;}
};

int main()
{
   Test *t = new Test;
   cout << t->x;
}

Compiler Error
5
Garbage Value
0
Show Answer
Questions on new and delete : Question 4 :
What happens when delete is used for a NULL pointer?
 int *ptr = NULL;
 delete ptr; 

Compiler Error
Run-time Crash
No Effect
Show Answer
Questions on new and delete : Question 5 :
Is it fine to call delete twice for a pointer?
#include<iostream>
using namespace std;

int main()
{
    int *ptr = new int;
    delete ptr;
    delete ptr;
    return 0;
}

Yes
No
Show Answer