Showing 21–30 of 303 questions
Which preprocessor directive is used to conditionally compile code based on whether a macro is defined?
A
#if
B
#ifdef
C
#ifndef
D
Both B and C
Correct Answer:
D. Both B and C
EXPLANATION
#ifdef checks if a macro is defined, #ifndef checks if it's not defined. Both are used for conditional compilation based on macro definition status.
Which of the following is NOT a preprocessor directive?
A
#include
B
#define
C
#pragma
D
#allocate
Correct Answer:
D. #allocate
EXPLANATION
#allocate is not a valid preprocessor directive. Valid directives include #include, #define, #pragma, #ifdef, #ifndef, #if, #else, #elif, #endif, #error, #undef.
What is the primary role of the C preprocessor in the compilation process?
A
It processes source code before actual compilation and performs text substitutions
B
It executes the program after compilation
C
It handles memory allocation during runtime
D
It manages hardware resources
Correct Answer:
A. It processes source code before actual compilation and performs text substitutions
EXPLANATION
The preprocessor is a separate program that processes source code before the compiler. It handles directives like #include, #define, and #ifdef.
What is the purpose of the #undef directive?
A
To undefine a previously defined macro
B
To prevent macro expansion
C
To create undefined behavior
D
To test if a macro is undefined
Correct Answer:
A. To undefine a previously defined macro
EXPLANATION
#undef is used to remove the definition of a macro so it cannot be used in subsequent code.
What is the output?
#define MAX(a,b) (a>b?a:b)
printf("%d", MAX(10, 5));
EXPLANATION
The macro expands to (10>5?10:5) which evaluates to 10 since the condition is true.
What does the following preprocessor directive do?
#define SQUARE(x) ((x)*(x))
A
Defines a function named SQUARE
B
Defines an object-like macro
C
Defines a function-like macro
D
Creates a compilation error
Correct Answer:
C. Defines a function-like macro
EXPLANATION
This is a function-like macro that takes parameter x and expands to ((x)*(x)). The parentheses around x prevent operator precedence issues.
Which preprocessor directive is used to include standard library files?
A
#include
B
#include "filename"
C
Both A and B
D
#import
Correct Answer:
C. Both A and B
EXPLANATION
#include <filename> is used for standard library files (searched in standard paths). #include "filename" is used for user-defined header files (searched in current directory first).
What is the output of the following code?
#define MAX 5
int arr[MAX];
A
Compilation error
B
Array of size 5 is created
C
Runtime error
D
Preprocessor error
Correct Answer:
B. Array of size 5 is created
EXPLANATION
The preprocessor replaces MAX with 5, creating an array of size 5. This is a valid use of macro expansion.
Which of the following is NOT a preprocessor directive in C?
A
#define
B
#include
C
#pragma
D
#declare
Correct Answer:
D. #declare
EXPLANATION
#declare is not a valid preprocessor directive. Valid directives include #define, #include, #pragma, #ifdef, #endif, #if, #else, etc.
In implementing a dynamic doubly-linked list, what additional field is required compared to singly-linked list?
A
Data field
B
Previous pointer
C
Size field
D
Head pointer
Correct Answer:
B. Previous pointer
EXPLANATION
Doubly-linked list nodes require both next and previous pointers to enable bidirectional traversal unlike singly-linked lists.