C Programming Questions and Answers on Conditional Inclusion for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_12.html
1. What is the output of this C code?
#include
#define SYSTEM 20
int main()
{
int a = 20;
#if SYSTEM == a
printf("HELLO ");
#endif
#if SYSTEM == 20
printf("WORLD\n");
#endif
}
a) HELLO
b) WORLD
c) HELLO WORLD
d) No Output
b) WORLD
c) HELLO WORLD
d) No Output
Answer: b
2. Comment on the following code?
#include
#define Cprog
int main()
{
int a = 2;
#ifdef Cprog
a = 1;
printf("%d", Cprog);
}
a) No output on execution
b) Output as 1
c) Output as 2
d) Compile time error
b) Output as 1
c) Output as 2
d) Compile time error
Answer: d
3. The “else if” in conditional inclusion is written by?
a) #else if
b) #elseif
c) #elsif
d) #elif
Answer: d
4. What is the output of this C code?
#include
#define COLD
int main()
{
#ifdef COLD
printf("COLD\t");
#undef COLD
#endif
#ifdef COLD
printf("HOT\t");
#endif
}
a) HOT
b) COLD
c) COLD HOT
d) No Output
b) COLD
c) COLD HOT
d) No Output
Answer: b
5. Which of the following sequences are unaccepted in C language?
a) #if
#else
#endif
b) #if
#elif
#endif
c) #if
#if
#endif
d) #if
#undef
#endif
Answer: c
6. In a conditional inclusion, if the condition that comes after the if holds.
a) Then the code up to the following #else or #elif or #endif is compiled
b) Then the code up to the following #endif is compiled even if #else or #elif is present
c) Then the code up to the following #eliif is compiled
d) None of the mentioned
Answer: a
.
.
7. Conditional inclusion can be used for
a) Preventing multiple declarations of a variable
b) Check for existence of a variable and doing something if it exists
c) Preventing multiple declarations of same function
d) All of the mentioned
Answer: d
8. The #elif directive cannot appear after the preprocessor #else directive.
a) True
b) False
c) None of the mentioned
d) Varies
Answer: a
9 For each #if, #ifdef, and #ifndef directive.
a) There are zero or more #elif directives
b) Zero or one #else directive
c) One matching #endif directive
d) All of the mentioned
Answer: d
10. The #else directive is used for
a) Conditionally include source text if the previous #if, #ifdef, #ifndef, or #elif test fails.
b) Conditionally include source text if a macro name is not defined
c) Conditionally include source text if a macro name is defined
d) Ending conditional text
Answer: a
11. What is the output of this C code?
#include
#define MIN 0
#if MIN
#define MAX 10
#endif
int main()
{
printf("%d %d\n", MAX, MIN);
return 0;
}
a) 10 0
b) Compile time error
c) Undefined behaviour
d) None of the mentioned
b) Compile time error
c) Undefined behaviour
d) None of the mentioned
Answer: b
12. What is the output of this C code?
#include
#define MIN 0
#ifdef MIN
#define MAX 10
#endif
int main()
{
printf("%d %d\n", MAX, MIN);
return 0;
}
a) 10 0
b) Compile time error
c) Undefined behaviour
d) None of the mentioned
b) Compile time error
c) Undefined behaviour
d) None of the mentioned
Answer: a
13. What is the output of this C code?
#include
#define MIN 0
#if defined(MIN) + defined(MAX)
#define MAX 10
#endif
int main()
{
printf("%d %d\n", MAX, MIN);
return 0;
}
a) 10 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
Answer: a
14. What is the output of this C code?
#include
#define MIN 0
#if defined(MIN) - (!defined(MAX))
#define MAX 10
#endif
int main()
{
printf("%d %d\n", MAX, MIN);
return 0;
}
a) 10 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
Answer: b
15. What is the output of this C code?
#include
#define MIN 0
#ifdef(MIN)
#define MAX 10
#endif
int main()
{
printf("%d %d\n", MAX, MIN);
return 0;
}
a) 10 0
b) Compile time error
c) Run time error
d) Preprocessor error
b) Compile time error
c) Run time error
d) Preprocessor error
Answer: d
16. What is the output of code given below?
#include
#define MIN 0);
#ifdef MIN
#define MAX 10
#endif
int main()
{
printf("%d %d\n", MAX, MIN
return 0;
}
a) 10 0
b) Compile time error due to illegal syntax for printf
c) Undefined behaviour
d) Compile time error due to illegal MIN value
b) Compile time error due to illegal syntax for printf
c) Undefined behaviour
d) Compile time error due to illegal MIN value
Answer: a