Bird
Raised Fist0
MATLABdata~20 mins

Relational expressions in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Relational Expressions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of chained relational expressions
What is the output of this MATLAB code snippet?
MATLAB
a = 5;
b = 10;
c = 15;
result = (a < b) & (b < c);
disp(result);
A[1 1]
B0
CError: Undefined function or variable 'result'
D1
Attempts:
2 left
💡 Hint
Recall that relational expressions return logical 1 (true) or 0 (false).
❓ Predict Output
intermediate
2:00remaining
Relational expression with arrays
What is the output of this MATLAB code?
MATLAB
x = [2, 4, 6];
y = [3, 4, 5];
result = x >= y;
disp(result);
A[0 1 1]
B[1 1 1]
C[0 0 0]
DError: Matrix dimensions must agree.
Attempts:
2 left
💡 Hint
Relational operators compare elements element-wise in arrays of the same size.
❓ Predict Output
advanced
2:00remaining
Result of mixing relational and logical operators
What does this MATLAB code display?
MATLAB
a = 7;
b = 5;
c = 7;
result = (a == c) | (b > a);
disp(result);
A[1 0]
B0
C1
DError: Undefined function or variable 'result'
Attempts:
2 left
💡 Hint
The OR operator (|) returns true if either condition is true.
❓ Predict Output
advanced
2:00remaining
Relational expression with mixed data types
What is the output of this MATLAB code?
MATLAB
x = 3;
y = '3';
result = (x == y);
disp(result);
A0
B1
CError: Conversion to double from char is not possible.
DError: Undefined function or variable 'result'
Attempts:
2 left
💡 Hint
MATLAB compares numeric and char types differently in relational expressions.
❓ Predict Output
expert
2:00remaining
Logical indexing with relational expressions
What is the output of this MATLAB code?
MATLAB
A = [10, 20, 30, 40, 50];
idx = A > 25;
result = A(idx);
disp(result);
A[25 30 40 50]
B[30 40 50]
C[10 20 25]
DError: Index exceeds matrix dimensions.
Attempts:
2 left
💡 Hint
Logical indexing selects elements where the condition is true.

Practice

(1/5)
1. Which of the following relational operators in MATLAB checks if two values are NOT equal?
easy
A. ==
B. <=
C. >=
D. ~=

Solution

  1. Step 1: Understand the meaning of relational operators

    The operator == checks equality, ~= checks inequality, and >=, <= check greater or equal and less or equal respectively.
  2. Step 2: Identify the operator for NOT equal

    The operator ~= means NOT equal in MATLAB.
  3. Final Answer:

    ~= -> Option D
  4. Quick Check:

    NOT equal operator = ~= [OK]
Hint: Remember: ~= means NOT equal in MATLAB [OK]
Common Mistakes:
  • Confusing == with ~=
  • Using = instead of ==
  • Mixing up >= and >
2. Which of the following is the correct syntax to check if variable a is greater than or equal to 10 in MATLAB?
easy
A. a >== 10
B. a >= 10
C. a =< 10
D. a => 10

Solution

  1. Step 1: Recall MATLAB relational operator syntax

    MATLAB uses >= to mean greater than or equal to.
  2. Step 2: Check each option for correct syntax

    a >= 10 uses >= correctly. Options A, B, and C use invalid or reversed operators.
  3. Final Answer:

    a >= 10 -> Option B
  4. Quick Check:

    Greater or equal operator = >= [OK]
Hint: Use >= for greater or equal, not => or =< [OK]
Common Mistakes:
  • Writing => instead of >=
  • Writing =< instead of <=
  • Adding extra = signs
3. What is the output of the following MATLAB code?
a = 5; b = 7;
result = (a < b) & (b <= 7);
disp(result);
medium
A. 1
B. 0
C. Error
D. [1 0]

Solution

  1. Step 1: Evaluate each relational expression

    a < b is 5 < 7 which is true (1). b <= 7 is 7 <= 7 which is true (1).
  2. Step 2: Apply logical AND operator &

    Both expressions are true, so (1 & 1) equals 1 (true).
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    True & True = 1 [OK]
Hint: True & True gives 1, false & anything gives 0 [OK]
Common Mistakes:
  • Confusing & with &&
  • Expecting output as logical word instead of 1
  • Misreading <= operator
4. Identify the error in this MATLAB code snippet:
x = 10;
if x = 10
disp('Equal to 10');
end
medium
A. Using = instead of == in if condition
B. Missing semicolon after disp statement
C. Incorrect use of end keyword
D. Variable x is not initialized

Solution

  1. Step 1: Check the if condition syntax

    In MATLAB, = is assignment, == is comparison. Using = in if causes error.
  2. Step 2: Confirm other parts are correct

    Semicolon after disp is optional, end is correctly used, x is initialized.
  3. Final Answer:

    Using = instead of == in if condition -> Option A
  4. Quick Check:

    Comparison needs ==, not = [OK]
Hint: Use == for comparison inside if, not = [OK]
Common Mistakes:
  • Using = instead of ==
  • Thinking semicolon is mandatory after disp
  • Misplacing end keyword
5. Given a vector v = [3, 6, 9, 12], which MATLAB expression returns a logical array indicating elements greater than 5 and less than or equal to 10?
hard
A. v > (5 & v) <= 10
B. v > 5 && v <= 10
C. (v > 5) & (v <= 10)
D. (v > 5) | (v <= 10)

Solution

  1. Step 1: Understand vector relational operations

    Relational operators applied to vectors return logical arrays element-wise.
  2. Step 2: Combine conditions correctly with parentheses and &

    Use parentheses around each condition: (v > 5) & (v <= 10) to get elements satisfying both.
  3. Step 3: Check other options for syntax errors

    v > 5 && v <= 10 uses short-circuit &&, invalid for vectors. v > (5 & v) <= 10 is invalid syntax. (v > 5) | (v <= 10) uses OR instead of AND.
  4. Final Answer:

    (v > 5) & (v <= 10) -> Option C
  5. Quick Check:

    Use parentheses with & for combined conditions [OK]
Hint: Always use parentheses around each relational condition [OK]
Common Mistakes:
  • Using && instead of & for array operations
  • Using | instead of & for AND
  • Writing invalid combined expressions
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