Which header file is required for dynamic memory allocation functions?
Answer: B
The <stdlib.h> header file contains declarations for malloc(), calloc(), realloc(), and free() functions.
Q.2Easy
What does calloc() do differently from malloc()?
Answer: B
calloc() takes two arguments (number of elements and size) and initializes all allocated bytes to zero, unlike malloc().
Q.3Medium
What will be the output of the following code?
int *ptr = malloc(sizeof(int));
if(ptr == NULL) printf("Failed");
else printf("Success");
Answer: B
In typical scenarios, malloc() successfully allocates memory and returns non-NULL pointer, printing 'Success'. NULL check is good practice for error handling.
Q.4Easy
Which function is used to free dynamically allocated memory?
Answer: C
free() is the standard function to deallocate memory previously allocated by malloc(), calloc(), or realloc().
Q.5Medium
What is a memory leak in C?
Answer: B
A memory leak occurs when allocated memory is not freed, consuming system resources without releasing them back.
Advertisement
Q.6Medium
What happens when you try to access memory after calling free()?
Answer: B
Accessing freed memory results in undefined behavior and is a critical bug. The memory may be reallocated for other purposes.
Q.7Medium
What is the purpose of realloc() function?
Answer: B
realloc() resizes a previously allocated memory block. It can increase or decrease the size and preserves existing data.
Q.8Medium
Which of the following is correct for dynamic array allocation?
Answer: B
Dynamic array allocation requires pointer and malloc() with proper size calculation. VLAs are not standard C.
Q.9Medium
What is double free error?
Answer: B
Double free error occurs when free() is called on the same memory block twice, causing undefined behavior and program crash.
Q.10Medium
Analyze this code:
char *str = malloc(5);
strcpy(str, "Hello");
How many bytes should malloc allocate for safety?
Answer: B
String "Hello" has 5 characters plus 1 null terminator (\0), requiring 6 bytes total.
Q.11Medium
What is the output?
int *p = malloc(sizeof(int) * 3);
p[0] = 1; p[1] = 2; p[2] = 3;
printf("%d", *(p+2));
Answer: C
*(p+2) is equivalent to p[2], which contains 3. Pointer arithmetic works with dynamic arrays.
Q.12Hard
What will happen if malloc() fails and returns NULL but code doesn't check?
Answer: B
Dereferencing a NULL pointer causes undefined behavior, typically resulting in segmentation fault or program crash.
Q.13Hard
Which statement about dynamic memory is TRUE?
Answer: B
Dynamic memory persists until free() is called, unlike automatic variables which are freed at function end.
Q.14Hard
What is the issue in this code?
void func() {
int *p = malloc(sizeof(int));
*p = 5;
}
int main() {
func();
// p is not accessible here
return 0;
}
Answer: B
The pointer p is local to func(). Memory is allocated but never freed, causing a memory leak.
Q.15Hard
What is the correct way to allocate memory for 2D dynamic array (3x4)?
Answer: B
For true 2D dynamic array, allocate pointer array first, then allocate each row. Option C is 1D linear allocation.
Q.16Hard
What does this realloc() call do?
int *p = malloc(5 * sizeof(int));
p = realloc(p, 10 * sizeof(int));
Answer: B
realloc() resizes the memory block while preserving existing data. If expansion in-place fails, it allocates new block and copies data.
Q.17Easy
How much memory is allocated by malloc(sizeof(int) * 10) on a 32-bit system where sizeof(int) = 4?
Answer: B
sizeof(int) * 10 = 4 * 10 = 40 bytes on a 32-bit system.
Q.18Easy
Which header file must be included to use malloc(), calloc(), and free() functions?
Answer: B
All memory allocation and deallocation functions are declared in <stdlib.h>.
Q.19Easy
What is the key difference between malloc() and calloc()?
Answer: B
calloc() takes two parameters (count, size) and initializes memory to 0, while malloc() takes one parameter and leaves memory uninitialized.
Q.20Medium
Which of the following correctly allocates memory for an array of 5 strings (each max 20 characters)?
Answer: D
Option B uses dynamic allocation (2D array), Option C uses static allocation. Both are valid but for different scenarios.