Java OCA OCP Practice Question 2420

Question

Given that the content of the file data.txt is

Harry;8765,Per[fect

which options are correct for the following code?

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("data.txt"));
        String line;//from w  w  w .ja  v  a  2  s  .co m
        StringTokenizer st;
        while ((line = br.readLine()) != null) {
            st = new StringTokenizer(line, "[,;]");          //line1
            while (st.hasMoreElements())
            System.out.println(st.nextElement());
        }
        br.close();
    }
}

a  The code prints/*from   w ww  . j  av a  2s  . com*/

   Harry
    8765
   Per
   fect

b  The code prints

   Harry
    8765
   Per[fect

c  The code prints

   Harry;8765
   Per[fect

d  If the code on line  //line1 is changed to the following, it'll output the same
   results:

   st = new StringTokenizer(line, "[,;$]");          //line1

e  Code fails to compile.

f  Code throws a runtime exception.


a, d

Note

On the exam, you're likely to see questions based on multiple exam objectives, just as this question covers file handling and string processing.

Class StringTokenizer doesn't accept the delimiter as a regex pattern.

When [,;] is passed as a delimiter to class StringTokenizer, the occurrence of either of these characters acts as a delimiter.

So the line read from file data.txt is delimited using , ; and [.

Because the text in file data.txt doesn't include $, changing the delimiter text from [,;] to [,;$] won't affect the output.




PreviousNext

Related