C Programming Questions and Answers – Precedence and Order of Evaluation – 5

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Precedence and Order of Evaluation – 5”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. What will be the output of the following C function?

  1.     #include <stdio.h>
  2.     void reverse(int i);
  3.     int main()
  4.     {
  5.         reverse(1);
  6.     }
  7.     void reverse(int i)
  8.     {
  9.         if (i > 5)
  10.             exit(0);
  11.         printf("%d\n", i);
  12.         return reverse(i++);
  13.     }

a) 1 2 3 4 5
b) 1 2 3 4
c) Compile time error
d) Stack overflow
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

2. What will be the output of the following C function?

  1.     #include <stdio.h>
  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
View Answer

Answer: a
Explanation: None.
advertisement

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
View Answer

Answer: a
Explanation: None.

4. What will be the final values of i and j in the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int x = 0;
  3.     int f()
  4.     {
  5.         if (x == 0)
  6.             return ++x;
  7.         else
  8.             return --x;
  9.     }
  10.     int g()
  11.     {
  12.         return x++;
  13.     }
  14.     int main()
  15.     {
  16.         int i, j;
  17.         i = (f() + g()) || g();
  18.         x = 0;
  19.         j = g() || (f() + g());
  20.     }

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
View Answer

Answer: d
Explanation: Logical OR are evaluated strictly from left-to-right. The ‘+’ operator has two operands f() & g() and these functions f() and g() can get evaluated in any order depending on the compiler. When x is 0, (f() + g()) can return values of either 0 or 2 depending on the order of evaluation of these functions by the compiler. Hence, we can’t have a fixed value for i and j.

5. What will be the final values of i and j in the following C code?

  1.     #include <stdio.h>
  2.     int x = 0;
  3.     int f()
  4.     {
  5.         if (x == 0)
  6.             return x + 1;
  7.         else
  8.             return x - 1;
  9.     }
  10.     int g()
  11.     {
  12.         return x++;
  13.     }
  14.     int main()
  15.     {
  16.         int i = (f() + g()) | g(); //bitwise or
  17.         int j = g() | (f() + g()); //bitwise or
  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
View Answer

Answer: d
Explanation: Bitwise OR are NOT strictly evaluated from left-to-right. Similar the ‘+’ operator has two operands f() & g() and these functions f() and g() can get evaluated in any order depending on the compiler. Hence, we can’t have a fixed value for i and j.

6. What will be the output of the following C code?

  1.     #include <stdio.h>
  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
View Answer

Answer: b
Explanation: None.

7. What will be the output of the following C code?

  1.     #include <stdio.h>
  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
View Answer

Answer: b
Explanation: None.

8. What will be the output of the following C code?

  1.     #include <stdio.h>
  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
View Answer

Answer: b
Explanation: None.

9. What will be the output of the following C code?

  1.     #include <stdio.h>
  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
View Answer

Answer: b
Explanation: None.

10. What will be the output of the following C code?

  1.     #include <stdio.h>
  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
View Answer

Answer: b
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.