In competitive programming for graph problems, if adjacency list uses dynamic arrays, what is the time complexity of adding an edge?
Answer: A
Using dynamic arrays (vectors in C++) with doubling strategy provides O(1) amortized time for appending edges.
Q.442Medium
What is the correct way to store variable-length strings dynamically?
Answer: B
Proper way requires dynamic allocation with malloc, adding 1 for null terminator, and using fgets for safe input.
Q.443Medium
What is the output of this code?
#define ADD(a,b) a+b
int result = ADD(5,3)*2;
Answer: A
The macro expands to 5+3*2 = 5+6 = 11, not 16. However, due to operator precedence, it becomes 5+3*2. Actually, it expands correctly to (5+3)*2 = 16 when evaluated.
Q.444Medium
What is the purpose of the #ifdef directive?
Answer: A
#ifdef checks if a macro identifier is currently defined. Code following it is compiled only if the macro is defined.
Q.445Medium
Which of the following shows the correct order of preprocessor directives in a C program?
Answer: C
Preprocessor directives can appear in any order in the source code. However, logically, #include is often used before #define and #ifdef checks.
Advertisement
Q.446Medium
What is the difference between #include <stdio.h> and #include "myheader.h"?
Answer: B
Angle brackets search in standard system directories. Double quotes search in the current directory first, then standard directories. This is the standard convention.
Q.447Medium
What will happen if you define the same macro twice with different values?
Answer: B
When a macro is redefined, the new definition replaces the old one. Some compilers may issue a warning about redefinition.
Q.448Medium
What is the output of this code?
#define SWAP(a,b) {int temp=a; a=b; b=temp;}
int x=5, y=10;
SWAP(x,y);
printf("%d %d", x, y);
Answer: B
The SWAP macro exchanges x and y values using a temporary variable. After the swap, x becomes 10 and y becomes 5.
Q.449Medium
What does the ## operator do in a macro?
Answer: A
The ## operator (token pasting) concatenates two tokens into a single token during macro expansion.
Q.450Medium
What is the output?
#define STR(x) #x
printf("%s", STR(hello));
Answer: A
The # operator (stringification) converts the macro argument into a string literal. STR(hello) becomes "hello" as a string.
Q.451Medium
Consider the following code:
#define MAX 100
#define MAX 200
What will be the result?
Answer: A
In C, redefining a macro without #undef first will cause a compilation error. Most compilers treat macro redefinition as an error.
Q.452Medium
What will be the output of the following code?
#define SQUARE(x) x*x
int result = SQUARE(2+3);
printf("%d", result);
Answer: B
SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11 due to operator precedence. Macros don't evaluate arguments; they substitute text directly. Safe macro should be ((x)*(x)).
Q.453Medium
What will be printed?
#define DEBUG 1
#if DEBUG
printf("Debug mode ON");
#else
printf("Debug mode OFF");
#endif
Answer: A
Since DEBUG is defined as 1 (non-zero), the #if DEBUG condition is true, so only the code in #if block executes. The #else block is skipped.
Q.454Medium
What does the following macro do?
#define CONCATENATE(a,b) a##b
Answer: B
The ## operator (token pasting operator) concatenates two tokens into a single token during preprocessing. For example, CONCATENATE(var,1) becomes var1.
Q.455Medium
What is the output of:
#define STR(x) #x
printf("%s", STR(HELLO));
Answer: A
The # operator (stringification) converts the macro argument into a string literal. STR(HELLO) becomes "HELLO", and printf prints: HELLO
Q.456Medium
What happens when you use #undef on a macro?
Answer: B
#undef removes a macro definition from the preprocessor's symbol table, allowing you to redefine it later without errors.
Q.457Medium
Which of the following will correctly find the maximum of two numbers using a macro?
Answer: B
Option B is safest as all operands and the entire expression are parenthesized, preventing operator precedence issues and side effects.
Q.458Medium
Which of the following is the correct way to prevent multiple inclusion of a header file?
Answer: D
#ifndef guards and #pragma once are both used to prevent multiple inclusion. #pragma once is non-standard but widely supported, while #ifndef is the standard approach.
Q.459Medium
What is the output of the following code?
#define SQUARE(x) x*x
int a = SQUARE(5+3);
Answer: B
SQUARE(5+3) expands to 5+3*5+3 due to operator precedence (multiplication before addition), which equals 5+15+3=23. However, the expression is actually 5+3*5+3=23. Actually it's (5+3)*(5+3)=64 if properly parenthesized. Without parentheses: 5+3*5+3 = 5+15+3 = 23. But SQUARE(5+3) = 5+3*5+3 = 23. Re-evaluating: x*x where x=5+3 becomes 5+3*5+3 = 23+21 = 44.
Q.460Medium
What happens when you use stringification operator (#) in a macro?
#define STRINGIFY(x) #x
Answer: B
The # operator (stringification) converts its argument into a string literal. For example, STRINGIFY(hello) becomes "hello". This is useful for creating string representations of identifiers.