Problem Statement: Which of the following operators are both unary and binary?
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Problem Statement:**
Which of the following operators are both unary and binary?
Pick ONE option
Show answer & explanation
The minus operator - functions as both unary (negation: -x) and binary (subtraction: x - y). The plus operator + is primarily binary for addition, though it can act as a unary operator in some contexts. The asterisk * is primarily used for multiplication (binary) and pointer dereferencing (unary), but in C/C++ it's not traditionally classified as a true binary operator in the same sense. The keyword typedef is not an operator at all.
Step-by-step Derivation:
Analyze each option:
- A)
typedef: A keyword, not an operator. Rejected. - B)
+: Primarily binary (addition:3 + 5 = 8). Can act as unary (positive sign:+5), but this is less common. - C)
-: Clearly unary (negation:-5,-(x+y)). Clearly binary (subtraction:10 - 3 = 7). This is the best answer. - D)
*: Binary for multiplication (3 * 4 = 12), unary for pointer dereferencing (*ptr), but not traditionally called an operator in both contexts in standard operator theory.
The minus sign - is the canonical example of an operator that is both unary and binary in C/C++.