Multiple Choice Questions and Answers for Precedence and Order of Evaluation in C Language

https://www.computersprofessor.com/2017/09/multiple-choice-questions-and-answers_22.html
List of C language interview questions “Precedence and Order of Evaluation” along with answers, explanations and/or solutions:
1. What is the output of this C code?
#include
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
exit(0);
printf("%d\n", i);
return reverse(i++);
}
a) 1 2 3 4 5
b) 1 2 3 4
c) Compile time error
d) Stack overflow
b) 1 2 3 4
c) Compile time error
d) Stack overflow
Answer:d
2. What is the output of this C code?
#include
void reverse(int i);
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
return ;
printf("%d ", i);
return reverse((i++, i));
}
a) 1 2 3 4 5
b) Segmentation fault
c) Compilation error
d) Undefined behaviour
b) Segmentation fault
c) Compilation error
d) Undefined behaviour
Answer:a
3. In expression i = g() + f(), first function called depends on
a) Compiler
b) Associativiy of () operator
c) Precedence of () and + operator
d) Left to write of the expression
Answer:a
4. What is the value of i and j in the below code?
#include
int x = 0;
int main()
{
int i = (f() + g()) || g();
int j = g() || (f() + g());
}
int f()
{
if (x == 0)
return x + 1;
else
return x - 1;
}
int g()
{
return x++;
}
a)i value is 1 and j value is 1
b)i value is 0 and j value is 0
c)i value is 1 and j value is undefined
d)i and j value are undefined
b)i value is 0 and j value is 0
c)i value is 1 and j value is undefined
d)i and j value are undefined
Answer:d
5. What is the value of i and j in the below code?
#include
int x = 0;
int main()
{
int i = (f() + g()) | g(); //bitwise or
int j = g() | (f() + g()); //bitwise or
}
int f()
{
if (x == 0)
return x + 1;
else
return x - 1;
}
int g()
{
return x++;
}
a) i value is 1 and j value is 1
b) i value is 0 and j value is 0
c) i value is 1 and j value is undefined
d) i and j value are undefined
b) i value is 0 and j value is 0
c) i value is 1 and j value is undefined
d) i and j value are undefined
Answer:c
6. What is the output of this C code?
#include
int main()
{
int x = 2, y = 0;
int z = y && (y |= 10);
printf("%d\n", z);
return 0;
}
a) 1
b) 0
c) Undefined behaviour due to order of evaluation
d) 2
b) 0
c) Undefined behaviour due to order of evaluation
d) 2
Answer:b
7. What is the output of this C code?
#include
int main()
{
int x = 2, y = 0;
int z = (y++) ? 2 : y == 1 && x;
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) 2
d)Undefined behaviour
b) 1
c) 2
d)Undefined behaviour
Answer:b
8. What is the output of this C code?
#include
int main()
{
int x = 2, y = 0;
int z;
z = (y++, y);
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) Undefined behaviour
d) Compilation error
b) 1
c) Undefined behaviour
d) Compilation error
Answer:b
9. What is the output of this C code?
#include
int main()
{
int x = 2, y = 0, l;
int z;
z = y = 1, l = x && y;
printf("%d\n", l);
return 0;
}
a) 0
b) 1
c) Undefined behaviour due to order of evaluation can be different
d) Compilation error
b) 1
c) Undefined behaviour due to order of evaluation can be different
d) Compilation error
Answer:b
10. What is the output of this C code?
#include
int main()
{
int y = 2;
int z = y +(y = 10);
printf("%d\n", z);
}
a) 12
b) 20
c) 4
d) Either 12 or 20
b) 20
c) 4
d) Either 12 or 20
Answer:b
11. What is the output of this C code?
#include
void main()
{
int b = 5 - 4 + 2 * 5;
printf("%d", b);
}
a) 25
b) -5
c) 11
d) None of the mentioned
b) -5
c) 11
d) None of the mentioned
Answer:c
12. What is the output of this C code?
#include
void main()
{
int b = 5 & 4 & 6;
printf("%d", b);
}
a) 5
b) 6
c) 3
d) 4
b) 6
c) 3
d) 4
Answer:d
13. What is the output of this C code?
#include
void main()
{
int b = 5 & 4 | 6;
printf("%d", b);
}
a) 6
b) 4
c) 1
d) 0
b) 4
c) 1
d) 0
Answer:a
14. What is the output of this C code?
#include
void main()
{
int b = 5 + 7 * 4 - 9 * (3, 2);
printf("%d", b);
}
a) 6
b) 15
c) 13
d) 21
b) 15
c) 13
d) 21
Answer:b
15. What is the output of this C code?
#include
void main()
{
int h = 8;
int b = (h++, h++);
printf("%d%d\n", b, h);
}
a) 10 10
b) 10 9
c) 9 10
d) 8 10
b) 10 9
c) 9 10
d) 8 10
Answer:c
16. What is the output of this C code?
#include
void main()
{
int h = 8;
int b = h++ + h++ + h++;
printf("%d\n", h);
}
a) 9
b) 10
c) 12
d) 11
b) 10
c) 12
d) 11
Answer:d
17. What is the output of this C code?
#include
void main()
{
int h = 8;
int b = 4 * 6 + 3 * 4 < 3 ? 4 : 3;
printf("%d\n", b);
}
a) 3
b) 33
c) 34
d) Run time error
b) 33
c) 34
d) Run time error
Answer:a
18. What is the output of this C code?
#include
void main()
{
int a = 2 + 3 - 4 + 8 - 5 % 4;
printf("%d\n", a);
}
a) 0
b) 8
c) 11
d) 9
b) 8
c) 11
d) 9
Answer:b
19. What is the output of this C code?
#include
void main()
{
char a = '0';
char b = 'm';
int c = a && b || '1';
printf("%d\n", c);
}
a) 0
b) a
c) 1
d) m
b) a
c) 1
d) m
Answer:c
20. What is the output of this C code?
#include
void main()
{
char a = 'A';
char b = 'B';
int c = a + b % 3 - 3 * 2;
printf("%d\n", c);
}
a) 65
b) 58
c) 64
d) 59
b) 58
c) 64
d) 59
Answer:d