Memory Allocation in C Programming MCQ Questions and Answers

Q1. Which memory allocation type occurs automatically when a function is called?
A) Static memory allocation
B) Automatic memory allocation
C) Dynamic memory allocation
D) Register memory allocation
Answer: B) Automatic memory allocation

Q2. Which of the following is responsible for allocating memory at compile time?
A) Static memory allocation
B) Dynamic memory allocation
C) Heap memory allocation
D) Automatic memory allocation
Answer: A) Static memory allocation

Q3. Memory for global variables in C is allocated in which segment?
A) Stack
B) Data segment
C) Heap
D) Code segment
Answer: B) Data segment

Q4. Which keyword in C is used for dynamic memory allocation?
A) static
B) malloc()
C) auto
D) extern
Answer: B) malloc()

Q5. What will happen if malloc() fails to allocate memory?
A) Program crashes immediately
B) It returns NULL
C) It raises an exception
D) It allocates random memory
Answer: B) It returns NULL

Q6. Memory allocated using malloc() is stored in which area?
A) Stack
B) Data segment
C) Heap
D) Text segment
Answer: C) Heap

Q7. Which function allocates and initializes memory to zero in C?
A) malloc()
B) calloc()
C) realloc()
D) free()
Answer: B) calloc()

Q8. The realloc() function is used to:
A) Allocate new memory block
B) Resize an existing memory block
C) Free a memory block
D) Initialize a pointer
Answer: B) Resize an existing memory block

Q9. The memory of local variables in C is stored in:
A) Stack segment
B) Heap segment
C) Data segment
D) ROM
Answer: A) Stack segment

Q10. What is the correct prototype of malloc() function?
A) void malloc(int);
B) int malloc(void*);
C) void malloc(size_t size);*
D) char malloc(int size);
Answer: C) void* malloc(size_t size);

Q11. Which function is used to deallocate memory dynamically in C?
A) delete()
B) remove()
C) free()
D) clear()
Answer: C) free()

Q12. Which of the following functions is NOT used for memory allocation?
A) malloc()
B) calloc()
C) memset()
D) realloc()
Answer: C) memset()

Q13. Static memory allocation happens during which phase?
A) Compile time
B) Execution time
C) Run time
D) Link time
Answer: A) Compile time

Q14. Dynamic memory allocation happens during which phase?
A) Compile time
B) Run time
C) Load time
D) Link time
Answer: B) Run time

Q15. What is the return type of calloc()?
A) int
B) char*
C) void*
D) double*
Answer: C) void*

Q16. Which statement is TRUE regarding dynamic memory allocation?
A) Memory is allocated on stack
B) Memory is allocated on heap
C) Memory size is fixed at compile time
D) No manual deallocation is required
Answer: B) Memory is allocated on heap

Q17. Which function allows you to allocate multiple blocks of memory at once?
A) calloc()
B) malloc()
C) realloc()
D) reserve()
Answer: A) calloc()

Q18. What happens when you call free() twice on the same pointer?
A) Nothing happens
B) Undefined behavior
C) The pointer becomes NULL
D) It reallocates memory
Answer: B) Undefined behavior

Q19. Which of the following is the correct syntax for freeing memory?
A) delete(ptr);
B) remove(ptr);
C) free(ptr);
D) clear(ptr);
Answer: C) free(ptr);

Q20. When does automatic memory get deallocated?
A) When free() is called
B) When system memory is full
C) When the function exits
D) When variable is initialized
Answer: C) When the function exits

Q21. Which type of variable is stored in the heap?
A) Global variables
B) Local variables
C) Dynamically allocated variables
D) Static variables
Answer: C) Dynamically allocated variables

Q22. The keyword static inside a function causes:
A) Dynamic allocation
B) Local variable to retain its value between function calls
C) Global variable creation
D) Variable stored on heap
Answer: B) Local variable to retain its value between function calls

Q23. Which function can both increase and decrease memory size?
A) malloc()
B) calloc()
C) realloc()
D) memcpy()
Answer: C) realloc()

Q24. What does sizeof() operator return?
A) Address of variable
B) Size of operand in bytes
C) Value of variable
D) Type of variable
Answer: B) Size of operand in bytes

Q25. Which header file is required for dynamic memory allocation functions?
A) stdio.h
B) stdlib.h
C) malloc.h
D) string.h
Answer: B) stdlib.h

Q26. Which memory allocation is managed automatically by the compiler?
A) Automatic memory allocation
B) Static memory allocation
C) Dynamic memory allocation
D) Manual memory allocation
Answer: A) Automatic memory allocation

Q27. Which segment of memory stores function call information like return address and local variables?
A) Heap
B) Stack
C) Data
D) Text
Answer: B) Stack

