Java Files.readAllLines Indexing Behavior
Short answer
B The code correctly accesses indices 0, 1, and 2 of the list returned by Files.readAllLines, printing text1-text2-text3.
Question

Key Takeaways
The core concept tested is zero-based list indexing in Java; the common trap is assuming 1-based indexing or miscounting the number of lines returned by readAllLines.
This question tests Java I/O API usage with Files.readAllLines, establishing that the method returns a List with zero-based indexing where valid indices are 0 to size-1.
Common mistake: Learners often select options implying out-of-bounds access (like index 3) because they mistakenly assume 1-based indexing or fail to realize that a file with 3 lines only has indices 0, 1, and 2.
Expert Analysis
Why the Answer Is Correct
The code uses Files.readAllLines which returns a List<String>. Lists in Java use zero-based indexing. If the file contains three distinct lines ('text1', 'text2', 'text3'), the list size is 3. Valid indices are 0, 1, and 2. Accessing lines.get(0), lines.get(1), and lines.get(2) retrieves all three texts. The code likely concatenates them with hyphens, resulting in 'text1-text2-text3'. Option B represents this correct output.
Why the Other Options Are Wrong
Options suggesting 'text1-' imply missing elements or incorrect joining logic. Options suggesting an exception or different text usually stem from accessing index 3, which would throw an IndexOutOfBoundsException since index 3 is out of bounds for a list of size 3. Any option not matching the exact concatenated string of the three lines is incorrect based on standard stream/list processing shown in typical exam fragments.
Community Comment Notes
Community members consistently confirm that Java lists are zero-indexed. As user supersquax noted, "Java uses 0 indexing so we only have items 0... and 2." Another user SrinivasJasti clarified that "the first line... has an index of 0." These comments reinforce that understanding the 0-to-N-1 range is critical to avoiding runtime errors in this context.
Exam Strategy
Always verify the size of collections returned by I/O methods like readAllLines. Remember that get(size()) will always throw an exception; the last valid index is size() - 1.
Frequently Asked Questions
Why does accessing index 3 cause an exception?
Does readAllLines return 1-based indexing?
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 3 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 3
-
supersquax 👍 2B correct. Java uses 0 indexing so we only have items 0 (text1), 1 (text2), and 2 (text3). Index 3 outa bounds.
-
SrinivasJasti 👍 1 Answered Bthe indices used when accessing lines from a file using Files.readAllLines() are zero-based. This means: The first line of the file has an index of 0. The second line has an index of 1. The third line has an index of 2, and so on.
-
xplorerpj 👍 1Answer is B (text1-text2-text3), when input file has text in 3 different lines, in input file. text1 text2 text3 And Index out of bounds occur since there are only 2 items.
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.