%s inserts the string $name as is. %.1f formats $score as a float with 1 decimal place, rounding the value.
Step 2: Calculate rounded float
95.678 rounded to 1 decimal place is 95.7.
Final Answer:
Student: Alice, Score: 95.7 -> Option B
Quick Check:
%.1f rounds 95.678 to 95.7 [OK]
Hint: %.1f rounds float to 1 decimal place [OK]
Common Mistakes:
Not rounding float correctly
Confusing %s and %f usage
Expecting full float without formatting
4. Identify the error in this PHP code snippet:
$value = 123; printf("Value is %d.2", $value);
medium
A. The variable $value is not defined.
B. printf cannot print integers.
C. The format specifier %d.2 is invalid syntax.
D. The code is correct and prints 'Value is 123.2'.
Solution
Step 1: Check format specifier syntax
%d is for integers and does not support precision like .2. The correct usage is just %d.
Step 2: Understand error cause
Adding .2 after %d causes invalid format syntax, leading to an error or unexpected output.
Final Answer:
The format specifier %d.2 is invalid syntax. -> Option C
Quick Check:
%d does not take precision like %.2f [OK]
Hint: Use %d alone for integers; no decimal precision [OK]
Common Mistakes:
Trying to add decimal precision to %d
Confusing %d and %f format specifiers
Assuming printf auto-corrects invalid formats
5. You want to create a formatted string showing a product's name left-aligned in 10 spaces and its price right-aligned in 8 spaces with 2 decimals. Which sprintf format string is correct?