Q28. Global variables are allocated memory in which memory segment?
A) Heap
B) Data segment
C) Stack
D) ROM
Answer: B) Data segment

Q29. Which of the following is TRUE for heap memory?
A) Memory is automatically released
B) Memory is fixed during compile time
C) Memory must be manually freed using free()
D) Memory is stored in stack
Answer: C) Memory must be manually freed using free()

Q30. What does the function realloc(ptr, 0) effectively do?
A) Expands memory
B) Frees the memory block pointed by ptr
C) Returns same pointer
D) Creates a new block without freeing
Answer: B) Frees the memory block pointed by ptr

Q31. When memory is allocated using malloc(), it contains:
A) Garbage values
B) Undefined (uninitialized) values
C) Zeroes
D) Previous data
Answer: B) Undefined (uninitialized) values

Q32. Which statement about calloc() is true?
A) It returns NULL always
B) It allocates uninitialized memory
C) It allocates zero-initialized memory
D) It can’t allocate arrays
Answer: C) It allocates zero-initialized memory

Q33. What will happen if a pointer returned by malloc() is not freed?
A) Compiler error
B) Program crash
C) Memory leak
D) Segmentation fault
Answer: C) Memory leak

Q34. Which of the following functions can change the size of an allocated memory block?
A) malloc()
B) calloc()
C) realloc()
D) memcpy()
Answer: C) realloc()

Q35. The free() function:
A) Deletes pointer variable
B) Initializes pointer to NULL
C) Deallocates heap memory
D) Frees stack memory
Answer: C) Deallocates heap memory

Q36. The keyword extern affects memory allocation by:
A) Allocating memory twice
B) Declaring a variable without defining it
C) Allocating memory at runtime
D) Making memory static
Answer: B) Declaring a variable without defining it

Q37. Which type of memory allocation allows variable size arrays?
A) Static
B) Dynamic
C) Automatic
D) Constant
Answer: B) Dynamic

Q38. The heap grows ______ while the stack grows ______ in memory.
A) Upward, upward
B) Upward, downward
C) Downward, upward
D) Downward, downward
Answer: B) Upward, downward

Q39. The memory area where executable code is stored is called:
A) Stack
B) Text segment
C) Heap
D) Static segment
Answer: B) Text segment

Q40. What is the correct way to allocate memory for an array of 10 integers using malloc()?
A) int *p = malloc(10);
B) int p = (int) malloc(10 * sizeof(int));
C) int *p = malloc(sizeof(int));
D) int *p = malloc(int);
Answer: B) int p = (int) malloc(10 * sizeof(int));

Q41. Which of the following statements is TRUE about static variables inside functions?
A) They are stored on stack
B) They retain their value between function calls
C) They are destroyed when function ends
D) They are allocated dynamically
Answer: B) They retain their value between function calls

Q42. If malloc() fails to allocate memory, it returns:
A) 0
B) -1
C) NULL
D) Uninitialized pointer
Answer: C) NULL

Q43. Which of these can lead to a segmentation fault?
A) Using sizeof()
B) Accessing freed memory
C) Allocating small memory
D) Using NULL pointer in printf()
Answer: B) Accessing freed memory

Q44. The lifetime of a variable declared with auto keyword is:
A) Limited to the function in which it is declared
B) Program lifetime
C) Until system restarts
D) Global scope
Answer: A) Limited to the function in which it is declared

Q45. In dynamic memory allocation, failure to free unused memory results in:
A) Runtime error
B) Memory leak
C) Compile-time warning
D) Data corruption
Answer: B) Memory leak

Q46. Which function can initialize multiple blocks of memory simultaneously?
A) malloc()
B) calloc()
C) realloc()
D) memset()
Answer: B) calloc()

Q47. Which pointer operation is safe after freeing memory?
A) Using pointer in printf()
B) Assigning pointer to NULL
C) Dereferencing pointer
D) Calling free() again
Answer: B) Assigning pointer to NULL

Q48. Which of the following functions cannot allocate memory dynamically?
A) memset()
B) calloc()
C) malloc()
D) realloc()
Answer: A) memset()

Q49. What is the main difference between stack and heap memory?
A) Both are static
B) Stack is automatically managed, heap is manually managed
C) Heap is smaller than stack
D) Stack stores only integers
Answer: B) Stack is automatically managed, heap is manually managed

Q50. The memory of static variables is allocated in:
A) Heap segment
B) Data segment
C) Stack segment
D) Code segment
Answer: B) Data segment

Q51. Which of the following is a consequence of improper memory deallocation?
A) Faster execution
B) Memory leak
C) Reduced stack size
D) Automatic garbage collection
Answer: B) Memory leak

