Challenge - 5 Problems
Printf and sprintf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using printf?
Consider the following PHP code snippet. What will it print?
PHP
<?php $number = 123.456; printf("%.2f\n", $number); ?>
Attempts:
2 left
💡 Hint
Look at the format specifier %.2f which controls decimal places.
✗ Incorrect
The format %.2f rounds the number to 2 decimal places, so 123.456 becomes 123.46.
❓ Predict Output
intermediate2:00remaining
What does this sprintf return?
What string does this PHP code return?
PHP
<?php $result = sprintf("%05d", 42); echo $result; ?>
Attempts:
2 left
💡 Hint
The format %05d pads the number with zeros to width 5.
✗ Incorrect
The %05d means print an integer padded with zeros to 5 characters, so 42 becomes '00042'.
❓ Predict Output
advanced2:00remaining
What is the output of this complex printf?
What will this PHP code print?
PHP
<?php $val = 255; printf("%#08x\n", $val); ?>
Attempts:
2 left
💡 Hint
The %#08x format prints hex with 0x prefix and pads to 8 characters.
✗ Incorrect
The %#08x prints the number in hex with 0x prefix and pads with zeros to 8 characters total, so 255 (0xff) becomes '0x000000ff'.
❓ Predict Output
advanced2:00remaining
What error or output does this code produce?
What happens when this PHP code runs?
PHP
<?php printf("%d %s", 10); ?>
Attempts:
2 left
💡 Hint
Check if the number of arguments matches the format specifiers.
✗ Incorrect
The format string expects two arguments (%d and %s), but only one (10) is given, so PHP raises a warning about too few arguments.
🧠 Conceptual
expert2:00remaining
How many characters does this sprintf string have?
What is the length of the string returned by this PHP code?
PHP
<?php $str = sprintf("%-10s%04d", "Hi", 7); echo strlen($str); ?>
Attempts:
2 left
💡 Hint
Consider the width specifiers: %-10s and %04d.
✗ Incorrect
The %-10s prints 'Hi' left-aligned in 10 spaces (adds 8 spaces), and %04d prints 7 padded with zeros to 4 digits ('0007'). Total length is 10 + 4 = 14 characters.