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 51–60 of 499
Topics in C Programming
Q.51 Medium Dynamic Memory
In competitive programming for graph problems, if adjacency list uses dynamic arrays, what is the time complexity of adding an edge?
A O(1) amortized
B O(n)
C O(log n)
D O(n²)
Correct Answer:  A. O(1) amortized
EXPLANATION

Using dynamic arrays (vectors in C++) with doubling strategy provides O(1) amortized time for appending edges.

Take Test
Q.52 Medium Dynamic Memory
Which scenario is most likely to cause a segmentation fault in dynamic memory?
A Calling malloc() multiple times
B Accessing memory beyond allocated size
C Freeing NULL pointer
D Calling realloc() on allocated memory
Correct Answer:  B. Accessing memory beyond allocated size
EXPLANATION

Buffer overflow by accessing beyond allocated boundaries causes segmentation fault as it accesses invalid memory pages.

Take Test
Q.53 Medium Dynamic Memory
For implementing a dynamic circular buffer of size n, how many pointers are minimally required?
A 1 pointer
B 2 pointers
C 3 pointers
D 4 pointers
Correct Answer:  B. 2 pointers
EXPLANATION

A circular buffer requires head and tail pointers minimum to track current positions in the circular structure.

Take Test
Q.54 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.55 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.56 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.57 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.58 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
Q.59 Medium Dynamic Memory
In graph algorithms with dynamic adjacency lists, what's the memory requirement for a sparse graph with V vertices and E edges?
A O(V + E)
B O(V²)
C O(E²)
D O(V * E)
Correct Answer:  A. O(V + E)
EXPLANATION

Using dynamic adjacency lists, you allocate V list pointers and E edge nodes total, giving O(V + E) space complexity, which is optimal for sparse graphs.

Take Test
Q.60 Medium Dynamic Memory
What potential issue occurs with this code in a large-scale system?
while(1) { int *arr = malloc(1000000); }
A Memory exhaustion - repeated allocation without deallocation
B Stack overflow
C Compilation error
D Segmentation fault immediately
Correct Answer:  A. Memory exhaustion - repeated allocation without deallocation
EXPLANATION

This creates an infinite loop that allocates 1MB each iteration without freeing, rapidly exhausting available memory. This is a classic memory leak scenario.

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