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?
#include <stdio.h>
void reverse(int i);
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
View Answer
Explanation: None.
2. What will be the output of the following C function?
#include <stdio.h>
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
View Answer
Explanation: None.
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
Explanation: None.
4. What will be the final values of i and j in the following C code?
#include <stdio.h>
int x = 0;
int f()
{
if (x == 0)
return ++x;
else
return --x;
}
int g()
{
return x++;
}
int main()
{
int i, j;
i = (f() + g()) || g();
x = 0;
j = g() || (f() + g());
}
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
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?
#include <stdio.h>
int x = 0;
int f()
{
if (x == 0)
return x + 1;
else
return x - 1;
}
int g()
{
return x++;
}
int main()
{
int i = (f() + g()) | g(); //bitwise or
int j = g() | (f() + g()); //bitwise or
}
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
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?
#include <stdio.h>
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
View Answer
Explanation: None.
7. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
8. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
9. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
10. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
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.
- Practice BCA MCQs
- Check Computer Science Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship