C Programming Questions and Answers on Macro Substitution for Freshers
https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_11.html
1. What is the output of this C code?
#include#define foo(m, n) m ## nint main()
{printf("%s\n", foo(k, l));
}
a) k l
b) kl
c) Compile time error
d) Undefined behaviour
b) kl
c) Compile time error
d) Undefined behaviour
Answer: c
2. What is the output of this C code?
#include#define foo(m, n) " m ## n "int main()
{printf("%s\n", foo(k, l));
}
a) k l
b) kl
c) Compile time error
d) m ## n
b) kl
c) Compile time error
d) m ## n
Answer: d
3. What is the output of this C code?
#include#define foo(x, y) #x #yint main()
{printf("%s\n", foo(k, l));
return 0;
}
a) kl
b) k l
c) xy
d) Compile time error
b) k l
c) xy
d) Compile time error
Answer: a
4. What is the output of this C code?
#include#define foo(x, y) x / y + xint main()
{int i = -6, j = 3;
printf("%d\n",foo(i + j, 3));
return 0;
}
a) Divided by zero exception
b) Compile time error
c) -8
d) -4
b) Compile time error
c) -8
d) -4
Answer: c
.
.
5. What is the output of this C code?
#includevoid f();
int main()
{#define foo(x, y) x / y + xf();
}void f()
{printf("%d\n", foo(-3, 3));
}
a) -8
b) -4
c) Compile time error
d) Undefined behaviour
b) -4
c) Compile time error
d) Undefined behaviour
Answer: b
6. What is the output of this C code?
#includevoid f();
int main()
{#define max 10f();
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
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?
#include#define foo(x, y) x / y + xint 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
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?
#includeint foo(int, int);
#define foo(x, y) x / y + xint main()
{int i = -6, j = 3;
printf("%d ",foo(i + j, 3));
#undef fooprintf("%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
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?
#includevoid main()
{#define max 37;printf("%d", max);
}
a) 37
b) Compile time error
c) Varies
d) Depends on compiler
b) Compile time error
c) Varies
d) Depends on compiler
Answer: b
11. What is the output of this C code?
#includevoid main()
{#define max 37printf("%d", max);
}
a) 37
b) Run time error
c) Varies
d) Depends on compiler
b) Run time error
c) Varies
d) Depends on compiler
Answer: a
12. What is the output of this C code?
#includevoid main()
{#define const intconst max = 32;
printf("%d", max);
}
a) Run time error
b) 32
c) int
d) const
b) 32
c) int
d) const
Answer: b
13. What is the output of this C code?
#includevoid main()
{#define max 45max = 32;
printf("%d", max);
}
a) 32
b) 45
c) Compile time error
d) Varies
b) 45
c) Compile time error
d) Varies
Answer: c
14. What is the output of this C code?
#include# define maxvoid m()
{printf("hi");
}void main()
{max;m();
}
a) Run time error
b) hi hi
c) Nothing
d) hi
b) hi hi
c) Nothing
d) hi
Answer: d
15. What is the output of this C code?
#include#define A 1 + 2#define B 3 + 4int main()
{int var = A * B;
printf("%d\n", var);
}
a) 9
b) 11
c) 12
d) 21
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?
#include#define var 20);int main()
{printf("%d\n", var
}
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
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
