C Memory Management MCQ

In C, memory management is a crucial aspect. Using functions like malloc(), free(), and others, programmers can directly manage memory, ensuring optimal performance. However, mismanagement can lead to issues like memory leaks and undefined behaviors. This quiz will test your understanding of memory management in C programming. Ready to dive in?

Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which function is used to allocate memory in C?

A) alloc()
B) malloc()
C) mem()
D) allocate()

Answer:

B) malloc()

Explanation:

The malloc() function is used to allocate memory dynamically.

2. What does the free() function do?

A) Checks available memory
B) Allocates memory
C) Frees allocated memory
D) Resets memory

Answer:

C) Frees allocated memory

Explanation:

free() is used to release memory that was previously allocated by functions like malloc().

3. If you fail to release dynamically allocated memory, what might happen?

A) Memory Overflow
B) Memory Leak
C) Syntax Error
D) Logic Error

Answer:

B) Memory Leak

Explanation:

Not releasing allocated memory results in a memory leak where the memory remains occupied until the program ends.

4. Which function allocates memory and initializes it to zero?

A) zero()
B) malloc()
C) init()
D) calloc()

Answer:

D) calloc()

Explanation:

The calloc() function both allocates memory and initializes it to zero.

5. What is the main difference between malloc() and calloc()?

A) malloc() initializes memory to zero.
B) calloc() cannot allocate memory.
C) malloc() allocates single block, calloc() allocates multiple blocks.
D) No difference.

Answer:

C) malloc() allocates single block, calloc() allocates multiple blocks.

Explanation:

While both functions allocate memory, malloc() allocates a single block of memory, whereas calloc() allocates an array of blocks and initializes them to zero.

6. Which of the following is not related to memory management in C?

A) malloc()
B) free()
C) fopen()
D) realloc()

Answer:

C) fopen()

Explanation:

fopen() is used for file handling, not memory management.

7. What does realloc() do?

A) Releases all memory
B) Allocates memory and clears it
C) Re-allocates memory
D) Checks memory size

Answer:

C) Re-allocates memory

Explanation:

realloc() is used to resize previously allocated memory.

8. If malloc() fails to allocate memory, what does it return?

A) 0
B) -1
C) NULL
D) A garbage value

Answer:

C) NULL

Explanation:

If malloc() fails, it returns NULL.

9. Which of the following errors occur if the program tries to use a memory space that hasn't been allocated?

A) Compilation Error
B) Logical Error
C) Memory Leak
D) Segmentation Fault

Answer:

D) Segmentation Fault

Explanation:

Accessing memory that hasn't been allocated or is out of the allowed range results in a segmentation fault.

10. Which function allows you to duplicate a string and allocate the necessary memory for the copy?

A) strcpy()
B) strdup()
C) strcopy()
D) stralloc()

Answer:

B) strdup()

Explanation:

strdup() duplicates a string and allocates the necessary memory for the new copy.

11. Stack and Heap are:

A) Functions in C for memory allocation
B) Types of memory errors
C) Data structures in C
D) Memory areas used in C programs

Answer:

D) Memory areas used in C programs

Explanation:

The stack and the heap are memory areas where variables (local and dynamic respectively) are stored during a program's execution.

Congratulations on completing the quiz! Understanding memory management in C is essential for efficient programming and to avoid errors that could be hard to debug. Ensure you practice regularly, and soon these concepts will become second nature!


Comments