Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

490 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 51–60 of 490
Topics in C Programming
Q.51 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.

Test
Q.52 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.

Test
Q.53 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.

Test
Q.54 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.

Test
Q.55 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.

Test
Q.56 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.

Test
Q.57 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.

Test
Q.58 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.

Test
Q.59 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.

Test
Q.60 Medium Dynamic Memory
For storing variable-length strings dynamically in a competitive program, what's the safest approach?
A Allocate strlen(s)+1 bytes to include null terminator
B Allocate strlen(s) bytes
C Use fixed-size char array of 1000
D Allocate 2*strlen(s) for safety
Correct Answer:  A. Allocate strlen(s)+1 bytes to include null terminator
EXPLANATION

Strings in C are null-terminated, so allocating strlen(s)+1 bytes ensures space for the null terminator. This is the standard safe approach.

Test
IGET
IGET AI
Online · Exam prep assistant
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