0
0
R Programmingprogramming~20 mins

String formatting with sprintf in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
sprintf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A"3.14"
B"3.1416"
C"3.1"
D"3.15"
Attempts:
2 left
💡 Hint
Remember that %.2f rounds the number to 2 decimal places.
Predict Output
intermediate
2: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)
A"cat"
B"cat "
C" cat "
D" cat"
Attempts:
2 left
💡 Hint
The number 10 means the total width of the string including spaces.
Predict Output
advanced
2: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)
A"Name: Alice, Age: 30, Score: 95"
B"Name: Alice, Age: 30, Score: 95.6"
C"Name: Alice, Age: 30, Score: 95.7"
D"Name: Alice, Age: 30, Score: 95.67"
Attempts:
2 left
💡 Hint
%.1f rounds the number to one decimal place.
Predict Output
advanced
2: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)
A"FF"
B"ff"
C"255"
D"0xff"
Attempts:
2 left
💡 Hint
%x formats the number as lowercase hexadecimal without prefix.
🧠 Conceptual
expert
3: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?
A"%-8.3f"
B"%8.3f"
C"%08.3f"
D"%+8.3f"
Attempts:
2 left
💡 Hint
The minus sign (-) left-aligns the output in the field width.