• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C++ 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

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

Question 3

Predict the output? CPP
#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

Question 4

What happens when delete is used for a NULL pointer? C
 int *ptr = NULL;
 delete ptr; 
  • Compiler Error
  • Run-time Crash
  • No Effect

Question 5

Is it fine to call delete twice for a pointer? CPP
#include<iostream>
using namespace std;

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

There are 5 questions to complete.

Last Updated :
Take a part in the ongoing discussion