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

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?
  1.     #include 
  2.     int main()
  3.     {
  4.         reverse(1);
  5.     }
  6.     void reverse(int i)
  7.     {
  8.         if (i > 5)
  9.             exit(0);
  10.         printf("%d\n", i);
  11.         return reverse(i++);
  12.     }
a) 1 2 3 4 5
b) 1 2 3 4
c) Compile time error
d) Stack overflow
Answer:d
2. What is the output of this C code?
  1.     #include 
  2.     void reverse(int i);
  3.     int main()
  4.     {
  5.         reverse(1);
  6.     }
  7.     void reverse(int i)
  8.     {
  9.         if (i > 5)
  10.             return ;
  11.         printf("%d ", i);
  12.         return reverse((i++, i));
  13.     }
a) 1 2 3 4 5
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?
  1.     #include 
  2.     int x = 0;
  3.     int main()
  4.     {
  5.         int i = (f() + g()) || g();
  6.         int j = g() || (f() + g());
  7.     }
  8.     int f()
  9.     {
  10.         if (x == 0)
  11.             return x + 1;
  12.         else
  13.             return x - 1;
  14.     }
  15.     int g()
  16.     {
  17.         return x++;
  18.     }
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
Answer:d
5. What is the value of i and j in the below code?
  1.     #include 
  2.     int x = 0;
  3.     int main()
  4.     {
  5.         int i = (f() + g()) | g(); //bitwise or
  6.         int j = g() | (f() + g()); //bitwise or
  7.     }
  8.     int f()
  9.     {
  10.         if (x == 0)
  11.             return x + 1;
  12.         else
  13.             return x - 1;
  14.     }
  15.     int g()
  16.     {
  17.         return x++;
  18.     }
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
Answer:c
6. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 2, y = 0;
  5.         int z = y && (y |= 10);
  6.         printf("%d\n", z);
  7.         return 0;
  8.     }
a) 1
b) 0
c) Undefined behaviour due to order of evaluation
d) 2
Answer:b
7. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 2, y = 0;
  5.         int z = (y++) ? 2 : y == 1 && x;
  6.         printf("%d\n", z);
  7.         return 0;
  8.     }
a) 0
b) 1
c) 2
d)Undefined behaviour
Answer:b
8. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 2, y = 0;
  5.         int z;
  6.         z = (y++, y);
  7.         printf("%d\n", z);
  8.         return 0;
  9.     }
a) 0
b) 1
c) Undefined behaviour
d) Compilation error
Answer:b
9. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 2, y = 0, l;
  5.         int z;
  6.         z = y = 1, l = x && y;
  7.         printf("%d\n", l);
  8.         return 0;
  9.     }
a) 0
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?
  1.     #include 
  2.     int main()
  3.     {
  4.         int y = 2;
  5.         int z = y +(y = 10);
  6.         printf("%d\n", z);
  7.     }
a) 12
b) 20
c) 4
d) Either 12 or 20
Answer:b
11. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 - 4 + 2 * 5;
  5.         printf("%d", b);
  6.     }
a) 25
b) -5
c) 11
d) None of the mentioned
Answer:c 
12. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 & 4 & 6;
  5.         printf("%d", b);
  6.     }
a) 5
b) 6
c) 3
d) 4
Answer:d 
13. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 & 4 | 6;
  5.         printf("%d", b);
  6.     }
a) 6
b) 4
c) 1
d) 0
Answer:a 
14. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 + 7 * 4 - 9 * (3, 2);
  5.         printf("%d", b);
  6.     }
a) 6
b) 15
c) 13
d) 21
Answer:b 
15. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = (h++, h++);
  6.         printf("%d%d\n", b, h);
  7.     }
a) 10 10
b) 10 9
c) 9 10
d) 8 10
Answer:c 
16. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = h++ + h++ + h++;
  6.         printf("%d\n", h);
  7.     }
a) 9
b) 10
c) 12
d) 11
Answer:d 
17. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = 4 * 6 + 3 * 4 < 3 ? 4 : 3;
  6.         printf("%d\n", b);
  7.     }
a) 3
b) 33
c) 34
d) Run time error
Answer:a 
18. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int a = 2 + 3 - 4 + 8 -  5 % 4;
  5.         printf("%d\n", a);
  6.     }
a) 0
b) 8
c) 11
d) 9
Answer:b 
19. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         char a = '0';
  5.         char b = 'm';
  6.         int c = a && b || '1';
  7.         printf("%d\n", c);
  8.     }
a) 0
b) a
c) 1
d) m
Answer:c 
20. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         char a = 'A';
  5.         char b = 'B';
  6.         int c = a + b % 3 - 3 * 2;
  7.         printf("%d\n", c);
  8.     }
a) 65
b) 58
c) 64
d) 59
Answer:d

Related

Multiple Choice Questions 3472522792831165966

Post a Comment

emo-but-icon

item