Q52. The process of breaking available memory into non-contiguous blocks is called:
A) Paging
B) Fragmentation
C) Compaction
D) Segmentation
Answer: B) Fragmentation

Q53. What does realloc() return when it fails to allocate the requested memory?
A) Garbage address
B) NULL
C) Zero
D) Old pointer unchanged
Answer: B) NULL

Q54. Which type of memory allocation allows resizing at runtime?
A) Static
B) Dynamic
C) Automatic
D) Constant
Answer: B) Dynamic

Q55. What happens if free() is called with a NULL pointer?
A) Causes segmentation fault
B) Memory leak
C) Nothing happens
D) Invalid pointer operation
Answer: C) Nothing happens

Q56. Which of the following is not true about dynamic memory allocation?
A) Uses functions from stdlib.h
B) Managed automatically by the compiler
C) Uses heap area
D) Requires manual deallocation
Answer: B) Managed automatically by the compiler

Q57. The expression sizeof(char) always returns:
A) 2
B) 4
C) 1
D) Depends on compiler
Answer: C) 1

Q58. If you allocate memory using malloc() and forget to call free(), what happens?
A) Stack overflow
B) Memory leak
C) Program crash
D) Undefined variable
Answer: B) Memory leak

Q59. Accessing memory that has already been freed results in:
A) Safe execution
B) Undefined behavior
C) Compile-time error
D) Memory recycling
Answer: B) Undefined behavior

Q60. Which statement about realloc() is FALSE?
A) It can reduce memory size
B) It always preserves original pointer after failure
C) It can expand memory block
D) It can allocate new block if needed
Answer: B) It always preserves original pointer after failure

Q61. How is the stack memory managed in C?
A) Programmer manually manages
B) Managed automatically by compiler
C) Allocated on heap
D) Controlled by operating system only
Answer: B) Managed automatically by compiler

Q62. Which memory area stores string literals in C?
A) Heap
B) Read-only data segment
C) Stack
D) RAM directly
Answer: B) Read-only data segment

Q63. What is the result of freeing memory twice?
A) Memory saved
B) Undefined behavior
C) No effect
D) Memory reset
Answer: B) Undefined behavior

Q64. Which function can dynamically allocate memory and zero-initialize it at the same time?
A) malloc()
B) calloc()
C) realloc()
D) memset()
Answer: B) calloc()

Q65. When dynamic memory is allocated, who manages that portion of memory?
A) Compiler
B) Hardware
C) Programmer
D) OS automatically
Answer: C) Programmer

Q66. Which of the following is NOT a valid reason for memory allocation failure?
A) Insufficient memory
B) Incorrect syntax
C) Fragmentation
D) Exhausted heap
Answer: B) Incorrect syntax

Q67. What is the size of a NULL pointer on a 64-bit machine?
A) 2 bytes
B) 4 bytes
C) 8 bytes
D) Depends on compiler only
Answer: C) 8 bytes

Q68. Which of these is a runtime error in C related to memory allocation?
A) Missing semicolon
B) Type mismatch
C) Accessing memory after free()
D) Undefined macro
Answer: C) Accessing memory after free()

Q69. How many arguments does calloc() take?
A) 1
B) 2
C) 3
D) 4
Answer: B) 2

Q70. Which function reallocates memory without losing existing data?
A) malloc()
B) calloc()
C) realloc()
D) memset()
Answer: C) realloc()

Q71. Which variable’s lifetime ends only after the entire program ends?
A) Local variable
B) Dynamic variable
C) Global variable
D) Register variable
Answer: C) Global variable

Q72. Which of the following causes a “dangling pointer”?
A) Forgetting to declare pointer
B) Freeing memory but still using the pointer
C) Assigning NULL to pointer
D) Declaring static pointer
Answer: B) Freeing memory but still using the pointer

Q73. Which type of memory allocation leads to variable size at runtime?
A) Static
B) Dynamic
C) Automatic
D) Constant
Answer: B) Dynamic

Q74. What does the sizeof operator evaluate at?
A) Runtime
B) Compile time
C) Link time
D) Execution completion
Answer: B) Compile time

Q75. Which kind of memory allocation is automatically freed after function execution ends?
A) Static memory allocation
B) Dynamic memory allocation
C) Automatic memory allocation
D) Global memory allocation
Answer: C) Automatic memory allocation

Q76. Which function in C can dynamically increase or decrease the size of an allocated memory block?
A) malloc()
B) calloc()
C) realloc()
D) memcpy()
Answer: C) realloc()

Q77. Memory allocated to local variables of a function is freed when:
A) The program terminates
B) A variable goes out of scope
C) The function returns
D) User calls free()
Answer: C) The function returns

