How much memory in bytes does calloc(10, 4) allocate?
A 10 bytes B 4 bytes C 40 bytes D 10.4 bytes
calloc(num, size) allocates num * size = 10 * 4 = 40 bytes total.
Which scenario best demonstrates a memory leak?
A char *s = malloc(10); free(s); B char *s = malloc(10); s = malloc(20); C char *s = malloc(10); free(s); free(s); D char *s = NULL; free(s);
Original malloc(10) pointer is lost before freeing. The 10 bytes are now inaccessible - a memory leak.
What is the output of this code? int *p = (int*)calloc(3, sizeof(int)); printf("%d %d %d", p[0], p[1], p[2]);
A Garbage values B 0 0 0 C 3 bytes each D Compiler error
calloc() initializes all allocated memory to zero, so all elements will be 0.
If realloc() cannot expand memory at the current location, what does it do?
A Fails and returns NULL B Allocates new memory, copies old data, frees old memory, returns new pointer C Extends memory anyway D Keeps old pointer valid
realloc() finds new space if needed, copies data, deallocates old block, and returns new pointer.
Consider this code: int *p = malloc(sizeof(int)); int *q = p; free(p); q[0] = 5; // What is the result?
A Valid assignment B Compiler error C Use-after-free: undefined behavior D Memory automatically reallocated
Both p and q point to freed memory. Writing to q after free(p) is undefined behavior.
In competitive exams, why is dynamic allocation preferred over static arrays for unknown input sizes?
A It's faster than static arrays B Allows allocation only as much memory as needed, preventing stack overflow C Static arrays don't work in C D It's automatically freed by compiler
Dynamic allocation allocates memory on heap based on runtime input, preventing fixed-size limitations and stack overflow.
What is the correct way to allocate a dynamic array of struct for 'n' elements? struct Node { int data; char name[20]; };
A struct Node *arr = malloc(n); B struct Node *arr = malloc(n * sizeof(struct Node)); C struct Node arr = malloc(n); D struct Node *arr = calloc(n, 1);
Must allocate n * sizeof(struct Node) bytes to hold all n structures.
What distinguishes a dangling pointer?
A A pointer that points to deallocated memory B A pointer initialized to NULL C A pointer that hasn't been malloc'd D A pointer that points to stack variable
Dangling pointer points to memory that has been freed. Accessing it causes undefined behavior.
For a program handling 10^6 integers dynamically, which allocation is most appropriate?
A int arr[1000000]; B int *arr = malloc(1000000 * sizeof(int)); C static int arr[1000000]; D auto int arr[1000000];
Stack allocation (option A) will cause stack overflow. Dynamic allocation on heap is required for large data.
What is the typical behavior if malloc() is called in a loop without corresponding free() calls?
A Program automatically clears memory B Memory leaks accumulate, eventually causing program crash C Compiler optimizes and prevents leaks D Memory is recycled automatically
Without freeing, allocated memory accumulates until system runs out of heap space, causing allocation failure or crash.
Which header file must be included to use dynamic memory allocation functions in C?
A #include <stdlib.h> B #include <stdio.h> C #include <string.h> D #include <math.h>
stdlib.h contains declarations for malloc(), calloc(), realloc(), and free() functions.
What does free() function do in C?
A Allocates memory from heap B Deallocates previously allocated memory C Copies memory from one location to another D Initializes memory with zeros
free() is used to deallocate memory that was previously allocated using malloc(), calloc(), or realloc().
What is the primary difference between malloc() and calloc()?
A malloc() is faster than calloc() B calloc() initializes memory with zeros, malloc() does not C malloc() allocates from stack, calloc() from heap D calloc() requires only one argument
calloc() allocates memory and initializes all bytes to zero, while malloc() leaves the allocated memory uninitialized.
Which of the following correctly allocates memory for an array of 5 integers and initializes to zero?
A int *p = malloc(5 * sizeof(int)); B int *p = calloc(5, sizeof(int)); C int *p = realloc(NULL, 5); D int p[5] = {0};
calloc(5, sizeof(int)) allocates memory for 5 integers and initializes all to zero.
A competitive programmer allocates a 2D array dynamically. What is the correct approach?
A int arr = (int )malloc(rows * sizeof(int*)); for(i=0; i<rows; i++) arr[i] = (int*)malloc(cols * sizeof(int)); B int arr[rows][cols] = (int**)malloc(...); C int **arr = calloc(rows*cols, sizeof(int)); D int arr[][] = malloc(rows * cols);
2D arrays are allocated as array of pointers, where each row pointer points to a dynamically allocated array.
Consider a program that repeatedly allocates memory in a loop but never frees it. What problem occurs?
A Stack overflow B Memory leak - heap memory is exhausted C Segmentation fault immediately D Integer overflow
Allocating without freeing causes memory leak, where allocated memory is not returned to the system, eventually exhausting heap.
What happens if you call free() twice on the same pointer?
A Program immediately crashes B Double free error - undefined behavior C Memory is freed twice successfully D No effect, second free is ignored
Double free is undefined behavior and can cause program crash or corruption. After first free(), the pointer should not be used.
In a competitive programming scenario, you need to store strings dynamically. What is safe practice?
A char *str = malloc(100); without checking for NULL B char *str = malloc(strlen(input)+1); with NULL check and strncpy C char str[100] always on stack D No allocation needed, use global arrays
Safe practice includes allocating exact size (input length + 1 for null terminator), checking for NULL, and using bounded string functions.
A program uses realloc() to expand an array from 100 to 200 elements. The old address is p. After realloc():?
A p still points to old memory location B p is automatically updated to new location C Must assign returned value: p = realloc(p, ...) D The array content is lost
realloc() returns a new pointer (old location may move). Must use: p = realloc(p, newsize) and check for NULL.
For a graph with V vertices and E edges stored dynamically, what is typical space complexity?
A O(1) B O(V + E) for adjacency list, O(V²) for adjacency matrix C O(E²) D O(V*E)
Adjacency list uses dynamic allocation proportional to V+E, while matrix uses fixed V² space regardless of actual edges.