C Programming
C language from basics to advanced placement prep
198 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 198 questions
Q.21 Hard Preprocessor
What is the problem with this macro?
#define DOUBLE(x) x*x
int result = DOUBLE(2+3);
A No problem, result is 25
B Result is 11 due to operator precedence
C Compilation error
D Result is undefined
Correct Answer:  B. Result is 11 due to operator precedence
EXPLANATION

The macro expands to 2+3*2+3 = 2+6+3 = 11, not 25. This is because x is not parenthesized. Should use #define DOUBLE(x) ((x)*(x))

Take Test
Q.22 Hard Preprocessor
Which statement about #define is TRUE?
A Macros are type-safe like functions
B Macros do not allocate memory at runtime
C Macros cannot take arguments
D Macros are evaluated at runtime
Correct Answer:  B. Macros do not allocate memory at runtime
EXPLANATION

Macros are purely textual substitutions performed by the preprocessor at compile time, not at runtime. They don't allocate memory themselves.

Take Test
Q.23 Hard Dynamic Memory
Which memory allocation technique provides better locality of reference for accessing array elements?
A Allocating each element separately with malloc
B Single malloc for contiguous array
C Using static arrays
D Allocating with calloc for zeroed memory
Correct Answer:  B. Single malloc for contiguous array
EXPLANATION

Contiguous allocation places array elements next to each other in memory, improving cache hits and CPU performance through spatial locality.

Take Test
Q.24 Hard Dynamic Memory
In competitive coding, when implementing dynamic trees, which traversal is most suitable for level-order?
A Recursion with DFS
B Stack-based iteration
C Queue-based BFS using dynamic allocation
D In-order traversal
Correct Answer:  C. Queue-based BFS using dynamic allocation
EXPLANATION

Level-order traversal requires BFS which uses a queue. Dynamic queue allocation handles arbitrary tree sizes efficiently.

Take Test
Q.25 Hard Dynamic Memory
What is the memory overhead when allocating 1000 integers vs 1 integer in a system with malloc metadata?
A Negligible difference
B Proportional to number of allocations
C Fixed overhead per allocation regardless of size
D Exponential with allocation count
Correct Answer:  C. Fixed overhead per allocation regardless of size
EXPLANATION

Each malloc() call has fixed metadata overhead (typically 8-16 bytes). Single allocation of 1000 ints has less overhead than 1000 separate allocations.

Take Test
Advertisement
Q.26 Hard Dynamic Memory
For a dynamic hash table with chaining, if collision occurs, the new element should be inserted where?
A Always at the beginning of the chain
B Always at the end of the chain
C In sorted position
D Depends on implementation, typically at beginning for O(1)
Correct Answer:  D. Depends on implementation, typically at beginning for O(1)
EXPLANATION

While both work, inserting at beginning provides O(1) insertion; end requires traversal. Choice depends on use case.

Take Test
Q.27 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.28 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.29 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.30 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
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