This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Conditional Preprocessor Directives – 1”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. What will be the output of the following C code?
#include<stdio.h> #define max 100 main() { #ifdef max printf("hello"); }
a) 100
b) hello
c) “hello”
d) error
View Answer
Explanation: 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
View Answer
Explanation: 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?
#include<stdio.h> void main() { #ifndef max printf("hello"); #endif printf("hi"); }
a) hello
b) hellohi
c) error
d) hi
View Answer
Explanation: 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<stdio.h> #define san 557 int main() { #ifndef san printf("yes"); #endif printf("no"); }
a) error
b) yes
c) no
d) yesno
View Answer
Explanation: 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
View Answer
Explanation: #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<stdio.h> #define max 100 void main() { #if(max%10) printf("san"); #endif printf("foundry"); }
a) error
b) san
c) foundry
d) sanfoundry
View Answer
Explanation: 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 if (100%10), which is false, the statements after #endif are executed. Hence the output is foundry.
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
View Answer
Explanation: #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<stdio.h> #define hello 10 void main() { printf("%d",hello); #undef hello printf("%d",hello); }
a) 10
b) hello
c) error
d) 1010
View Answer
Explanation: 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 <stdio.h> #define a 2 main() { int r; #define a 5 r=a*2; printf("%d",r); }
a) 10
b) 4
c) 2
d) 5
View Answer
Explanation: 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?
#include<stdio.h> int main() { int p,q; printf("Enter two numbers\n"); 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
View Answer
Explanation: 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.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Check C Books
- Practice BCA MCQs
- Check Computer Science Books