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 61–70 of 499
Topics in C Programming
Q.61 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.

Take Test
Q.62 Medium Dynamic Memory
Which scenario represents a use-after-free error?
A int *p = malloc(10); free(p); p[0] = 5;
B int *p = malloc(10); free(p); free(p);
C int *p = malloc(10); int *q = p; free(p);
D free(NULL);
Correct Answer:  A. int *p = malloc(10); free(p); p[0] = 5;
EXPLANATION

In option A, after free(p), the memory is deallocated. Accessing it with p[0] = 5 is a use-after-free error, leading to undefined behavior.

Take Test
Q.63 Medium Dynamic Memory
In the 2024-2025 competitive exam pattern, a dynamic array needs to grow from 100 to 200 elements. Using realloc(), what should be the concern?
A Old pointers to the array may become invalid after realloc()
B realloc() always allocates new memory
C Data is lost during realloc()
D realloc() is slower than malloc() + memcpy()
Correct Answer:  A. Old pointers to the array may become invalid after realloc()
EXPLANATION

If realloc() needs to relocate the memory block to a different address, all old pointers to the array become invalid. Only the returned pointer should be used.

Take Test
Q.64 Medium Dynamic Memory
Which of the following is a memory leak?
A int *p = malloc(100); p = malloc(50);
B int *p = malloc(100); free(p);
C int *p = malloc(100); p = NULL;
D int *p = malloc(100); int *q = p;
Correct Answer:  A. int *p = malloc(100); p = malloc(50);
EXPLANATION

In option A, the first malloc(100) block becomes inaccessible after p is reassigned, but it's not freed, causing a memory leak. The original 100 bytes are lost.

Take Test
Q.65 Medium Dynamic Memory
What is the critical issue with this code snippet?
int *ptr = (int*)malloc(10);
ptr[11] = 5;
A Buffer overflow - accessing beyond allocated memory
B Memory leak - malloc not paired with free
C Type casting error
D NULL pointer dereference
Correct Answer:  A. Buffer overflow - accessing beyond allocated memory
EXPLANATION

malloc(10) allocates 10 bytes, but accessing ptr[11] goes beyond the allocated boundary, causing a buffer overflow which leads to undefined behavior and potential segmentation fault.

Take Test
Q.66 Medium Dynamic Memory
In a competitive programming problem, you need to allocate a 2D array of size 5x5 dynamically. Which approach is most efficient?
A Allocate single block of 25 integers and use 2D indexing
B Allocate 5 pointers, then allocate 5 integers for each pointer
C Use stack allocation with fixed size
D Use global arrays instead of dynamic allocation
Correct Answer:  A. Allocate single block of 25 integers and use 2D indexing
EXPLANATION

Allocating a single contiguous block is more cache-efficient and requires only one malloc() call, making it better for competitive programming where memory access speed matters.

Take Test
Q.67 Medium Dynamic Memory
In 2024-2025 competitive exams, for storing variable number of test cases (up to 10^5), which approach is optimal?
A Fixed array of size 10^5
B Dynamic allocation with exact size needed
C Multiple calls to realloc() as data arrives
D Use stack allocation for speed
Correct Answer:  B. Dynamic allocation with exact size needed
EXPLANATION

Reading input size first, then allocating exact size is optimal for memory efficiency and avoids repeated realloc() overhead.

Take Test
Q.68 Medium Dynamic Memory
What is the critical issue with this code? int *p = (int*)malloc(sizeof(int)); free(p); p = NULL;
A Cast to (int*) is unnecessary
B Setting p=NULL after free is good practice but not done immediately
C No issue, this is safe practice
D malloc size is too small
Correct Answer:  C. No issue, this is safe practice
EXPLANATION

This shows safe practice: allocation, use, freeing, and immediately setting pointer to NULL to avoid use-after-free.

Take Test
Q.69 Medium Dynamic Memory
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)
Correct Answer:  B. O(V + E) for adjacency list, O(V²) for adjacency matrix
EXPLANATION

Adjacency list uses dynamic allocation proportional to V+E, while matrix uses fixed V² space regardless of actual edges.

Take Test
Q.70 Medium Dynamic Memory
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
Correct Answer:  C. Must assign returned value: p = realloc(p, ...)
EXPLANATION

realloc() returns a new pointer (old location may move). Must use: p = realloc(p, newsize) and check for NULL.

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