Java OCA OCP Practice Question 2551

Question

Consider the following program and predict the output (ignore any empty lines in the output):

public class Main {
     public static void main(String[] s) {
             String quote = "aba*abaa**aabaa***";
             String [] words = quote.split("a\\**", 10);
             for (String word : words) {
                     System.out.println(word);
             }
     }
}
a)   ab//from   w  w  w.j a v  a  2 s .c o m
     aba
     *aaba
     **

b)   b
     b
     b

c)   aba*aba
     aaba

d)This program throws a runtime exception.


b)

Note

The specified regex (i.e. "a\\") will match to a string starting from an "a" followed by one or more "*" (since "\\" means zero or more occurrences of "*").

Hence, when the split is called on the input string, it results in three "b"s.

The second argument indicates a limit to split, which controls the number of times the pattern is applied.

Here the limit is 10, but the pattern is applied only three times, so it does not make any difference in this program.




PreviousNext

Related