Here is a listing of C language Objective Questions “Precedence and Order of Evaluation” along with answers, explanations and/or solutions:
1. What will be the output of the following C function?
-
#include -
int main()
-
{ -
reverse(1);
-
} -
void reverse(int i)
-
{ -
if (i > 5)
-
exit(0);
-
printf("%dn", i);
-
return reverse(i++);
-
}
a) 1 2 3 4 5
b) 1 2 3 4
c) Compile time error
d) Stack overflow
Answer: d
Clarification: None.
2. What will be the output of the following C function?
-
#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
Answer: a
Clarification: 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
Answer: a
Clarification: None.
4. What will be the final values of i and j in the following C 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
Answer: d
Clarification: None.
5. What will be the final values of i and j in the following C 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
Answer: c
Clarification: None.
6. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 2, y = 0;
-
int z = y && (y |= 10);
-
printf("%dn", z);
-
return 0;
-
}
a) 1
b) 0
c) Undefined behaviour due to order of evaluation
d) 2
Answer: b
Clarification: None.
7. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 2, y = 0;
-
int z = (y++) ? 2 : y == 1 && x;
-
printf("%dn", z);
-
return 0;
-
}
a) 0
b) 1
c) 2
d) Undefined behaviour
Answer: b
Clarification: None.
8. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 2, y = 0;
-
int z;
-
z = (y++, y);
-
printf("%dn", z);
-
return 0;
-
}
a) 0
b) 1
c) Undefined behaviour
d) Compilation error
Answer: b
Clarification: None.
9. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 2, y = 0, l;
-
int z;
-
z = y = 1, l = x && y;
-
printf("%dn", l);
-
return 0;
-
}
a) 0
b) 1
c) Undefined behaviour due to order of evaluation can be different
d) Compilation error
Answer: b
Clarification: None.
10. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int y = 2;
-
int z = y +(y = 10);
-
printf("%dn", z);
-
}
a) 12
b) 20
c) 4
d) Either 12 or 20
Answer: b
Clarification: None.
