1Z0-811 · Question #52
Given the code fragment: public static void main (String[] args) { String name = "Rita"; int age = 14; / line n1 / } Which code fragment, when inserted at line n1, enables it to print Rita is 14…
The correct answer is C. System.out.println(" %s is %d years old", name, age). Option C uses the correct format specifiers - %s for the String variable name and %d for the integer variable age - and passes all arguments with proper comma separation, which is the syntax required for formatted output methods in Java (note: in practice this syntax belongs to…
Question
Options
- ASystem.out.println("%s is %d years old" +name+age);
- BSystem.out.println("%s is %n years old" name, age);
- CSystem.out.println(" %s is %d years old", name, age);
- DSystem.out.println("%s is %n years old", name, age);
How the community answered
(44 responses)- A7% (3)
- B5% (2)
- C86% (38)
- D2% (1)
Explanation
Option C uses the correct format specifiers - %s for the String variable name and %d for the integer variable age - and passes all arguments with proper comma separation, which is the syntax required for formatted output methods in Java (note: in practice this syntax belongs to System.out.printf, not println, but the exam is testing your knowledge of format specifiers and argument syntax).
Why the distractors fail:
- A uses string concatenation (
+) instead of format specifiers, so the literal text%s is %d years oldwould print verbatim, followed byRita14. - B has a syntax error - the comma separating the format string from the first argument is missing.
- D uses
%n, which is a newline escape, not a number formatter; the age would not print correctly.
Memory tip: Think of format letters by their first letter - %s = String, %d = Digit (integer), %f = Floating-point. And %n = Newline, never a number - if you see it used as a numeric placeholder on an exam, it's a trap.
Topics
Community Discussion
No community discussion yet for this question.