C MCQs on “Conditional Preprocessor Directives – 1”.
1. What will be the output of the following C code?
#include#define max 100 main() { #ifdef max printf("hello"); }
a) 100
b) hello
c) “hello”
d) error
Answer: d
Clarification: The code shown above results in an error. This is because the preprocessor #endif is missing in the above code. If the identifier is defined, then the statements following #ifdef are executed till #endif is encountered.
2. _______________ is the preprocessor directive which is used to end the scope of #ifdef.
a) #elif
b) #ifndef
c) #endif
d) #if
Answer: c
Clarification: The #ifdef preprocessor directive is used to check if a particular identifier is currently defined or not. If the particular identifier is defined, the statements following this preprocessor directive are executed till another preprocessor directive #endif is encountered. #endif is used to end the scope of #ifdef.
3. What will be the output of the following C code?
#includevoid main() { #ifndef max printf("hello"); #endif printf("hi"); }
a) hello
b) hellohi
c) error
d) hi
Answer: b
Clarification: The code shown above illustrates the preprocessor directive #ifndef. If the identifier checked is not defined, then the statements following #ifndef are executed. Here, since the identifier max is not defined, the statement after #ifndef is executed. Hence the output is: hellohi
4. What will be the output of the following C code?
#include#define san 557 main() { #ifndef san printf("yes"); #endif printf("no"); }
a) error
b) yes
c) no
d) yesno
Answer: c
Clarification: The output to the above code will be no. Since we have used the preprocessor directive, #ifndef and defined the identifier san, “yes” is not printed. Hence only “no” is printed as output.
5. The preprocessor directive which checks whether a constant expression results in a zero or non-zero value __________
a) #if
b) #ifdef
c) #undef
d) #ifndef
Answer: a
Clarification: #if checks whether a constant expression results in zero or not. If the expression is a non-zero value, then the statements between #if and #endif will be executed. If the constant expression results in a zero value, the statements between #if and #endif will not be executed.
6. What will be the output of the following C code?
#include#define max 100 void main() { #if(max%10) printf("san"); #endif printf("foundry"); }
a) error
b) san
c) foundry
d)
Answer: d
Clarification: The code shown above is an illustration of the preprocessor directive #if. The value of the defined identifier max is 100. On checking the condition (100510==), which is true, the statements after #if are executed till #endif is encountered. Hence the output is .
7. The preprocessor directive which is used to remove the definition of an identifier which was previously defined with #define?
a) #ifdef
b) #undef
c) #ifndef
d) #def
Answer: b
Clarification: #undef is used to remove the definition of any identifier which had been previously defined in the code with #define.
8. What will be the output of the following C code?
#include#define hello 10 void main() { printf("%d",hello); #undef hello printf("%d",hello); }
a) 10
b) hello
c) error
d) 1010
Answer: c
Clarification: Error: hello undeclared. An error is thrown when the code shown above is executed because before the second call to the printf function, the macro hello was undefined using #undef.
9. What will be the output of the following C code?
#include#define a 2 main() { int r; #define a 5 r=a*2; printf("%d",r); }
a) 10
b) 4
c) 2
d) 5
Answer: a
Clarification: The macro ‘a’ is redefined in the code shown above. Hence the value of a, which is initially 2, is redefined to 5. The value stored in ‘r’ is the result of the expression (a*2), which is equal to 10. Hence the output of this code will be 10.
10. What will be the output of the following C code if the value of ‘p’ is 10 and that of ‘q’ is 15?
#includeint main() { int p,q; printf("Enter two numbersn"); scanf("%d",&p); scanf("%d",&q); #if(4<2) printf("%d",p); #elif(2>-1) printf("%d",q); #else printf("bye"); #endif }
a) 10
b) 15
c) bye
d) error
Answer: b
Clarification: The output of the code shown above is 15. The values given for ‘p’ and ‘q’ are 10 and 15 respectively. Since the condition given for #elif is true, the value stored in ‘q’ will be printed, that is 15.