Site icon T4Tutorials.com

nullptr and dynamic memory management (new, delete) — C++ MCQs

1. What is nullptr in C++?

(A) Null integer


(B) Null pointer constant


(C) Null reference


(D) Garbage value



2. Which of the following is correct to assign nullptr to a pointer?

(A) int *p = 0;


(B) int *p = nullptr;


(C) int *p = NULL;


(D) All of the above



3. What is the type of nullptr?

(A) int


(B) void*


(C) std::nullptr_t


(D) char*



4. What does new int; do?

(A) Allocates memory for int on stack


(B) Allocates memory for int on heap and returns pointer


(C) Declares int variable


(D) None of these



5. How do you deallocate memory allocated by new?

(A) free()


(B) delete


(C) remove()


(D) release()



6. How do you allocate an array of 5 integers on heap?

(A) int *arr = new int[5];


(B) int arr[5];


(C) int *arr = malloc(5);


(D) int *arr = new int;



7. How do you free memory allocated for an array?

(A) delete arr;


(B) delete[] arr;


(C) free(arr);


(D) release(arr);



8. What is the difference between NULL and nullptr?

(A) NULL is integer 0, nullptr is pointer type


(B) NULL is safer than nullptr


(C) nullptr is obsolete


(D) None



9. What happens if you delete a nullptr?

(A) Runtime error


(B) Compile-time error


(C) Safe, does nothing


(D) Undefined behavior



10. What is wrong with this code?
int *p = new int;
delete p;
delete p;

(A) Compile-time error


(B) Runtime error (double delete)


(C) Correct code


(D) Warning only



11. Which of the following is correct dynamic memory allocation for a double variable?

(A) double *d = new double;


(B) double d = new double;


(C) double *d = double();


(D) double *d = malloc(sizeof(double));



12. Which operator allocates memory and calls constructor for class objects?

(A) malloc


(B) new


(C) delete


(D) free



13. Which operator calls destructor and frees memory?

(A) free


(B) delete


(C) new


(D) malloc



14. What happens if you do not delete dynamically allocated memory?

(A) Memory leak


(B) Compile error


(C) Runtime error


(D) Nothing



15. Which is correct way to allocate memory for an array of objects?

(A) MyClass *arr = new MyClass[10];


(B) MyClass arr[10];


(C) MyClass *arr = malloc(10 * sizeof(MyClass));


(D) MyClass arr = new MyClass[10];



16. Can you assign nullptr to any pointer type?

(A) Yes


(B) No


(C) Only int pointers


(D) Only class pointers



17. What is output?
int *p = nullptr;
if(p) cout << "Not null";
else cout << "Null";

(A) Not null


(B) Null


(C) Garbage


(D) Compile error



18. Dynamic memory for objects of class is created using?

(A) malloc


(B) new


(C) calloc


(D) free



19. How do you delete a single object dynamically allocated?

(A) delete obj;


(B) delete[] obj;


(C) free(obj);


(D) release(obj);



20. What happens if delete[] is used on single object allocated by new?

(A) Correctly deletes object


(B) Undefined behavior


(C) Compile-time error


(D) Throws exception



21. How do you dynamically allocate a 2D array?

(A) int arr = new int[rows]; for(int i=0;i<rows;i++) arr[i] = new int[cols];


(B) int arr[rows][cols];


(C) int **arr = malloc(rowscols);


(D) int arr = new int[rowscols];



22. Can nullptr be compared with NULL?

(A) Yes


(B) No


(C) Only in C++11


(D) Only for pointers to int



23. What is advantage of nullptr over NULL?

(A) Type-safe


(B) Safer for overload resolution


(C) Avoids ambiguity


(D) All of the above



24. Which is correct syntax for new object with constructor?

(A) MyClass *obj = new MyClass();


(B) MyClass obj = new MyClass();


(C) MyClass *obj = malloc(sizeof(MyClass));


(D) MyClass *obj = MyClass();



25. What is wrong with code?
int *p;
delete p;

(A) p is uninitialized, delete causes undefined behavior


(B) Correct


(C) Compile-time error


(D) Deletes nullptr



26. What is correct way to free dynamically allocated 2D array?

(A) delete[] arr;


(B) for(i=0;i<rows;i++) delete[] arr[i]; delete[] arr;


(C) free(arr);


(D) delete arr;



27. Which of the following is NOT allowed?

(A) int *p = nullptr;


(B) delete nullptr;


(C) int a = nullptr;


(D) nullptr comparison with pointer



28. Which operator allocates memory but does NOT call constructor?

(A) new


(B) malloc


(C) new[]


(D) delete



29. Which of the following is valid initialization of pointer using nullptr?

(A) int *p = nullptr;


(B) char *c = nullptr;


(C) double *d = nullptr;


(D) All of the above



30. What is correct to prevent memory leak?

(A) Use delete/delete[] for every new/new[]


(B) Avoid using new


(C) Use malloc/free instead


(D) Assign nullptr to pointers



Exit mobile version