Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

499 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 81–90 of 499
Topics in C Programming
Q.81 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.

Take Test
Q.82 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.

Take Test
Q.83 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.

Take Test
Q.84 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.

Take Test
Q.85 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.

Take Test
Q.86 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.

Take Test
Q.87 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.

Take Test
Q.88 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.

Take Test
Q.89 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.

Take Test
Q.90 Medium Dynamic Memory
Consider reallocating memory:
int *p = malloc(10);
p = realloc(p, 20);
A The original 10 bytes are lost
B realloc() preserves existing data and allocates additional memory
C realloc() always returns a new address
D Memory at address p is deallocated first
Correct Answer:  B. realloc() preserves existing data and allocates additional memory
EXPLANATION

realloc() extends the existing allocation, preserving original data, and may return the same or different address.

Take Test
IGET
iget AI
Online · Ask anything about exams
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