In C/C++/Java-style boolean expressions, match the following operators to their meanings: (A) &&, (B) ||, (C) = — identify logical AND, logical OR, and assignment.
-
AA-1, B-2, C-3
-
BA-2, B-3, C-1
-
CA-3, B-2, C-1
-
DA-1, B-3, C-2
Answer
Correct Answer: A-2, B-3, C-1
Explanation
Introduction / Context:Many programming languages use distinct operators for boolean logic and for assignment. Misreading these symbols is a common source of bugs. This matching task reinforces the difference between logical conjunction/disjunction and variable assignment in mainstream C-like languages.
Given Data / Assumptions:
- Operator '&&': used in conditional expressions as logical AND.
- Operator '||': used as logical OR.
- Operator '=': used to assign a value to a variable (single equals is not comparison).
- Short-circuit evaluation semantics typically apply to && and ||.
Concept / Approach:Disambiguate symbols by role: logical operators combine boolean results; assignment stores a value. Remember that equality comparison is '==' (not '='), which prevents confusing assignment with testing for equality.
Step-by-Step Solution:
Map '&&' → Logical AND → A-2.Map '||' → Logical OR → B-3.Map '=' → Assignment operator → C-1.Verification / Alternative check:Consider code: if (x > 0 && y > 0) {...} executes only if both comparisons are true; flag = (x > 0 || y > 0); assigns a boolean result to flag. Using a single '=' inside an if-condition would change a value and is typically flagged by compilers or linters.
Why Other Options Are Wrong:
- A-1, B-2, C-3: Swaps logical AND with assignment and mislabels OR.
- A-3, B-2, C-1: Interchanges AND/OR semantics.
- A-1, B-3, C-2: Assigns '=' to logical OR and '||' to assignment—both incorrect.
Common Pitfalls:
- Writing 'if (x = 1)' when you meant 'if (x == 1)', causing unintended assignment.
- Forgetting short-circuiting: in A && B, B is evaluated only if A is true; in A || B, B is evaluated only if A is false.
Final Answer:A-2, B-3, C-1