C#.NET — Understand logical OR (||), short-circuit evaluation, and when an assignment executes. Given: if (age > 18 || no < 11) a = 25; Which statements are correct?

Difficulty: Easy

Correct Answer: 1, 3, 4

Explanation:


Introduction / Context:
This item checks your grasp of the logical OR operator (||) in C#, particularly short-circuiting behavior and the exact condition under which the if body executes. Distinguishing which operand is evaluated in each scenario is the key.


Given Data / Assumptions:

  • Statement: if (age > 18 || no < 11) a = 25;
  • Five claims about evaluation order and execution.


Concept / Approach:
For p || q: if p is true, q is not evaluated (short-circuit). If p is false, q is evaluated, and the whole expression is true when either operand is true. The assignment runs when the compound condition is true (i.e., at least one subcondition is true).


Step-by-Step Solution:

Statement 1: "no < 11 is evaluated only if age > 18 is False." — Correct. Statement 2: "no < 11 is evaluated if age > 18 is True." — Incorrect; short-circuit prevents it. Statement 3: "a = 25 executes if any one condition is True." — Correct. Statement 4: "|| is a short-circuiting logical operator." — Correct. Statement 5: "a = 25 executes only if both are True." — Incorrect; that is AND behavior.


Verification / Alternative check:
Truth table confirms that p || q is true if p or q is true, and q is skipped when p is true.


Why Other Options Are Wrong:

  • A includes 5 (false).
  • B includes 2 (false).
  • D includes 2 and 5 (both false).
  • E is wrong because a correct set exists.


Common Pitfalls:
Mixing up || with && or assuming both sides always evaluate.


Final Answer:
1, 3, 4

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion