Exam 1z0-829 All QuestionsBrowse all questions from this exam
Question 31

Assuming that the data.txt file exists and has the following content: text1 text2 text3

Given the code fragment:

What is the result?

    Correct Answer: B

    The code first reads all lines in the file and joins them with a hyphen to produce the output 'text1-text2-text3'. However, when attempting to access the fourth element (index 3) in the list of lines using 'Files.readAllLines(p).get(3)', a java.lang.IndexOutOfBoundsException is thrown because the file only contains three lines, which correspond to indices 0, 1, and 2.

Discussion
xplorerpjOption: B

Answer 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.

supersquaxOption: B

B correct. Java uses 0 indexing so we only have items 0 (text1), 1 (text2), and 2 (text3). Index 3 outa bounds.