Bird
Raised Fist0
MATLABdata~20 mins

String formatting (sprintf) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
sprintf Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of sprintf with integer and float formatting
What is the output of the following MATLAB code?
str = sprintf('Value: %d, Pi approx: %.2f', 7, 3.14159); disp(str);
MATLAB
str = sprintf('Value: %d, Pi approx: %.2f', 7, 3.14159); disp(str);
AValue: 7, Pi approx: 3.14159
BValue: 7, Pi approx: 3.14
CValue: 7, Pi approx: 3.1
DValue: 7, Pi approx: 3
Attempts:
2 left
💡 Hint
Look at the %.2f format specifier; it rounds to two decimal places.
❓ Predict Output
intermediate
2:00remaining
Using sprintf to format a date string
What will be the output of this MATLAB code?
day = 5; month = 9; year = 2024; str = sprintf('Date: %02d/%02d/%04d', day, month, year); disp(str);
MATLAB
day = 5; month = 9; year = 2024; str = sprintf('Date: %02d/%02d/%04d', day, month, year); disp(str);
ADate: 5/9/2024
BDate: 05/9/2024
CDate: 05/09/2024
DDate: 5/09/2024
Attempts:
2 left
💡 Hint
The %02d means pad with zeros to 2 digits, %04d pads to 4 digits.
❓ Predict Output
advanced
2:00remaining
Output of sprintf with mixed format specifiers
What is the output of this MATLAB code?
str = sprintf('Hex: %X, Oct: %o, Char: %c', 255, 8, 65); disp(str);
MATLAB
str = sprintf('Hex: %X, Oct: %o, Char: %c', 255, 8, 65); disp(str);
AHex: FF, Oct: 10, Char: A
BHex: ff, Oct: 8, Char: 65
CHex: FF, Oct: 8, Char: A
DHex: 255, Oct: 10, Char: 65
Attempts:
2 left
💡 Hint
Remember %X prints uppercase hex, %o prints octal, %c prints character from ASCII code.
❓ Predict Output
advanced
2:00remaining
sprintf with width and left alignment
What will this MATLAB code display?
str = sprintf('|%-10s|%5d|', 'Hi', 42); disp(str);
MATLAB
str = sprintf('|%-10s|%5d|', 'Hi', 42); disp(str);
A|Hi | 42|
B| Hi| 42|
C|Hi |42 |
D|Hi| 42|
Attempts:
2 left
💡 Hint
%-10s means left-align string in 10 spaces; %5d means right-align integer in 5 spaces.
🧠 Conceptual
expert
2:00remaining
Understanding sprintf behavior with multiple format specifiers
Consider this MATLAB code:
str = sprintf('%d %d %d', 1, 2); disp(str);

What happens when you run this code?
AIt prints '1 2' ignoring the last format specifier.
BIt prints '1 2 0' because missing arguments default to zero.
CIt prints '1 2 2' repeating the last argument.
DIt raises an error because there are fewer input arguments than format specifiers.
Attempts:
2 left
💡 Hint
Check how MATLAB handles missing arguments for sprintf.

Practice

(1/5)
1. What does the sprintf function in MATLAB do?
easy
A. Creates a formatted string by inserting values into a template
B. Reads input from the user
C. Plots data on a graph
D. Saves variables to a file

Solution

  1. Step 1: Understand the purpose of sprintf

    sprintf is used to create text with values inserted in a specific format.
  2. Step 2: Compare options to function behavior

    Only Creates a formatted string by inserting values into a template describes creating formatted strings, which matches sprintf's role.
  3. Final Answer:

    Creates a formatted string by inserting values into a template -> Option A
  4. Quick Check:

    sprintf formats strings = A [OK]
Hint: Remember: sprintf formats text, not input or files [OK]
Common Mistakes:
  • Confusing sprintf with input functions
  • Thinking sprintf plots graphs
  • Assuming sprintf saves files
2. Which of the following is the correct syntax to format an integer variable num using sprintf in MATLAB?
easy
A. str = sprintf('%s', num);
B. str = sprintf('%c', num);
C. str = sprintf('%d', num);
D. str = sprintf('%f', num);

Solution

  1. Step 1: Identify format specifier for integers

    The format specifier %d is used for integers in sprintf.
  2. Step 2: Match specifier with variable type

    Since num is an integer, %d is correct; others are for strings, floats, or characters.
  3. Final Answer:

    str = sprintf('%d', num); -> Option C
  4. Quick Check:

    Integer format = %d = D [OK]
Hint: Use %d for integers, %s for strings, %f for floats [OK]
Common Mistakes:
  • Using %s for numbers
  • Using %f for integers
  • Using %c for non-character data
3. What is the output of the following MATLAB code?
str = sprintf('Value: %0.2f', 3.14159); disp(str);
medium
A. Value: 3.1
B. Value: 3.14
C. Value: 3.14159
D. Value: 03.14

Solution

  1. Step 1: Understand the format specifier %0.2f

    This means a floating-point number with 2 digits after the decimal point, padded with zeros if needed.
  2. Step 2: Apply formatting to 3.14159

    Rounded to 2 decimals, 3.14159 becomes 3.14, no extra zero padding before the number.
  3. Final Answer:

    Value: 3.14 -> Option B
  4. Quick Check:

    %.2f rounds float to 2 decimals = Value: 3.14 [OK]
Hint: %.2f rounds float to 2 decimals in sprintf [OK]
Common Mistakes:
  • Expecting full decimal without rounding
  • Confusing zero padding before decimal
  • Ignoring rounding rules
4. Identify the error in this MATLAB code:
str = sprintf('Name: %s, Age: %d', 25, 'John');
medium
A. sprintf cannot format multiple variables
B. Missing semicolon at the end
C. Incorrect format specifier for string
D. The order of variables does not match format specifiers

Solution

  1. Step 1: Check format specifiers and variable order

    The format expects a string first (%s) then an integer (%d), but variables are 25 (int) then 'John' (string).
  2. Step 2: Identify mismatch cause

    Variables are swapped, causing wrong data types for specifiers.
  3. Final Answer:

    The order of variables does not match format specifiers -> Option D
  4. Quick Check:

    Variable order must match specifiers = A [OK]
Hint: Match variable order exactly to format specifiers [OK]
Common Mistakes:
  • Swapping variable order
  • Ignoring type mismatch
  • Assuming sprintf auto-corrects order
5. You want to create a formatted string showing a product name and price with 2 decimals, like "Product: Laptop, Price: $999.99". Which sprintf call correctly formats name = 'Laptop' and price = 999.987?
hard
A. sprintf('Product: %s, Price: $%0.2f', name, price)
B. sprintf('Product: %s, Price: $%d', price, name)
C. sprintf('Product: %s, Price: $%f', name, price)
D. sprintf('Product: %s, Price: $%.2d', name, price)

Solution

  1. Step 1: Identify correct format specifiers

    %s for string name, %0.2f for float price with 2 decimals.
  2. Step 2: Check variable order and formatting

    sprintf('Product: %s, Price: $%0.2f', name, price) uses correct order and formats price to 2 decimals, rounding 999.987 to 999.99.
  3. Final Answer:

    sprintf('Product: %s, Price: $%0.2f', name, price) -> Option A
  4. Quick Check:

    String + float with 2 decimals = B [OK]
Hint: Use %s for strings and %0.2f for 2-decimal floats [OK]
Common Mistakes:
  • Swapping variable order
  • Using %d for floats
  • Using incorrect precision specifier
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