Bird
Raised Fist0
MATLABdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which logical operator in MATLAB returns true only if both conditions are true?
easy
A. ~ (NOT)
B. & (AND)
C. | (OR)
D. ^ (XOR)

Solution

  1. Step 1: Understand the AND operator

    The & operator returns true only when both conditions it compares are true.
  2. Step 2: Compare with other operators

    The | operator returns true if at least one condition is true, ~ negates a condition, and ^ is XOR which returns true if exactly one condition is true.
  3. Final Answer:

    & (AND) -> Option B
  4. Quick Check:

    Both true = true means AND = & [OK]
Hint: AND means both true, use & operator [OK]
Common Mistakes:
  • Confusing & with | operator
  • Thinking ~ returns true if both true
  • Mixing XOR (^) with AND (&)
2. Which of the following is the correct syntax to negate a logical variable a in MATLAB?
easy
A. not a
B. !a
C. ~a
D. a~

Solution

  1. Step 1: Recall MATLAB negation syntax

    In MATLAB, the negation operator is ~ placed before the variable to flip its logical value.
  2. Step 2: Check each option

    not a uses 'not' which is not valid syntax, C uses '!' which is not MATLAB syntax, and D places '~' after the variable which is incorrect.
  3. Final Answer:

    ~a -> Option C
  4. Quick Check:

    Negate with ~ before variable [OK]
Hint: Use ~ before variable to negate in MATLAB [OK]
Common Mistakes:
  • Using ! instead of ~ for negation
  • Writing negation after variable
  • Using 'not' keyword which is invalid
3. What is the output of the following MATLAB code?
a = true; b = false; c = a & ~b;
medium
A. true
B. false
C. 1
D. 0

Solution

  1. Step 1: Evaluate ~b

    Since b is false, ~b is true.
  2. Step 2: Evaluate a & ~b

    Both a and ~b are true, so a & ~b is true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    true AND true = true [OK]
Hint: Negate false to true, then AND with true gives true [OK]
Common Mistakes:
  • Confusing logical true/false with numeric 1/0
  • Forgetting to negate b
  • Assuming & acts like | (OR)
4. Identify the error in this MATLAB code snippet:
a = true;
b = false;
c = a | ~ b;
medium
A. No error, code runs correctly
B. Incorrect use of | operator
C. Extra space between ~ and b causes error
D. Missing parentheses around ~b

Solution

  1. Step 1: Check operator usage

    The | operator is correctly used to perform logical OR between a and ~b.
  2. Step 2: Check negation syntax

    The negation operator ~ can have a space before the variable; MATLAB ignores spaces here.
  3. Final Answer:

    No error, code runs correctly -> Option A
  4. Quick Check:

    Logical OR with negation syntax is valid [OK]
Hint: Spaces around ~ do not cause errors in MATLAB [OK]
Common Mistakes:
  • Thinking ~ needs parentheses always
  • Assuming space causes syntax error
  • Confusing | with & operator
5. Given logical variables x = [true false true] and y = [false true true], what is the result of z = ~(x & y) | (x | y) in MATLAB?
hard
A. [false false false]
B. [false true true]
C. [true false true]
D. [true true true]

Solution

  1. Step 1: Calculate x & y element-wise

    x = [true false true], y = [false true true]
    x & y = [true & false, false & true, true & true] = [false, false, true]
  2. Step 2: Negate x & y

    ~(x & y) = [~false, ~false, ~true] = [true, true, false]
  3. Step 3: Calculate x | y element-wise

    x | y = [true | false, false | true, true | true] = [true, true, true]
  4. Step 4: Calculate final expression

    z = ~(x & y) | (x | y) = [true | true, true | true, false | true] = [true, true, true]
  5. Final Answer:

    [true true true] -> Option D
  6. Quick Check:

    Element-wise logic with ~, &, | gives all true [OK]
Hint: Calculate & first, then ~, then | element-wise [OK]
Common Mistakes:
  • Forgetting element-wise operation
  • Negating before AND instead of after
  • Mixing up & and | precedence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard