Site icon T4Tutorials.com

Dynamic memory allocation MCQs

1. : What is dynamic memory allocation?

(A) Allocating memory at compile time


(B) Allocating memory at runtime


(C) Allocating memory for global variables


(D) Allocating fixed-size memory



2. : Which function is used to allocate memory dynamically in C?

(A) malloc()


(B) alloc()


(C) reserve()


(D) new()



3. : What is the return type of the malloc() function?

(A) int


(B) void*


(C) char*


(D) float*



4. : What happens if malloc() fails to allocate memory?

(A) It returns NULL


(B) It returns 0


(C) It crashes the program


(D) It returns a garbage value



5. : Which function is used to free dynamically allocated memory in C?

(A) release()


(B) dealloc()


(C) free()


(D) dispose()



6. : What is the purpose of the calloc() function?

(A) To allocate memory for a single variable


(B) To allocate memory and initialize it to zero


(C) To free memory


(D) To reallocate memory



7. : What is the difference between malloc() and calloc()?

(A) malloc() initializes memory to zero


(B) calloc() allocates memory for a single variable


(C) malloc() does not initialize memory, while calloc() does


(D) There is no difference



8. : What does the realloc() function do?

(A) Allocates new memory


(B) Frees memory


(C) Changes the size of previously allocated memory


(D) Initializes memory to zero



9. : What is a potential risk of dynamic memory allocation?

(A) Memory leaks


(B) Stack overflow


(C) Underflow


(D) Array out of bounds



10. : Which of the following is true about dynamic memory?

(A) It is automatically deallocated


(B) It must be manually deallocated


(C) It is always faster than static memory


(D) It cannot be resized



11. : What is the purpose of the sizeof operator in dynamic memory allocation?

(A) To determine the size of the pointer


(B) To allocate memory for a pointer


(C) To determine the size of data types


(D) To free allocated memory



12. : What is the outcome of using free() on a pointer that is already freed?

(A) Undefined behavior


(B) Memory leak


(C) No effect


(D) Program termination



13. : In which scenario would you use dynamic memory allocation?

(A) When the size of the data is known at compile time


(B) When creating static arrays


(C) When the size of the data is determined at runtime


(D) When using global variables



14. : What is a common use case for dynamic memory allocation?

(A) Storing constants


(B) Managing arrays of unknown size


(C) Allocating memory for stack variables


(D) Defining global variables



15. : Which function is used to allocate memory for a specific number of elements in C?

(A) alloc()


(B) malloc()


(C) calloc()


(D) reserve()



16. : What happens if you forget to free dynamically allocated memory?

(A) The memory is automatically freed


(B) It causes a segmentation fault


(C) It leads to memory leaks


(D) It has no effect



17. : What is the difference between stack and heap memory?

(A) Stack memory is slower than heap memory


(B) Heap memory is automatically managed, stack is not


(C) Stack memory is used for dynamic allocation, heap is not


(D) Stack memory is used for function calls, heap is for dynamic allocation



18. : What is a memory leak?

(A) When memory is allocated but not freed


(B) When memory is freed too early


(C) When memory is allocated more than needed


(D) When memory is accessed after being freed



19. : What type of memory allocation does the new operator perform in C++?

(A) Static memory allocation


(B) Automatic memory allocation


(C) Dynamic memory allocation


(D) Stack memory allocation



20. : What is the purpose of the delete operator in C++?

(A) To allocate memory


(B) To free memory


(C) To resize memory


(D) To initialize memory



21. : What is the syntax to allocate an array of integers dynamically in C?

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


(B) int* arr = malloc(10 * sizeof(int));


(C) int arr[10];


(D) int arr = allocate(10 * sizeof(int));



22. : What will happen if you try to access memory that has already been freed?

(A) The program will run normally


(B) It will cause a memory leak


(C) It may cause undefined behavior


(D) It will print an error message



23. : How do you check if malloc() succeeded?

(A) Check if the pointer is not NULL


(B) Check if the pointer is -1


(C) Check if the pointer is 0


(D) Check if the pointer is greater than 0



24. : Which of the following can lead to fragmentation in dynamic memory allocation?

(A) Using realloc()


(B) Frequent allocation and deallocation


(C) Using malloc()


(D) Using calloc()



25. : What is the typical overhead for dynamic memory allocation?

(A) 0 bytes


(B) A few bytes per allocation


(C) A few kilobytes per allocation


(D) No overhead



26. : Which function can be used to determine the number of bytes allocated to a pointer in C?

(A) sizeof()


(B) sizeof(pointer)


(C) malloc()


(D) free()



27. : What does realloc() return if it fails to allocate the requested memory?

(A) NULL


(B) -1


(C) 0


(D) The original pointer



28. : In C++, what happens to dynamically allocated memory when the program exits?

(A) It is automatically freed


(B) It remains allocated


(C) It leads to memory leaks


(D) It is set to NULL



29. : Which of the following scenarios requires dynamic memory allocation?

(A) Fixed-size arrays


(B) Variable-length data structures


(C) Global variables


(D) Constant data



30. : What is the memory allocation technique called when the size of memory is not known beforehand?

(A) Static allocation


(B) Dynamic allocation


(C) Automatic allocation


(D) Global allocation



31. : Which of the following statements about memory allocation is false?

(A) Dynamic memory allocation allows for more flexible memory usage


(B) Static memory allocation is faster than dynamic allocation


(C) Dynamic memory allocation is more efficient for large data structures


(D) Dynamic memory allocation guarantees no fragmentation



32. : Which of the following functions is used for memory allocation in C++?

(A) alloc()


(B) new()


(C) malloc()


(D) allocate()



33. : What is the purpose of the new[] operator in C++?

(A) To allocate memory for a single object


(B) To allocate memory for an array of objects


(C) To free memory


(D) To check memory usage



34. : What will happen if you forget to use delete on a dynamically allocated object in C++?

(A) The program will run correctly


(B) The memory will be freed automatically


(C) It will lead to a memory leak


(D) The program will crash



35. : What is the main advantage of using dynamic memory allocation?

(A) It is faster than static allocation


(B) It provides flexibility in memory usage


(C) It uses less memory


(D) It is easier to implement



36. : In C, how do you allocate memory for a structure dynamically?

(A) struct* ptr = malloc(sizeof(struct));


(B) struct* ptr = new struct;


(C) struct* ptr = calloc(1, sizeof(struct));


(D) struct* ptr = allocate(sizeof(struct));



 

 

Data Structures MCQs

Basic Concepts

  1. Introduction to Data Structures
  2. Complexity Analysis MCQs

Linear Data Structures MCQs

  1. Arrays MCQs
  2. Linked Lists MCQs
  3. Stacks MCQs
  4. Queues MCQs

Non-Linear Data Structures MCQs

  1. Trees MCQs
  2. Heaps MCQs
  3. Graphs MCQs

Hashing MCQs MCQs

  1. Hash Tables

Sorting and Searching Algorithms MCQs 

  1. Sorting Algorithms MCQs
  2. Searching Algorithms MCQs

Miscellaneous

  1. Memory Management in data structures MCQs
  2. String Manipulation Algorithms MCQs
  1. Data Structures MCQs 1
  2. Data Structures MCQs 2
  3. Data Structures MCQs 3
  4. Data Structures MCQs 4
  5. Data Structures MCQs 5
  6. Stacks Solved MCQs
  7. Queues MCQs
  8. pointer mcqs
  9. Array MCQs

 

Exit mobile version