C Questions and Answers Part-30

1. What will be the output of the following C code?
#include < stdio.h >
void f();
int main()
{
#define max 10
f();
return 0;
}
void f()
{
printf("%d\n", max * 10);
}
a) 100
b) Compile time error since #define cannot be inside functions
c) Compile time error since max is not visible in f()
d) Undefined behaviour

Answer: a

2. What will be the output of the following C code?
#include < stdio.h >
#define foo(x, y) x / y + x
int main()
{
int i = -6, j = 3;
printf("%d ", foo(i + j, 3));
printf("%d\n", foo(-3, 3));
return 0;
}
a) -8 -4
b) -4 divided by zero exception
c) -4 -4
d) Divided by zero exception

Answer: a

3. What will be the output of the following C code?
#include < stdio.h >
int foo(int, int);
#define foo(x, y) x / y + x
int main()
{
int i = -6, j = 3;
printf("%d ",foo(i + j, 3));
#undef foo
printf("%d\n",foo(i + j, 3));
}
int foo(int x, int y)
{
return x / y + x;
}
a) -8 -4
b) Compile time error
c) -8 -8
d) Undefined behaviour

Answer: a

4. What is the advantage of #define over const?
a) Data type is flexible
b) Can have a pointer
c) Reduction in the size of the program
d) None of the mentioned

Answer: a

5. What will be the output of the following C code?
#include < stdio.h >
#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

Answer: b

6. The “else if” in conditional inclusion is written by?
a) #else if
b) #elseif
c) #elsif
d) #elif

Answer: d

7. What will be the output of the following C code?
#include < stdio.h >
#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

Answer: b

8. In a conditional inclusion, if the condition that comes after the if is true, then what will happen during compilation?
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

9. 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

10. The #elif directive cannot appear after the preprocessor #else directive.
a) true
b) false

Answer: a