Challenge - 5 Problems
sprintf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sprintf with numeric formatting
What is the output of the following R code using
sprintf to format numbers?R Programming
result <- sprintf("%.2f", 3.14159) print(result)
Attempts:
2 left
💡 Hint
Remember that %.2f rounds the number to 2 decimal places.
✗ Incorrect
The format %.2f rounds the number to two decimal places. 3.14159 rounded to two decimals is 3.14.
❓ Predict Output
intermediate2:00remaining
String padding with sprintf
What will be the output of this R code that uses
sprintf to pad a string?R Programming
result <- sprintf("%10s", "cat") print(result)
Attempts:
2 left
💡 Hint
The number 10 means the total width of the string including spaces.
✗ Incorrect
The format %10s right-aligns the string in a field of width 10, padding with spaces on the left.
❓ Predict Output
advanced2:00remaining
Multiple values formatting with sprintf
What is the output of this R code that formats multiple values with
sprintf?R Programming
result <- sprintf("Name: %s, Age: %d, Score: %.1f", "Alice", 30, 95.678) print(result)
Attempts:
2 left
💡 Hint
%.1f rounds the number to one decimal place.
✗ Incorrect
The score 95.678 rounded to one decimal place is 95.7, so the formatted string shows that.
❓ Predict Output
advanced2:00remaining
Using sprintf with hexadecimal formatting
What will this R code print when formatting a number as hexadecimal using
sprintf?R Programming
result <- sprintf("%x", 255) print(result)
Attempts:
2 left
💡 Hint
%x formats the number as lowercase hexadecimal without prefix.
✗ Incorrect
The number 255 in hexadecimal is ff in lowercase, which %x produces without 0x prefix.
🧠 Conceptual
expert3:00remaining
Understanding sprintf format specifiers
Which
sprintf format specifier correctly formats a floating-point number with at least 8 characters wide, 3 decimal places, and left-aligned?Attempts:
2 left
💡 Hint
The minus sign (-) left-aligns the output in the field width.
✗ Incorrect
The format %-8.3f means: floating number with 3 decimals, left-aligned in 8 spaces wide field.