Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

1,000 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 111–120 of 1,000
Topics in C Programming
Q.111 Medium Dynamic Memory
What happens when realloc() is called on a pointer allocated by malloc() with a larger size?
A Old memory is freed and new memory is allocated
B Returns NULL always
C Reallocates in same location or new location, preserving old data
D Causes undefined behavior
Correct Answer:  C. Reallocates in same location or new location, preserving old data
EXPLANATION

realloc() attempts to expand memory in place if possible; if not, allocates new block, copies old data, and frees old block.

Take Test
Q.112 Easy Dynamic Memory
In a dynamic queue implementation, if front pointer is NULL, what does it indicate?
A Queue is full
B Queue has only one element
C Queue is empty
D Memory allocation failed
Correct Answer:  C. Queue is empty
EXPLANATION

In queue implementations using linked lists, NULL front pointer indicates queue is empty as there are no nodes to dequeue.

Take Test
Q.113 Easy Dynamic Memory
What is the primary difference between calloc() and malloc() in C?
A calloc() allocates more memory than malloc()
B calloc() initializes allocated memory to zero, malloc() does not
C malloc() is faster than calloc()
D calloc() can only allocate arrays, malloc() allocates single blocks
Correct Answer:  B. calloc() initializes allocated memory to zero, malloc() does not
EXPLANATION

calloc(n, size) allocates n*size bytes and initializes all to 0, while malloc(size) allocates size bytes with garbage values.

Take Test
Q.114 Medium Dynamic Memory
Which of the following correctly deallocates a 2D dynamically allocated array created as int **arr?
A free(arr);
B for(int i=0;i
C delete arr;
D free(**arr);
Correct Answer:  B. for(int i=0;i
EXPLANATION

A 2D array created with double pointers requires freeing each row first, then the pointer array itself to avoid memory leaks.

Take Test
Q.115 Easy Dynamic Memory
How many bytes are allocated by malloc(sizeof(int) * 10) on a system where int is 4 bytes?
A 10 bytes
B 40 bytes
C 44 bytes
D 50 bytes
Correct Answer:  B. 40 bytes
EXPLANATION

malloc allocates exactly sizeof(int) * 10 = 4 * 10 = 40 bytes. sizeof() operator returns size in bytes.

Take Test
Q.116 Hard Dynamic Memory
Which of these represents proper error handling for malloc in competitive programming?
A int *arr = (int*)malloc(n * sizeof(int)); if(!arr) return -1;
B int *arr = (int*)malloc(n * sizeof(int));
C int *arr = malloc(-1);
D int *arr = (int*)malloc(0);
Correct Answer:  A. int *arr = (int*)malloc(n * sizeof(int)); if(!arr) return -1;
EXPLANATION

Checking if malloc returns NULL before using the pointer is essential error handling. If allocation fails, the program should handle it gracefully rather than proceeding with NULL.

Take Test
Q.117 Hard Dynamic Memory
In a dynamic stack implementation for competitive programming, how should you handle reallocation when the stack becomes full?
A Use realloc() to double the capacity and copy old data
B Return an error and stop accepting elements
C Allocate a new stack dynamically
D Use a fixed-size array instead
Correct Answer:  A. Use realloc() to double the capacity and copy old data
EXPLANATION

Doubling capacity and using realloc() provides amortized O(1) insertion with manageable memory overhead, which is the standard approach for dynamic data structures.

Take Test
Q.118 Medium Dynamic Memory
What happens when you try to free a pointer that was not allocated by malloc/calloc/realloc?
A Undefined behavior - likely crash or memory corruption
B The program ignores the free() call
C It frees some random memory safely
D Compilation error occurs
Correct Answer:  A. Undefined behavior - likely crash or memory corruption
EXPLANATION

Calling free() on a pointer that wasn't returned by malloc/calloc/realloc causes undefined behavior, potentially corrupting the heap and crashing the program.

Take Test
Q.119 Medium Dynamic Memory
Which function should be used to allocate memory for an array of 50 structures of size 'sizeof(struct Node)' and initialize to zero?
A calloc(50, sizeof(struct Node))
B malloc(50 * sizeof(struct Node))
C realloc(NULL, 50)
D alloc(50, sizeof(struct Node))
Correct Answer:  A. calloc(50, sizeof(struct Node))
EXPLANATION

calloc(50, sizeof(struct Node)) allocates memory for 50 elements of struct Node size and initializes all bytes to zero, which is ideal when you need initialized memory.

Take Test
Q.120 Medium Dynamic Memory
What is the advantage of dynamic allocation over static allocation in competitive programming?
A Flexibility to use memory based on input size, avoiding stack limits
B Faster execution speed
C No need to include headers
D Automatic memory cleanup
Correct Answer:  A. Flexibility to use memory based on input size, avoiding stack limits
EXPLANATION

Dynamic allocation allows using memory proportional to actual input size, preventing TLE from stack overflow with large static arrays and enabling solutions for variable-sized problems.

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