C Programming Questions and Answers on Macro Substitution for Freshers

1. What is the output of this C code?

  1.     #include 
  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
Answer: c
2. What is the output of this C code?

  1.     #include 
  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
Answer: d
3. What is the output of this C code?

  1.     #include 
  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
Answer: a
4. What is the output of this 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\n",foo(i + j, 3));
  7.         return 0;
  8.     }
a) Divided by zero exception
b) Compile time error
c) -8
d) -4
Answer: c
.
5. What is the output of this 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("%d\n", foo(-3, 3));
  11.     }
a) -8
b) -4
c) Compile time error
d) Undefined behaviour
Answer: b
6. What is the output of this 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("%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
Answer: a
7. What is the output of this 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("%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
Answer: a
8. What is the output of this 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("%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
Answer: a
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
10. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         #define max 37;
  5.         printf("%d", max);
  6.     }
a) 37
b) Compile time error
c) Varies
d) Depends on compiler
Answer: b
11. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         #define max 37
  5.         printf("%d", max);
  6.     }
a) 37
b) Run time error
c) Varies
d) Depends on compiler
Answer: a
12. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         #define const int
  5.         const max = 32;
  6.         printf("%d", max);
  7.     }
a) Run time error
b) 32
c) int
d) const
Answer: b
13. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         #define max 45
  5.         max = 32;
  6.         printf("%d", max);
  7.     }
a) 32
b) 45
c) Compile time error
d) Varies
Answer: c
14. What is the output of this C code?

  1.     #include 
  2.     # define max
  3.     void m()
  4.     {
  5.         printf("hi");
  6.     }
  7.     void main()
  8.     {
  9.         max;
  10.         m();
  11.     }
a) Run time error
b) hi hi
c) Nothing
d) hi
Answer: d
15. What is the output of this C code?

  1.     #include 
  2.     #define A 1 + 2
  3.     #define B 3 + 4
  4.     int main()
  5.     {
  6.         int var = A * B;
  7.         printf("%d\n", var);
  8.     }
a) 9
b) 11
c) 12
d) 21
Answer: b
16. Which of the following Macro substitution are accepted in C?

a) #define A #define
    A VAR 20
b) #define A define
    #A VAR 20
c) #define #A #define
    #A VAR 20
d) None of the mentioned
Answer: d
17. Comment on the following code?

  1.     #include 
  2.     #define var 20);
  3.     int main()
  4.     {
  5.         printf("%d\n", var
  6.     }
a) No errors, it will show the output 20
b) Compile time error, the printf braces aren’t closed
c) Compile time error, there are no open braces in #define
d) None of the mentioned
Answer: a
18. Which of the following properties of #define not true?

a) You can use a pointer to #define
b) #define can be made externally available
c) They obey scope rules
d) All of the mentioned
Answer: d


Related

Multiple Choice Questions 4510123551818948617

Post a Comment

emo-but-icon

item