Challenge - 5 Problems
Switch-Case Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple switch-case
What is the output of the following MATLAB code?
MATLAB
x = 3; switch x case 1 disp('One') case 2 disp('Two') case 3 disp('Three') otherwise disp('Other') end
Attempts:
2 left
💡 Hint
Check which case matches the value of x.
✗ Incorrect
The variable x is 3, so the switch matches case 3 and displays 'Three'.
❓ Predict Output
intermediate2:00remaining
Switch-case with string input
What will this MATLAB code display?
MATLAB
color = 'red'; switch color case 'blue' disp('Blue color') case 'red' disp('Red color') case 'green' disp('Green color') otherwise disp('Unknown color') end
Attempts:
2 left
💡 Hint
Look at the value of the variable color.
✗ Incorrect
The variable color is 'red', so the switch matches case 'red' and displays 'Red color'.
❓ Predict Output
advanced2:00remaining
Switch-case with multiple values in one case
What does this MATLAB code print?
MATLAB
val = 5; switch val case {1, 3, 5} disp('Odd number') case {2, 4, 6} disp('Even number') otherwise disp('Other number') end
Attempts:
2 left
💡 Hint
Check which group contains the value 5.
✗ Incorrect
The value 5 is in the first case group {1, 3, 5}, so it prints 'Odd number'.
❓ Predict Output
advanced2:00remaining
Switch-case with numeric and string mismatch
What error or output does this MATLAB code produce?
MATLAB
x = 2; switch x case '2' disp('String two') case 2 disp('Numeric two') otherwise disp('No match') end
Attempts:
2 left
💡 Hint
MATLAB distinguishes between strings and numbers in switch cases.
✗ Incorrect
The variable x is numeric 2, so it matches the numeric case 2, not the string '2'.
❓ Predict Output
expert3:00remaining
Switch-case with cell array and fall-through behavior
What is the output of this MATLAB code?
MATLAB
val = 'b'; switch val case {'a', 'b'} disp('First group') case {'b', 'c'} disp('Second group') otherwise disp('No group') end
Attempts:
2 left
💡 Hint
MATLAB switch stops at the first matching case.
✗ Incorrect
The value 'b' matches the first case group {'a', 'b'}, so it prints 'First group' and does not check further cases.