Home Subjects C Programming Dynamic Memory

C Programming
Dynamic Memory

C language from basics to advanced placement prep

53 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 53
Topics in C Programming
Q.31 Medium Dynamic Memory
What happens if you use a pointer after calling free() on it (without reallocating)?
A Program runs normally
B Pointer becomes NULL
C Use-after-free: undefined behavior, possible crash or data corruption
D Compiler automatically prevents it
Correct Answer:  C. Use-after-free: undefined behavior, possible crash or data corruption
EXPLANATION

After free(), accessing the pointer causes undefined behavior as memory is returned to the heap and may be reused.

Test
Q.32 Medium Dynamic Memory
In dynamic 2D array creation:
int **arr = (int**)malloc(m * sizeof(int*));
what does the first malloc() allocate?
A m rows of integers
B An array of m pointers to int
C m*n integers
D Memory for entire 2D array at once
Correct Answer:  B. An array of m pointers to int
EXPLANATION

First malloc allocates an array of m pointers. Each pointer must be individually allocated in a loop for actual row data.

Test
Q.33 Medium Dynamic Memory
Which statement about free() is correct?
A free() must always be called exactly once per malloc()
B free() sets the pointer to NULL automatically
C Calling free() twice causes undefined behavior
D free() returns 1 on success, 0 on failure
Correct Answer:  C. Calling free() twice causes undefined behavior
EXPLANATION

Double-free causes undefined behavior (segmentation fault). free() doesn't set pointer to NULL; programmer must do it explicitly.

Test
Q.34 Medium Dynamic Memory
What will be the output of this code?
int *p = (int*)malloc(5 * sizeof(int));
printf("%d", sizeof(p));
A 20
B 4 (or 8 on 64-bit systems)
C 5
D Undefined
Correct Answer:  B. 4 (or 8 on 64-bit systems)
EXPLANATION

sizeof(p) returns the size of pointer itself (4 bytes on 32-bit, 8 bytes on 64-bit), not the allocated memory.

Test
Q.35 Medium Dynamic Memory
What does realloc() do if the new size is smaller than the old size?
A Increases the memory block
B Shrinks the memory block and returns a pointer
C Frees extra memory automatically
D Returns NULL
Correct Answer:  B. Shrinks the memory block and returns a pointer
EXPLANATION

realloc() shrinks or expands memory. If new size is smaller, it reduces allocation and returns pointer to resized block.

Test
Q.36 Medium Dynamic Memory
Which best practice prevents memory leaks in complex programs?
A Allocate all memory at program start
B Never use dynamic allocation
C Track allocations and ensure every malloc() has a corresponding free()
D Use realloc() instead of free()
Correct Answer:  C. Track allocations and ensure every malloc() has a corresponding free()
EXPLANATION

Disciplined memory management with matching alloc/free pairs and proper error handling prevents leaks.

Test
Q.37 Medium Dynamic Memory
What will happen if you try to allocate extremely large memory?
int *p = malloc(INT_MAX);
A It will always succeed
B It will return NULL on failure, and you should check for this
C It will crash immediately
D Compiler error
Correct Answer:  B. It will return NULL on failure, and you should check for this
EXPLANATION

malloc() returns NULL if allocation fails (insufficient memory); always check the return value.

Test
Q.38 Medium Dynamic Memory
In the context of dynamic memory, what does 'memory fragmentation' refer to?
A Breaking memory into chunks that cannot be used efficiently
B Allocating too much memory at once
C Using calloc() instead of malloc()
D Accessing freed memory
Correct Answer:  A. Breaking memory into chunks that cannot be used efficiently
EXPLANATION

Fragmentation occurs when memory is allocated and freed irregularly, leaving unused gaps that waste space.

Test
Q.39 Medium Dynamic Memory
How can you safely check if malloc() succeeded?
A Check if pointer equals 0 or NULL
B Dereference the pointer immediately
C Use sizeof() on the pointer
D Compare with void pointer
Correct Answer:  A. Check if pointer equals 0 or NULL
EXPLANATION

malloc() returns NULL (pointer value 0) on failure; always check before using the pointer.

Test
Q.40 Medium Dynamic Memory
Which allocation method is most suitable for a dynamically growing linked list?
A Static array allocation
B malloc() for each node as needed
C calloc() all nodes upfront
D Stack allocation
Correct Answer:  B. malloc() for each node as needed
EXPLANATION

Each node should be allocated individually with malloc() to allow dynamic growth and efficient memory usage.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips