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

«
»

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

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

advertisement

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

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

Answer: d
Explanation: None.
advertisement
advertisement

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

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

Answer: d
Explanation: None.

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

Answer: c
Explanation: None.

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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.