Given i = 0, j = 1, k = –1 x = 0.5, y = 0.0 What is the output of the…
2016
Given i = 0, j = 1, k = –1
x = 0.5, y = 0.0
What is the output of the following expression in C language ?
x * y < i + j || k
- A.
- 1
- B.
0
- C.
1
- D.
2
Attempted by 687 students.
Show answer & explanation
Correct answer: C
Concept:
In C, every relational operator (<, >, ==, !=, <=, >=) evaluates to the int value 1 when the comparison is true and 0 when it is false. The logical OR operator (||) also produces only 0 or 1, and it short-circuits: once its left operand is already nonzero (true), the right operand is never evaluated and the result is 1 regardless of that operand's own value. Operator precedence also governs how a mixed expression is grouped: the arithmetic operators (*, +) bind before the relational operator (<), which in turn binds before the logical operator (||).
Application:
By operator precedence, the expression x * y < i + j || k groups as ((x * y) < (i + j)) || k.
Compute the arithmetic sub-expressions: x * y = 0.5 * 0.0 = 0.0, and i + j = 0 + 1 = 1.
Evaluate the relational comparison: 0.0 < 1 is true, which C represents as the integer 1.
Evaluate the logical OR: because the left operand (1) is already nonzero/true, || short-circuits — it does not evaluate k — so the whole expression evaluates to 1.
Cross-check:
Even if k were evaluated, its value (-1) is also nonzero/true, so 1 || (-1) would still be 1 — the short-circuit does not change the outcome here. This also confirms that || in C can only ever produce 0 or 1, never any other magnitude such as 2 or a negative number.
Answer: 1
A video solution is available for this question — log in and enroll to watch it.