Q78. What is the main disadvantage of dynamic memory allocation in C?
A) Slow compilation
B) No flexibility
C) Possibility of memory leaks
D) Stack overflow
Answer: C) Possibility of memory leaks

Q79. What is the correct way to allocate memory for a string of 20 characters in C?
A) char *s = malloc(20);
B) char s = (char) malloc(20 * sizeof(char));
C) char s[20];
D) char *s = calloc(1, 10);
Answer: B) char s = (char) malloc(20 * sizeof(char));

Q80. Which statement correctly describes stack memory?
A) Grows upward in memory
B) Must be manually freed
C) Follows LIFO (Last In First Out) structure
D) Stores dynamic variables only
Answer: C) Follows LIFO (Last In First Out) structure

Q81. What is the purpose of memset() function?
A) To set a block of memory to a specific value
B) To allocate memory
C) To release memory
D) To move memory blocks
Answer: A) To set a block of memory to a specific value

Q82. Which function should be used to allocate a 2D array dynamically?
A) malloc()
B) realloc()
C) Nested malloc() calls
D) memset()
Answer: C) Nested malloc() calls

Q83. Which function can prevent memory fragmentation if used correctly?
A) malloc()
B) calloc()
C) realloc()
D) memset()
Answer: C) realloc()

Q84. What happens if free() is not called for dynamically allocated memory?
A) Program crash
B) Automatic release
C) Memory leak occurs
D) Stack corruption
Answer: C) Memory leak occurs

Q85. What is the correct return type of realloc()?
A) int
B) void*
C) float*
D) char
Answer: B) void*

Q86. Which of the following is not a valid memory allocation function in C?
A) malloc()
B) calloc()
C) new()
D) realloc()
Answer: C) new()

Q87. What is a wild pointer?
A) Pointer initialized to NULL
B) Pointer not initialized at all
C) Pointer freed after use
D) Pointer to constant
Answer: B) Pointer not initialized at all

Q88. Which function can copy data from one memory block to another?
A) malloc()
B) memcpy()
C) realloc()
D) free()
Answer: B) memcpy()

Q89. The memory layout of a C program includes which of the following in correct order (bottom to top)?
A) Heap → Stack → Data → Code
B) Code → Data → Heap → Stack
C) Code → Data → Heap → Stack
D) Stack → Data → Code → Heap
Answer: C) Code → Data → Heap → Stack

Q90. The pointer returned by malloc() should always be checked for:
A) Zero value
B) NULL value
C) Garbage value
D) Stack overflow
Answer: B) NULL value

Q91. What happens if you allocate memory and lose its pointer reference before freeing?
A) The OS automatically frees it
B) It causes compile-time error
C) It leads to a memory leak
D) The memory is reallocated
Answer: C) It leads to a memory leak

Q92. Which function is used to release dynamically allocated memory?
A) release()
B) delete()
C) free()
D) remove()
Answer: C) free()

Q93. What does the following code do?
int *p = NULL;
p = (int*)malloc(sizeof(int));
*p = 100;
A) Causes runtime error
B) Allocates memory and assigns 100 to it
C) Crashes the program
D) Produces garbage value
Answer: B) Allocates memory and assigns 100 to it

Q94. A segmentation fault can occur when:
A) Pointer arithmetic is correct
B) Dereferencing a NULL or invalid pointer
C) Allocating memory with malloc()
D) Using sizeof() operator
Answer: B) Dereferencing a NULL or invalid pointer

Q95. When memory is allocated dynamically, where is it taken from?
A) Stack
B) Register
C) Heap
D) Data segment
Answer: C) Heap

Q96. What should be done after calling free(ptr)?
A) Do nothing
B) Call free() again
C) Assign ptr = NULL
D) Reallocate immediately
Answer: C) Assign ptr = NULL

Q97. Which statement about stack memory is FALSE?
A) It is limited in size
B) It stores local variables
C) It must be freed manually
D) It grows and shrinks automatically
Answer: C) It must be freed manually

Q98. The function calloc(5, sizeof(int)) allocates memory for how many bytes?
A) 4 bytes
B) 20 bytes
C) 10 bytes
D) Depends on system
Answer: B) 20 bytes

Q99. Which is an invalid use of malloc()?
A) malloc(100)
B) malloc(sizeof(int) * 5)
C) malloc(char)
D) malloc(1)
Answer: C) malloc(char)

Q100. Which statement about memory allocation in C is correct?
A) Static and dynamic allocations both occur at runtime
B) Stack memory must be freed using free()
C) Static allocation happens at compile time; dynamic at runtime
D) Automatic memory is stored in heap
Answer: C) Static allocation happens at compile time; dynamic at runtime