Assuming that the data, txt file exists and has the following content:
Text1
Text2
Text3
Given the code fragment:
What is the result?
A.
text1-
text2-
text3-
text3
B.
text1-text2-text3
text1
text2
text3
C.
text1-text2-text3
A java.lang.indexoutofBoundsException is thrown.
The answer is D because the code fragment reads the file ''data.txt'' and collects all the lines in the file into a single string, separated by hyphens. Then, it prints the resulting string. Next, it attempts to read the fourth line in the file (index 3) and print it. However, since the file only has three lines, an IndexOutOfBoundsException is thrown.Reference:
Oracle Certified Professional: Java SE 17 Developer
OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Read contents of a file using Files class in Java
Currently there are no comments in this discussion, be the first to comment!