Java OCA OCP Practice Question 2422

Question

What is the output of the following code?

String m = "e Java Guru";
if(m.matches("u.u")){
    String[] tokens = m.split("\\Bu");
    for (String token : tokens) System.out.println(token);
}
else {
    System.out.println(m.replace(
                           m.subSequence(3, 4), m.substring(11)));
}
a  e Java G
   r
b  e Java Guru
c  e Jv Guru
d  e Juvu Guru
e  java.lang.StringIndexOutOfBoundsException:                 String     index     out   of
   range:  11
f  Code fails to compile.


c

Note

Method matches() doesn't look for a matching subsequence.

It matches the complete string value against the given regex pattern.

Because the regex pattern u.u matches a subsequence and not the entire string value e Java Guru, it returns false.

The length of string m is 11, so m.substring(11) doesn't throw StringIndexOutOfBoundsException. It returns an empty string, which replaces string "a in string value e Java Guru, resulting in e Jv Guru.




PreviousNext

Related