Dynamic memory allocation MCQsBy: Prof. Dr. Fazal Rehman | Last updated: May 14, 2025 36 Score: 0 Attempted: 0/36 Subscribe 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 Introduction to Data Structures Abstract Data Types (ADT) MCQs Complexity Analysis MCQs Time complexity MCQs Space complexity MCQs Big O, Big Ω, Big Θ notations MCQs Linear Data Structures MCQs Arrays MCQs One-dimensional arrays MCQs Multi-dimensional arrays MCQs Operations: traversal, insertion, deletion MCQs Linked Lists MCQs Singly linked list MCQs Doubly linked list MCQs Circular linked list MCQs Stacks MCQs Stack operations (push, pop, peek) MCQs Applications of stacks (expression evaluation, recursion) MCQs Queues MCQs Queue operations (enqueue, dequeue, front, rear) MCQs Types: Simple queue, circular queue, priority queue, deque MCQs Non-Linear Data Structures MCQs Trees MCQs Binary trees MCQs Binary Search Trees (BST) MCQs AVL Trees MCQs B-trees and B+ trees MCQs Tree traversal methods (in-order, pre-order, post-order) MCQs Heaps MCQs Min-heap MCQs Max-heap MCQs Heap operations (insertion, deletion, heapify) MCQs Applications of heaps (priority queues, heap sort) MCQs Graphs MCQs Graph representation (adjacency matrix, adjacency list) MCQs Graph traversal algorithms (DFS, BFS) MCQs Shortest path algorithms (Dijkstra’s, Bellman-Ford) MCQs Minimum Spanning Tree (Kruskal’s, Prim’s) MCQs Hashing MCQs MCQs Hash Tables Hash functions MCQs Collision resolution techniques (chaining, open addressing) MCQs Applications of hashing MCQs Sorting and Searching Algorithms MCQs Sorting Algorithms MCQs Bubble sort MCQs Selection sort MCQs Insertion sort MCQs Merge sort MCQs Quick sort MCQs Heap sort MCQs Searching Algorithms MCQs Linear search MCQs Binary search MCQs Interpolation search MCQs Miscellaneous Memory Management in data structures MCQs Dynamic memory allocation MCQs Garbage collection MCQs String Manipulation Algorithms MCQs Pattern matching (KMP, Rabin-Karp) MCQs String hashing MCQs Data Structures MCQs 1 Data Structures MCQs 2 Data Structures MCQs 3 Data Structures MCQs 4 Data Structures MCQs 5 Stacks Solved MCQs Queues MCQs pointer mcqs Array MCQs Related Posts:Dynamic memory allocation and garbage collection(MCQs)Memory Allocation and Deallocation MCQsShared Memory vs Distributed Memory MCQsDifference Between Primary Memory and Secondary MemoryResource allocation MCQs - Software Project ManagementRegister allocation and assignment(MCQs)