C- Memory Allocation Questions and Answers

Practice Mode
Showing 10 of 28 questions
Q1
What function should be used to free the memory allocated by calloc() ?
  • A dealloc();
  • B malloc(variable_name, 0)
  • C free();
  • D memalloc(variable_name, 0)
Answer: Option C
Q2
How will you free the memory allocated by the following program? #include<stdio.h> #include<stdlib.h> #define MAXROW 3 #define MAXCOL 4 int main() {     int **p, i, j;     p = (int **) malloc(MAXROW * sizeof(int*));     return 0; }
  • A memfree(int p);
  • B dealloc(p);
  • C malloc(p, 0);
  • D free(p);
Answer: Option D
Q3
Specify the 2 library functions to dynamically allocate memory?
  • A malloc() and memalloc()
  • B alloc() and memalloc()
  • C malloc() and calloc()
  • D memalloc() and faralloc()
Answer: Option C
Q4
Which function is used to allocate memory dynamically in C?
  • A malloc()
  • B alloc()
  • C new()
  • D create()
Answer: Option A
Explanation: malloc() is used to allocate memory dynamically at runtime.
Q5
What does the calloc() function do differently from malloc()?
  • A Initializes memory to zero
  • B Allocates memory on stack
  • C Automatically frees memory
  • D Allocates memory for functions
Answer: Option A
Explanation: calloc() initializes allocated memory to zero, while malloc() leaves it uninitialized.
Q6
Which function is used to free dynamically allocated memory?
  • A free()
  • B dealloc()
  • C release()
  • D delete()
Answer: Option A
Explanation: free() is used to deallocate memory that was previously allocated by malloc(), calloc(), or realloc().
Q7
What is the purpose of the realloc() function?
  • A Resizes allocated memory
  • B Reallocates stack memory
  • C Relocates program in memory
  • D Reinitializes memory
Answer: Option A
Explanation: realloc() is used to resize previously allocated memory block, either expanding or shrinking it.
Q8
What happens if you try to free a NULL pointer?
  • A Nothing, it is safe
  • B Program crashes
  • C Memory leak occurs
  • D Undefined behavior
Answer: Option A
Explanation: free(NULL) is safe and does nothing according to C standard.
Q9
Which header file is required for dynamic memory allocation functions?
  • A stdlib.h
  • B stdio.h
  • C memory.h
  • D alloc.h
Answer: Option A
Explanation: stdlib.h contains declarations for malloc(), calloc(), realloc(), and free().
Q10
What is memory leak in C?
  • A Memory not freed after use
  • B Memory allocated too slowly
  • C Memory corrupted by virus
  • D Memory allocation failure
Answer: Option A
Explanation: Memory leak occurs when allocated memory is not freed, causing gradual memory consumption.
Questions and Answers for Competitive Exams Various Entrance Test