C Programming Questions and Answers on Precedence and Order of Evaluation for Freshers

https://www.computersprofessor.com/2017/11/c-programming-questions-and-answers-on.html
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
int main()
{
int x = 2, y = 2;
float f = y + x /= x / y;
printf("%d %f\n", x, f);
return 0;
}
a) 2 4.000000
b) Compile time error
c) 2 3.500000
d) Undefined behaviour
b) Compile time error
c) 2 3.500000
d) Undefined behaviour
Answer: b
12. What is the output of this C code?
#include
int main()
{
int x = 1, y = 2;
if (x && y == 1)
printf("true\n");
else
printf("false\n");
}
a) true
b) false
c) compile time error
d) undefined behaviour
b) false
c) compile time error
d) undefined behaviour
Answer: b
13. What is the output of this C code?
#include
int main()
{
int x = 1, y = 2;
int z = x & y == 2;
printf("%d\n", z);
}
a) 0
b) 1
c) Compile time error
d) Undefined behaviour
b) 1
c) Compile time error
d) Undefined behaviour
Answer: b
14. What is the output of this C code?
#include
int main()
{
int x = 3, y = 2;
int z = x /= y %= 2;
printf("%d\n", z);
}
a) 1
b) Compile time error
c) Floating point exception
d) Segmentation fault
b) Compile time error
c) Floating point exception
d) Segmentation fault
Answer: c
15. What is the output of this C code?
#include
int main()
{
int x = 3, y = 2;
int z = x << 1 > 5;
printf("%d\n", z);
}
a) 1
b) 0
c) 3
d) Compile time error
b) 0
c) 3
d) Compile time error
Answer: a
16. What is the output of this C code?
#include
int main()
{
int x = 3; //, y = 2;
const int *p = &x;
*p++;
printf("%d\n", *p);
}
a) Increment of read-only location compile error
b) 4
c) Some garbage value
d) Undefined behaviour
b) 4
c) Some garbage value
d) Undefined behaviour
Answer: c
17. What is the output of this C code?
#include
int main()
{
int x = 2, y = 2;
int z = x ^ y & 1;
printf("%d\n", z);
}
a) 1
b) 2
c) 0
d) 1 or 2
b) 2
c) 0
d) 1 or 2
Answer: b
18. What is the output of this C code?
#include
int main()
{
int x = 2, y = 0;
int z = x && y = 1;
printf("%d\n", z);
}
a) 0
b) 1
c) Compile time error
d) 2
b) 1
c) Compile time error
d) 2
Answer: c
.
.
19. What is the output of the code given below
#include
int main()
{
int x = 0, y = 2;
if (!x && y)
printf("true\n");
else
printf("false\n");
}
a) true
b) false
c) compile time error
d) undefined behaviour
b) false
c) compile time error
d) undefined behaviour
Answer: a
20. What is the output of this C code?
#include
int main()
{
int x = 0, y = 2;
int z = ~x & y;
printf("%d\n", z);
}
a) -1
b) 2
c) 0
d) Compile time error
b) 2
c) 0
d) Compile time error
Answer: b
21. What is the output of this C code?
#include
void main()
{
int a = 5 * 3 + 2 - 4;
printf("%d", a);
}
a) 13
b) 14
c) 12
d) 1 6
b) 14
c) 12
d) 1 6
Answer: a
22. What is the output of this C code?
#include
void main()
{
int a = 2 + 4 + 3 * 5 / 3 - 5;
printf("%d", a);
}
a) 7
b) 6
c) 10
d) 9
b) 6
c) 10
d) 9
Answer: b
23. What is the output of this C code?
#include
void main()
{
int a = 5 * 3 % 6 - 8 + 3;
printf("%d", a);
}
a) 10
b) 2
c) -2
d) -3
b) 2
c) -2
d) -3
Answer: c
24. What is the output of this C code?
#include
void main()
{
int b = 6;
int c = 7;
int a = ++b + c--;
printf("%d", a);
}
a) Run time error
b) 15
c) 13
d) 14
b) 15
c) 13
d) 14
Answer: d
25. What is the output of this C code?
#include
void main(
{
double b = 8;
b++;
printf("%lf", b);
}
a) 9.000000
b) 9
c) 9.0
d) Run time error
b) 9
c) 9.0
d) Run time error
Answer: a
26. What is the output of this C code?
#include
void main()
{
double b = 3 % 0 * 1 - 4 / 2;
printf("%lf", b);
}
a) -2
b) Floating point Exception
c) 1
d) None of the mentioned
b) Floating point Exception
c) 1
d) None of the mentioned
Answer: b
27. What is the output of this C code?
#include
void main()
{
double b = 5 % 3 & 4 + 5 * 6;
printf("%lf", b);
}
a) 2
b) 30
c) 2.000000
d) Run time error
b) 30
c) 2.000000
d) Run time error
Answer: c
28. What is the output of this C code?
#include
void main()
{
double b = 3 && 5 & 4 % 3;
printf("%lf", b);
}
a) 3.000000
b) 4.000000
c) 5.000000
d) 1.000000
b) 4.000000
c) 5.000000
d) 1.000000
Answer: d
.
.
29. What is the output of this C code?
#include
void main()
{
double b = 5 & 3 && 4 || 5 | 6;
printf("%lf", b);
}
a) 1.000000
b) 0.000000
c) 7.000000
d) 2.000000
b) 0.000000
c) 7.000000
d) 2.000000
Answer: a
30. What is the output of this C code?
#include
void main()
{
int k = 0;
double b = k++ + ++k + k--;
printf("%d", k);
}
a) 6
b) 1
c) 5
d) undefined
b) 1
c) 5
d) undefined
Answer: d