250+ TOP MCQs on Macro Substitution and Answers

C questions and puzzles on “Macro Substitution”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C questions and puzzles on “Macro Substitution” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     #define foo(m, n) m ## n
  3.     int main()
  4.     {
  5.         printf("%sn", foo(k, l));
  6.     }

a) k l
b) kl
c) Compile time error
d) Undefined behaviour
Answer: c
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     #define foo(m, n) " m ## n "
  3.     int main()
  4.     {
  5.         printf("%sn", foo(k, l));
  6.     }

a) k l
b) kl
c) Compile time error
d) m ## n
Answer: d
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     #define foo(x, y) #x #y
  3.     int main()
  4.     {
  5.         printf("%sn", foo(k, l));
  6.         return 0;
  7.     }

a) kl
b) k l
c) xy
d) Compile time error
Answer: a
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     #define foo(x, y) x / y + x
  3.     int main()
  4.     {
  5.         int i = -6, j = 3;
  6.         printf("%dn",foo(i + j, 3));
  7.         return 0;
  8.     }

a) Divided by zero exception
b) Compile time error
c) -8
d) -4
Answer: c
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     void f();
  3.     int main()
  4.     {
  5.         #define foo(x, y) x / y + x
  6.         f();
  7.     }
  8.     void f()
  9.     {
  10.         printf("%dn", foo(-3, 3));
  11.     }

a) -8
b) -4
c) Compile time error
d) Undefined behaviour
Answer: b
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     void f();
  3.     int main()
  4.     {
  5.         #define max 10
  6.         f();
  7.         return 0;
  8.     }
  9.     void f()
  10.     {
  11.         printf("%dn", max * 10);
  12.     }

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
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     #define foo(x, y) x / y + x
  3.     int main()
  4.     {
  5.         int i = -6, j = 3;
  6.         printf("%d ", foo(i + j, 3));
  7.         printf("%dn", foo(-3, 3));
  8.         return 0;
  9.     }

a) -8 -4
b) -4 divided by zero exception
c) -4 -4
d) Divided by zero exception
Answer: a
Clarification: None.

8. What will be the output of the following C code?

  1.  #include 
  2.     int foo(int, int);
  3.     #define foo(x, y) x / y + x
  4.     int main()
  5.     {
  6.         int i = -6, j = 3;
  7.         printf("%d ",foo(i + j, 3));
  8.         #undef foo
  9.         printf("%dn",foo(i + j, 3));
  10.     }
  11.     int foo(int x, int y)
  12.     {
  13.         return x / y + x;
  14.     }

a) -8 -4
b) Compile time error
c) -8 -8
d) Undefined behaviour
Answer: a
Clarification: None.

9. 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
Clarification: None.

Leave a Reply

Your email address will not be published. Required fields are marked *