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
Answer: B) Allocating memory at runtime
Which function is used to allocate memory dynamically in C?
A) malloc()
B) alloc()
C) reserve()
D) new()
Answer: A) malloc()
What is the return type of the malloc() function?
A) int
B) void*
C) char*
D) float*
Answer: B) void*
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
Answer: A) It returns NULL
Which function is used to free dynamically allocated memory in C?
A) release()
B) dealloc()
C) free()
D) dispose()
Answer: C) free()
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
Answer: B) To allocate memory and initialize it to zero
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
Answer: C) malloc() does not initialize memory, while calloc() does
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
Answer: C) Changes the size of previously allocated memory
What is a potential risk of dynamic memory allocation?
A) Memory leaks
B) Stack overflow
C) Underflow
D) Array out of bounds
Answer: A) Memory leaks
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
Answer: B) It must be manually deallocated
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
Answer: C) To determine the size of data types
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
Answer: A) Undefined behavior
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
Answer: C) When the size of the data is determined at runtime
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
Answer: B) Managing arrays of unknown size
Which function is used to allocate memory for a specific number of elements in C?
A) alloc()
B) malloc()
C) calloc()
D) reserve()
Answer: C) calloc()
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
Answer: C) It leads to memory leaks
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
Answer: D) Stack memory is used for function calls, heap is for dynamic allocation
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
Answer: A) When memory is allocated but not freed
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
Answer: C) Dynamic memory allocation
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
Answer: B) To free memory
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));
Answer: B) int arr = malloc(10 * sizeof(int));*
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
Answer: C) It may cause undefined behavior
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
Answer: A) Check if the pointer is not NULL
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()
Answer: B) Frequent allocation and deallocation
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
Answer: B) A few bytes per allocation
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()
Answer: A) sizeof()
What does realloc() return if it fails to allocate the requested memory?
A) NULL
B) -1
C) 0
D) The original pointer
Answer: A) NULL
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
Answer: A) It is automatically freed
Which of the following scenarios requires dynamic memory allocation?
A) Fixed-size arrays
B) Variable-length data structures
C) Global variables
D) Constant data
Answer: B) Variable-length data structures
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
Answer: B) Dynamic allocation
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
Answer: D) Dynamic memory allocation guarantees no fragmentation
Which of the following functions is used for memory allocation in C++?
A) alloc()
B) new()
C) malloc()
D) allocate()
Answer: B) new()
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
Answer: B) To allocate memory for an array of objects
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
Answer: C) It will lead to a memory leak
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
Answer: B) It provides flexibility in memory usage
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));
Answer: A) struct ptr = malloc(sizeof(struct));*
Basic Concepts
Non-Linear Data Structures MCQs
Sorting and Searching Algorithms 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