0
0
MATLABdata~20 mins

Logical operators (&, |, ~) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A
[0 0 0]
[1 1 1]
B
[0 0 1]
[1 0 1]
C
[1 0 1]
[1 0 1]
D
[1 1 1]
[0 0 0]
Attempts:
2 left
💡 Hint
Remember that & returns true only if both elements are true, | returns true if at least one is true.
Predict Output
intermediate
1:30remaining
Output of logical NOT operation
What does the following MATLAB code display?
MATLAB
X = [true false true false];
Y = ~X;
disp(Y);
A[false true false true]
B[true false true false]
C[1 0 1 0]
D[0 1 0 1]
Attempts:
2 left
💡 Hint
The ~ operator flips true to false and false to true.
🔧 Debug
advanced
2: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;
ADimension mismatch error
BSyntax error
CNo error, outputs [0 0 1]
DIndex exceeds matrix dimensions
Attempts:
2 left
💡 Hint
Logical operators require arrays to be the same size or compatible.
Predict Output
advanced
2: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);
A[0 0 0 0]
B[0 1 1 0]
C[1 1 0 1]
D[1 1 1 1]
Attempts:
2 left
💡 Hint
Break down the expression step by step: compute A & B, then negate, then compute A & ~B, then OR the results.
🧠 Conceptual
expert
1:30remaining
Understanding precedence of logical operators
Given the expression in MATLAB: true | false & false, what is the result?
Atrue
Bfalse
CError due to ambiguous operators
DDepends on variable types
Attempts:
2 left
💡 Hint
Remember that & has higher precedence than | in MATLAB.