Challenge - 5 Problems
Comparison Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this MATLAB code using comparison operators?
Consider the following MATLAB code snippet. What will be the output displayed in the command window?
MATLAB
a = 5; b = 10; result = (a >= 5) & (b < 10); disp(result);
Attempts:
2 left
💡 Hint
Remember that & is the element-wise AND operator and both conditions must be true.
✗ Incorrect
The first condition (a >= 5) is true (1), but the second condition (b < 10) is false (0). Using & operator, true & false results in false (0). So, the output is 0.
❓ Predict Output
intermediate2:00remaining
What does this MATLAB code output when comparing arrays?
What will be the output of this MATLAB code?
MATLAB
x = [1 2 3 4]; y = [1 3 2 4]; result = x == y; disp(result);
Attempts:
2 left
💡 Hint
The == operator compares elements at the same positions.
✗ Incorrect
The comparison x == y checks each element: 1==1 (true=1), 2==3 (false=0), 3==2 (false=0), 4==4 (true=1). So the output is [1 0 0 1].
❓ Predict Output
advanced2:00remaining
What is the output of this MATLAB code using relational operators with matrices?
Given the following code, what will be the output?
MATLAB
A = [1 4; 3 2]; B = [2 3; 3 5]; result = A < B; disp(result);
Attempts:
2 left
💡 Hint
The < operator compares each element of A with the corresponding element of B.
✗ Incorrect
Comparisons: 1<2 (1), 4<3 (0), 3<3 (0), 2<5 (1). So the result matrix is [1 0; 0 1], which is option D.
❓ Predict Output
advanced2:00remaining
What is the output of this MATLAB code using the '==' operator with strings?
What will this code display?
MATLAB
str1 = 'hello'; str2 = 'Hello'; result = str1 == str2; disp(result);
Attempts:
2 left
💡 Hint
The == operator compares characters by their ASCII codes.
✗ Incorrect
Comparing 'h' (ASCII 104) with 'H' (ASCII 72) is false (0), so option B is incorrect. Actually, the first characters differ, so first element is 0. The rest: 'e'=='e' (1), 'l'=='l' (1), 'l'=='l' (1), 'o'=='o' (1). So output is [0 1 1 1 1].
❓ Predict Output
expert2:00remaining
What is the output of this MATLAB code using logical operators with mixed data types?
Analyze the following code and determine the output.
MATLAB
a = [true false true]; b = [1 0 2]; result = a & (b > 1); disp(result);
Attempts:
2 left
💡 Hint
Logical AND & works element-wise and b > 1 returns a logical array.
✗ Incorrect
b > 1 is [false false true] or [0 0 1]. a is [true false true] or [1 0 1]. Element-wise AND: 1&0=0, 0&0=0, 1&1=1. So result is [0 0 1].