This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Conditional Preprocessor Directives – 2”.
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 san 10 main() { #ifdef san #define san 20 #endif printf("%d",san); }
a) 10
b) 20
c) Error
d) 1020
View Answer
Explanation: In the code shown above, if the identifier san is defined, then its value is redefined and changed to 20. It is then printed. Hence the output is 20.
2. What will be the output of the following C code?
#include<stdio.h> #define hello main() { #ifdef hello #define hi 4 #else #define hi 5 #endif printf("%d",hi); }
a) 4
b) 5
c) 45
d) error
View Answer
Explanation: The code shown above illustrates #define, #ifdef, #else, #endif. Since the identifier hello is defined (condition of #if is true), another identifier names hi is defined and it’s value is 4. If hello was not defined, then the value of hi would be 5.
3. The purpose of the preprocessor directive #error is that ____________
a) It rectifies any error present in the code
b) It rectifies only the first error which occurs in the code
c) It causes the preprocessor to report a fatal error
d) It causes the preprocessor to ignore an error
View Answer
Explanation: #error is a preprocessor directive which causes the preprocessor to report a fatal error. The tokens which form the rest of the line after #error are used as the error message.
4. What will be the output of the following C code?
#include<stdio.h> #define max 20 main() { #ifndef max #define min 10 #else #define min 30 #endif printf("%d",min); }
a) 10
b) 20
c) 30
d) error
View Answer
Explanation: The output of the code shown above is 30. Since the identifier max is defined (the condition of #ifndef is true), hence another identifier, that is, min is defined and its value is equal to 30.
5. What will be the output of the following C code?
#include<stdio.h> #define hello 10 main() { #ifdef hello #undef hello #define hello 100 #else #define hello 200 #endif printf("%d",hello); }
a) Error
b) 10
c) 100
d) 200
View Answer
Explanation: The output of the code shown above is 100. If the identifier hello is defined, then it is undefined and redefined. It’s new value is 100. If the identifier was not defined, it would be defined with a value of 200.
6. What will be the output of the following C code?
#include<stdio.h> #define sf 10 main() { if(sf==100) printf("good"); else { printf("bad"); sf=100; } printf("%d",sf); }
a) 100
b) bad
c) 10
d) error
View Answer
Explanation: The above code will result in an error because a macro cannot be defined using a simple assignment operator. In the code shown above, the value of sf is changed to 100 using the assignment operator. This causes an error.
7. What will be the output of the following C code?
#include<stdio.h> void f() { #define sf 100 printf("%d",sf); } int main() { #define sf 99; f(); printf("%d",sf); }
a) error
b) 100
c) 99
d) 10099
View Answer
Explanation: Macro definition is global even if it is appears within the function scope. In this case the macro sf is defined in the function f(). Hence it results in a compile time error.
8. What will be the output of the following C code?
#include<stdio.h> #define INDIA 1 #define US 2 #define CC US main() { #if CC==INDIA printf("Rupee"); #elif CC==US printf("Dollar"); #else printf("Euro"); #endif }
a) Euro
b) Rupee
c) Dollar
d) Error
View Answer
Explanation: The output of the code shown above is Dollar. Since the macro CC is defined as US at the beginning of the code, it satisfies the condition #elif(CC==US). Hence “Dollar” is printed.
9. What will be the output of the following C code?
#define sqr(x) x*x main() { int a1; a1=25/sqr(5); printf("%d",a1); }
a) 25
b) 1
c) 5
d) error
View Answer
Explanation: The output of the code shown above is 25. This is because the arithmetic statement takes the form of: 25/5*5 (which is equal to 1). If the macro had been defined as #define sqr(x) (x*x), the output would have been 1.
10. Which of the following is not a preprocessor directive?
a) #error
b) #pragma
c) #if
d) #ifelse
View Answer
Explanation: #ifelse is not a preprocessor directive. #error, #pragma, #if are preprocessor directives. There is a preprocessor directive, #elif, which performs the function of else-if.
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.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy C Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Buy Computer Science Books
- Watch Advanced C Programming Videos