1Z0-808 · Question #94
Given: ``java public class Test { public static void main(String[] args) { char[] arr = {97, 't', 'e', '\n', 'i', '\t', 'o'}; for (char var: arr) { System.out.print(var); } System.out.print("\nThe…
The correct answer is D. a e io The length is : 7. Option D is correct because the char array has 7 elements, making arr.length equal to 7 - immediately eliminating A (length 2), B, and C (both length 4). The integer literal 97 is a compile-time constant that fits within the valid char range (0–65535), so Java silently promotes…
Question
public class Test {
public static void main(String[] args) {
char[] arr = {97, 't', 'e', '\n', 'i', '\t', 'o'};
for (char var: arr) {
System.out.print(var);
}
System.out.print("\nThe length is :" + arr.length);
}
}
What is the result?Options
- Aa e The length is : 2
- Ba e io The length is : 4
- Caeio The length is : 4
- Da e io The length is : 7
- ECompilation fails.
How the community answered
(24 responses)- A4% (1)
- B13% (3)
- C4% (1)
- D71% (17)
- E8% (2)
Explanation
Option D is correct because the char array has 7 elements, making arr.length equal to 7 - immediately eliminating A (length 2), B, and C (both length 4). The integer literal 97 is a compile-time constant that fits within the valid char range (0–65535), so Java silently promotes it to the character 'a' (ASCII 97), which is why option E is wrong - the code compiles cleanly. The escape sequences '\n' and '\t' are treated as single characters (newline and tab respectively), causing the output to split across two lines with i followed by a tab and then o on the second line; the final System.out.print starts with "\n", which pushes the length message to a third line.
Memory tip: For char arrays, remember "NAIL the length" - Newline (\n), ASCII numerics, Integer-to-char promotion, and Length counts all elements including invisible ones like \n and \t. Every element in the array counts toward length, even whitespace characters.
Topics
Community Discussion
No community discussion yet for this question.