Challenge - 5 Problems
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of logical AND and OR operations
What is the output of the following MATLAB code?
MATLAB
A = [1 0 1]; B = [0 0 1]; C = A & B; D = A | B; disp(C); disp(D);
Attempts:
2 left
💡 Hint
Remember that & returns true only if both elements are true, | returns true if at least one is true.
✗ Incorrect
The logical AND (&) compares each element: 1&0=0, 0&0=0, 1&1=1. The logical OR (|) compares each element: 1|0=1, 0|0=0, 1|1=1.
❓ Predict Output
intermediate1:30remaining
Output of logical NOT operation
What does the following MATLAB code display?
MATLAB
X = [true false true false]; Y = ~X; disp(Y);
Attempts:
2 left
💡 Hint
The ~ operator flips true to false and false to true.
✗ Incorrect
The ~ operator negates each logical element: true becomes false, false becomes true.
🔧 Debug
advanced2:00remaining
Identify the error in logical expression
What error does this MATLAB code produce?
MATLAB
A = [1 0 1]; B = [0 1]; C = A & B;
Attempts:
2 left
💡 Hint
Logical operators require arrays to be the same size or compatible.
✗ Incorrect
The arrays A and B have different lengths, so element-wise logical AND (&) cannot be performed, causing a dimension mismatch error.
❓ Predict Output
advanced2:30remaining
Result of combined logical operations
What is the output of this MATLAB code?
MATLAB
A = [1 0 1 0]; B = [0 1 1 0]; C = ~(A & B) | (A & ~B); disp(C);
Attempts:
2 left
💡 Hint
Break down the expression step by step: compute A & B, then negate, then compute A & ~B, then OR the results.
✗ Incorrect
A & B = [0 0 1 0], ~(A & B) = [1 1 0 1], A & ~B = [1 0 0 0], OR gives [1 1 0 1] | [1 0 0 0] = [1 1 0 1]. But option C is [1 1 1 1], so re-check: Actually, A & ~B = [1 0 0 0], so final OR is [1 1 0 1]. So option C matches. Correct answer is B.
🧠 Conceptual
expert1:30remaining
Understanding precedence of logical operators
Given the expression in MATLAB: true | false & false, what is the result?
Attempts:
2 left
💡 Hint
Remember that & has higher precedence than | in MATLAB.
✗ Incorrect
The expression is evaluated as true | (false & false). false & false is false, so true | false is true.