Bird
Raised Fist0
MATLABdata~20 mins

First MATLAB program - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MATLAB First Program Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple MATLAB script
What is the output of this MATLAB code when run in the command window?
MATLAB
x = 5;
y = 3;
z = x + y;
disp(z);
Ax + y
B8
C53
DError: Undefined function or variable 'z'.
Attempts:
2 left
💡 Hint
Remember that disp shows the value of the variable, not the expression.
🧠 Conceptual
intermediate
2:00remaining
Understanding MATLAB script execution
Which statement best describes what happens when you run a MATLAB script file?
AMATLAB executes commands line by line in the script from top to bottom.
BMATLAB compiles the script into a standalone program before running.
CMATLAB runs all commands in the script simultaneously.
DMATLAB only checks the script for syntax errors but does not run it.
Attempts:
2 left
💡 Hint
Think about how you read a recipe or instructions.
🔧 Debug
advanced
2:00remaining
Identify the error in this MATLAB code
What error will this MATLAB code produce when run?
MATLAB
a = 10
b = 20;
c = a + b;
disp(c);
ANo error, output is 30
BSyntax error: Missing semicolon after first line
CRuntime error: Undefined variable 'a'
DSyntax error: Missing end statement
Attempts:
2 left
💡 Hint
In MATLAB, semicolons suppress output but are not required for correctness.
❓ Predict Output
advanced
2:00remaining
Output of a MATLAB script with string and numeric variables
What will be displayed when this MATLAB code runs?
MATLAB
name = 'Alice';
age = 25;
message = [name, ' is ', num2str(age), ' years old.'];
disp(message);
AAlice is age years old.
BError: Cannot concatenate string and number directly.
CAlice is 25 years old.
DAlice is 25 years old
Attempts:
2 left
💡 Hint
num2str converts numbers to strings for concatenation.
🚀 Application
expert
2:00remaining
Number of elements in a MATLAB array after operations
Consider this MATLAB code. How many elements does the variable 'result' contain after execution?
MATLAB
arr = 1:10;
result = arr(arr > 5);
A6
BError: Logical indexing not allowed
C10
D5
Attempts:
2 left
💡 Hint
Think about which numbers in arr are greater than 5.

Practice

(1/5)
1. What does the MATLAB command disp('Hello, world!') do?
easy
A. It saves the text Hello, world! to a file.
B. It displays the text Hello, world! on the screen.
C. It creates a variable named Hello, world!.
D. It runs a loop that prints Hello, world! multiple times.

Solution

  1. Step 1: Understand the disp function

    The disp function in MATLAB shows text or values on the screen.
  2. Step 2: Analyze the input to disp

    The input is a string 'Hello, world!' which means it will print this exact text.
  3. Final Answer:

    It displays the text Hello, world! on the screen. -> Option B
  4. Quick Check:

    disp('text') shows text [OK]
Hint: disp shows text inside single quotes [OK]
Common Mistakes:
  • Thinking disp saves text to a file
  • Using double quotes instead of single quotes
  • Confusing disp with variable creation
2. Which of the following is the correct syntax to display the message Hello in MATLAB?
easy
A. disp('Hello')
B. display(Hello)
C. disp(Hello)
D. print('Hello')

Solution

  1. Step 1: Check the correct function name

    MATLAB uses disp to display messages, not display or print.
  2. Step 2: Check the string syntax

    Strings must be inside single quotes in MATLAB, so disp('Hello') is correct.
  3. Final Answer:

    disp('Hello') -> Option A
  4. Quick Check:

    disp with single quotes is correct [OK]
Hint: Use disp with single quotes for text [OK]
Common Mistakes:
  • Using double quotes instead of single quotes
  • Using wrong function names like print or display
  • Missing quotes around text
3. What will be the output of this MATLAB code?
disp('Welcome to MATLAB!')
medium
A. Welcome to MATLAB!
B. disp('Welcome to MATLAB!')
C. SyntaxError
D. No output

Solution

  1. Step 1: Understand disp function behavior

    The disp function prints the string inside the parentheses to the screen.
  2. Step 2: Check the string format

    The string is correctly enclosed in single quotes, so it will print exactly as given.
  3. Final Answer:

    Welcome to MATLAB! -> Option A
  4. Quick Check:

    disp prints string text [OK]
Hint: disp prints text inside single quotes exactly [OK]
Common Mistakes:
  • Expecting disp to print the command itself
  • Using double quotes causing errors
  • Forgetting to use disp function
4. Identify the error in this MATLAB code:
disp(Welcome to MATLAB!)
medium
A. Parentheses are not allowed with disp
B. Missing semicolon at the end
C. disp function does not exist
D. Text is not inside single quotes

Solution

  1. Step 1: Check the input to disp

    The text Welcome to MATLAB! is not inside single quotes, so MATLAB treats it as variables or commands.
  2. Step 2: Understand string syntax in MATLAB

    Strings must be enclosed in single quotes to be recognized as text.
  3. Final Answer:

    Text is not inside single quotes -> Option D
  4. Quick Check:

    Strings need single quotes [OK]
Hint: Always put text inside single quotes in disp [OK]
Common Mistakes:
  • Omitting quotes around text
  • Thinking semicolon is required for disp
  • Believing disp is not a function
5. You want to write a MATLAB program that displays two lines:
Hello
and
Welcome to MATLAB
Which code will do this correctly?
hard
A. disp('Hello\nWelcome to MATLAB')
B. disp('Hello') disp(Welcome to MATLAB)
C. disp('Hello') disp('Welcome to MATLAB')
D. disp('Hello' + 'Welcome to MATLAB')

Solution

  1. Step 1: Check how to display multiple lines

    Using two separate disp calls prints each line separately.
  2. Step 2: Analyze each option

    disp('Hello') disp('Welcome to MATLAB') uses two disp calls with correct strings. disp('Hello\nWelcome to MATLAB') tries to use \n inside single quotes which MATLAB does not interpret as newline. disp('Hello') disp(Welcome to MATLAB) misses quotes in second disp. disp('Hello' + 'Welcome to MATLAB') tries to add strings which is invalid in MATLAB.
  3. Final Answer:

    disp('Hello') disp('Welcome to MATLAB') -> Option C
  4. Quick Check:

    Use separate disp calls for multiple lines [OK]
Hint: Use separate disp lines for multiple messages [OK]
Common Mistakes:
  • Using \n inside single quotes expecting newline
  • Missing quotes around text
  • Trying to add strings with + operator
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