C questions in placement tests and university exams are rarely about syntax alone. Most of what you will find here is output prediction, pointer arithmetic, arrays and strings, structures and unions, storage classes, recursion, and file handling. The explanations walk through memory behaviour step by step, which is usually where the confusion sits rather than in the code itself.
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.2Medium
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.
Q.3Medium
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.4Medium
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.5Medium
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.6Medium
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.7Medium
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.8Medium
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.9Medium
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.
Q.10Medium
What is the purpose of typecasting in malloc()? int *p = (int*)malloc(sizeof(int));
Answer: C
Typecasting malloc() is optional in C but improves readability. It's mandatory in C++.
Q.11Medium
Analyze the memory leak in this code: void func() { int *p = malloc(100); if(condition) return; free(p); }
Answer: B
When condition is true, the function returns without calling free(), causing a memory leak.
Q.12Medium
What is the output of this code? int *p = calloc(3, sizeof(int)); printf("%d %d %d", p[0], p[1], p[2]);
Answer: B
calloc() initializes all allocated bytes to 0, so all integers are 0.
Q.13Medium
Consider reallocating memory: int *p = malloc(10); p = realloc(p, 20);
Answer: B
realloc() extends the existing allocation, preserving original data, and may return the same or different address.
Q.14Medium
Which allocation method is most suitable for a dynamically growing linked list?
Answer: B
Each node should be allocated individually with malloc() to allow dynamic growth and efficient memory usage.
Q.15Medium
How can you safely check if malloc() succeeded?
Answer: A
malloc() returns NULL (pointer value 0) on failure; always check before using the pointer.
Q.16Medium
In the context of dynamic memory, what does 'memory fragmentation' refer to?
Answer: A
Fragmentation occurs when memory is allocated and freed irregularly, leaving unused gaps that waste space.
Q.17Medium
What will happen if you try to allocate extremely large memory? int *p = malloc(INT_MAX);
Answer: B
malloc() returns NULL if allocation fails (insufficient memory); always check the return value.
Q.18Medium
Which best practice prevents memory leaks in complex programs?
Answer: C
Disciplined memory management with matching alloc/free pairs and proper error handling prevents leaks.
Q.19Medium
What does realloc() do if the new size is smaller than the old size?
Answer: B
realloc() shrinks or expands memory. If new size is smaller, it reduces allocation and returns pointer to resized block.
Q.20Medium
What will be the output of this code? int *p = (int*)malloc(5 * sizeof(int)); printf("%d", sizeof(p));
Answer: B
sizeof(p) returns the size of pointer itself (4 bytes on 32-bit, 8 bytes on 64-bit), not the allocated memory.