C Programming Questions and Answers – Macro Substitution – 1

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Macro Substitution – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

advertisement

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

  1.     #include <stdio.h>
  2.     #define foo(m, n) m ## n
  3.     int main()
  4.     {
  5.         printf("%s\n", foo(k, l));
  6.     }

a) k l
b) kl
c) Compile time error
d) Undefined behaviour
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

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

advertisement
  1.     #include <stdio.h>
  2.     #define foo(m, n) " m ## n "
  3.     int main()
  4.     {
  5.         printf("%s\n", foo(k, l));
  6.     }

a) k l
b) kl
c) Compile time error
d) m ## n
View Answer

Answer: d
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     #define foo(x, y) #x #y
  3.     int main()
  4.     {
  5.         printf("%s\n", foo(k, l));
  6.         return 0;
  7.     }

a) kl
b) k l
c) xy
d) Compile time error
View Answer

Answer: a
Explanation: None.
advertisement

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

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

a) Divided by zero exception
b) Compile time error
c) -8
d) -4
View Answer

Answer: c
Explanation: None.

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

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

a) -8
b) -4
c) Compile time error
d) Undefined behaviour
View Answer

Answer: b
Explanation: None.

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

  1.     #include <stdio.h>
  2.     void f();
  3.     int main()
  4.     {
  5.         #define max 10
  6.         f();
  7.         return 0;
  8.     }
  9.     void f()
  10.     {
  11.         printf("%d\n", 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
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  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("%d\n", foo(-3, 3));
  8.         return 0;
  9.     }

a) -8 -4
b) -4 divided by zero exception
c) -4 -4
d) Divided by zero exception
View Answer

Answer: a
Explanation: None.

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

  1.  #include <stdio.h>
  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("%d\n",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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.