1Z0-803 · Question #111
Given: 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 i
The correct answer is D. a e. This program iterates through a character array, printing each element including special characters like tab and newline, then displays the array's total length.
Question
Given:
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
- Ba e
- Caeio
- Da e
- ECompilation fails.
How the community answered
(24 responses)- A4% (1)
- C4% (1)
- D92% (22)
Why each option
This program iterates through a character array, printing each element including special characters like tab and newline, then displays the array's total length.
This choice is incorrect because it omits several characters, the tab and newline formatting, and the final output about the array's length.
This choice is identical to A, failing to represent the complete and formatted output of the program.
This choice incorrectly concatenates characters, ignores the tab and newline characters, and omits the array's length information.
The `for-each` loop prints each character from the array: 'a' (ASCII 97), a tab, 'e', a newline, 'i', a tab, and 'o'. Following this, a new line is printed, and then the string 'The length is :' concatenated with the array's length, which is 7.
The provided Java code is syntactically valid and will compile and execute without any compilation failures.
Concept tested: Character array iteration and printing
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Topics
Community Discussion
No community discussion yet for this question.