Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to format the number 5 as a string using sprintf.
R Programming
result <- sprintf([1], 5)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%f" formats as floating point with decimals.
Using "%s" expects a string input, not a number.
Using "%x" formats as hexadecimal.
✗ Incorrect
The "%d" format specifier formats the number as an integer string.
2fill in blank
mediumComplete the code to format the number 3.14159 to two decimal places using sprintf.
R Programming
result <- sprintf([1], 3.14159)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%d" truncates the number to integer.
Using "%.0f" rounds to zero decimals.
Using "%.3f" keeps three decimals, not two.
✗ Incorrect
The "%.2f" format specifier formats the number with two decimal places.
3fill in blank
hardFix the error in the code to format the string with a name and age using sprintf.
R Programming
result <- sprintf("Name: %s, Age: [1]", "Alice", 30)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%s" for age causes type mismatch.
Using "%f" formats as floating point.
Using "%c" expects a single character.
✗ Incorrect
The "%d" format specifier is used to format integers like age.
4fill in blank
hardFill both blanks to format a floating number with width 8 and 3 decimals using sprintf.
R Programming
result <- sprintf([1], [2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%6.2f" formats with wrong width and decimals.
Passing a string instead of a number as second argument.
Mixing up width and precision in format.
✗ Incorrect
The format "%8.3f" sets width 8 and 3 decimals; 3.14159 is the number to format.
5fill in blank
hardFill all three blanks to format a string with a name left-aligned in width 10 and an integer score padded with zeros to width 4.
R Programming
result <- sprintf([1], [2], [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%10s" right-aligns the string.
Using "%d" does not pad zeros.
Passing numbers as strings causes errors.
✗ Incorrect
Use "%-10s %04d" for left-aligned name width 10 and zero-padded score width 4, "Alice" as name, 42 as score.