Challenge - 5 Problems
Script Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple script file
What is the output when you run this MATLAB script file named
example.m?MATLAB
a = 5; b = 3; c = a + b; disp(c);
Attempts:
2 left
💡 Hint
Remember that
disp shows the value of the variable, not the expression itself.✗ Incorrect
The script adds 5 and 3, stores in c, then displays c, which is 8.
❓ Predict Output
intermediate2:00remaining
Variable scope in script files
Given this script
myscript.m:
a = 10; b = 20; c = a + b;What will be the value of
c in the MATLAB command window after running myscript?MATLAB
a = 10; b = 20; c = a + b;
Attempts:
2 left
💡 Hint
Script files share the workspace with the command window.
✗ Incorrect
Variables created in scripts appear in the base workspace and can be accessed after running the script.
🔧 Debug
advanced2:00remaining
Identify the error in this script
What error will MATLAB show when running this script file named
test.m?MATLAB
x = 5 if x > 3 disp('x is greater than 3'); end
Attempts:
2 left
💡 Hint
In MATLAB, semicolons are optional to end statements.
✗ Incorrect
MATLAB allows statements without semicolons; the if block is correctly closed with 'end'.
❓ Predict Output
advanced2:00remaining
Effect of clearing variables in script
Consider this script
clear_test.m:
clear x = 7; y = x * 2; disp(y);What will be the output when you run
clear_test?MATLAB
clear x = 7; y = x * 2; disp(y);
Attempts:
2 left
💡 Hint
The
clear command removes variables before new ones are created.✗ Incorrect
The script clears variables, then assigns x=7, y=14, and displays 14.
🧠 Conceptual
expert3:00remaining
Script file behavior with function definitions
What happens if you define a function inside a MATLAB script file and then call it within the same script?
MATLAB
disp(myfunc(3)); function y = myfunc(x) y = x^2; end
Attempts:
2 left
💡 Hint
Since MATLAB R2016b, local functions can be included in scripts.
✗ Incorrect
Modern MATLAB allows local functions in scripts; calling them works and outputs 9.