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 131–140 of 1,000
Topics in C Programming
Q.131 Easy Dynamic Memory
Which header file must be included to use malloc() and free() functions?
A #include
B #include
C #include
D #include
Correct Answer:  A. #include
EXPLANATION

<stdlib.h> is the standard header file containing declarations for malloc(), calloc(), realloc(), and free() functions.

Take Test
Q.132 Easy Dynamic Memory
Which function in C is used to allocate memory dynamically at runtime?
A malloc()
B alloc()
C allocate()
D memalloc()
Correct Answer:  A. malloc()
EXPLANATION

malloc() is the standard C library function for dynamic memory allocation. It takes the number of bytes to allocate as an argument and returns a pointer to the allocated memory.

Take Test
Q.133 Hard Dynamic Memory
For implementing a dynamic stack in competitive programming, which memory management approach is best?
A Pre-allocate fixed size array on stack
B Single malloc for max capacity, track top with index
C malloc/free for each push/pop operation
D Use global static array
Correct Answer:  B. Single malloc for max capacity, track top with index
EXPLANATION

Single allocation with index tracking minimizes malloc/free overhead, suitable for competitive programming time constraints.

Take Test
Q.134 Hard Dynamic Memory
Consider this: char *p = malloc(10); strcpy(p, "Hello World"); What occurs?
A String is copied successfully
B Buffer overflow - writes beyond allocated 10 bytes
C Null terminator is lost
D Compilation error
Correct Answer:  B. Buffer overflow - writes beyond allocated 10 bytes
EXPLANATION

"Hello World" is 11 bytes (including null terminator). Writing to 10-byte buffer causes buffer overflow, corrupting adjacent memory.

Take Test
Q.135 Hard Dynamic Memory
What is a potential issue when allocating very large contiguous blocks (>10^8 bytes) dynamically?
A malloc() always succeeds for any size
B Fragmentation and allocation failure, should check return value
C Integer overflow in size calculation
D Free space is automatically allocated
Correct Answer:  B. Fragmentation and allocation failure, should check return value
EXPLANATION

Large allocations may fail due to heap fragmentation or insufficient contiguous memory. Always check malloc() return for NULL.

Take Test
Q.136 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.137 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.138 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.139 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
Q.140 Medium Dynamic Memory
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
Correct Answer:  B. char *str = malloc(strlen(input)+1); with NULL check and strncpy
EXPLANATION

Safe practice includes allocating exact size (input length + 1 for null terminator), checking for NULL, and using bounded string functions.

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