Saturday, May 9, 2009

SCJP Mock Exam Working with Strings and Split

 

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain, has gone.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments.length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option e) is correct.

The matching String “n” occurs three times in the String named prompt, and the split command will always return n+1 string fragments, where n is the number of times the argument passed into the split method is found in the String object being split.

*******************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain, has gone.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments[1]);
}

a) d the rai
b) nd the rai
c) d the rain
d) nd the rain
e) A checked exception will be thrown at runtime
f)  An unchecked exception will be thrown at runtime
g) The code will not compile

 

Option b) is correct.

“nd the rai” is the String that will be contained in the second fragment of the String array after the prompt has been parsed.

A common mistake is to think the sequence of characters, which in this case is the letter “n”, is included in the fragment returned from the split method. The character String passed into the split method will not be contained, either at the beginning or the end, of the various String fragments contained in the returned array.

The four String fragments contained in the array returned after the prompt is split on the letter “n” are:

1: A
2: d the rai
3: , has go
4: e.

*********************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "nnd the rain.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments.length());
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

 

Option i) is correct.

There variable fragments represents an array, and to find the size of an array, we use the length property, not the length() method.

If the round brackets, (), following ‘length’ were removed, the code would compile and run, spitting out the number 4 to indicate that splitting the String named prompt on the String sequence “n” would result in four String fragments. Since the String named prompt begins with two consecutive “n” matches, the first two elements in the fragments array would be empty String objects:

1:
2:
3: d the rai
4: .

*****************************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "Nnd the rain.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments.length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

 

Option d) is correct.

We will always see n+1 array elements after splitting a given String, where n is the number of times the String sequence passed into the split method is matched on the String in question. There are two matches to the String “n” in the prompt String, so, two plus one, which is three, elements will will be contained in the fragments array.

The split method of the java.lang.String class is case sensitive, so an upper case letter will not strike a match with a lower case letter.

**************************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain,\n has gone.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments.length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option e) is correct.

Although from a spelling point of view, the letter “n” appears four times in the String named prompt, the Java Virtual Machine does not consider the “\n” String sequence when performing a split match on the letter “n". This is because “\n” is a special construct in Java known as the ‘escape sequence’, with this particular escape sequence representing the carriage return, or enter key.

Removing the ‘\n’ escape sequence from the prompt String, we are left with three occurrences of the letter “n”, and as a result, n+1, or four, elements are placed in the fragments array.

************************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain,/n has gone.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments.length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option f) is correct.

The trick in this question is to see if you can differentiate between correct and incorrect escape sequence syntax. “/n” is not correct escape sequence syntax for a carriage return, so the Java Virtual Machine treats the ‘n’ in this String as any other match on the letter ‘n’, thus splitting thing String on this element. Along with the three other matches on the letter “n” in the String named prompt, we end up with five elements in the fragments array.

*******************************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain,\n has gone.";
    String[] fragments = prompt.split("\n");
    System.out.println(fragments.length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option c) is correct.

This question looks for a match in the String named prompt on the special character sequence representing the return key, “\n”. A single match is found, and two String are created and placed as separate elements in the array named fragments.

************************************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain,\* has gone.";
    String[] fragments = prompt.split("\*");
    System.out.println(fragments.length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option i) is correct.

This code will not compile, due to the fact that \* is not a valid escape sequence. Only the following eight escape sequences are valid:

\b
\t
\n
\f
\r
\”
\’
\\

*******************************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain,\n has gone.";
    String[] fragments = prompt.split("\n");
    System.out.println(fragments[2]);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option h) is correct.

The split in this case only generates two String fragments, which will go in elements [0] and [1] of the fragments array. Trying to access element [2] in the fragments array will trigger an unchecked exception to be thrown.

Here is the message you encounter when you attempt to run this particular snippet of code:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2

***************************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain, has gone.";
    String[] fragments = prompt.split("x");
    System.out.println(fragments.length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option b) is correct.

When the split method of a String is invoked using an argument that doesn’t have any corresponding matches on the String in question, the entire String, which in this case is “And the rain, has gone.”, is returned as the first and only element in the array. So, at the end of this method, the fragments array will contain one, and only one, element.

**********************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "And the rain,\u has gone.";
    String[] fragments = prompt.split("\u");
    System.out.println(fragments[5]);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 5
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

Option i) is the correct answer.

It is tempting to think that the answer to this question is h), that an exception will be thrown at runtime, but in fact, this code will never make it to runtime because the darned thing does not even compile.

“\u” is not a valid escape sequence in Java, so it is not a valid entry when used as a character sequence in a String.

Only the following eight escape sequences are valid:

\b
\t
\n
\f
\r
\”
\’
\\

*************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "nnnd the rain.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments[2].length());
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 9
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

 

Option a) is the correct answer.

Even though there are consecutive matches in the prompt String, each individual match triggers a new String fragment to be added to the array. The actual matching String sequence, in this case a single “n”, does not get included in the generated fragment. Since the letter “n” is matched consecutively, there are only empty String elements placed in the array until the fourth “n” is encountered.

The five String elements in the fragment array would be (nothing after the number indicates a blank String element with a length of zero):

1:
2:
3:
4: d the rai
5: .

*************************************************

What would be the result of attempting to compile and run the following code snippet?

public static void main(String[] args) {
    String prompt = "nnnd the rain.";
    String[] fragments = prompt.split("n");
    System.out.println(fragments[3].length);
}

a) An output of 0
b) An output of 1
c) An output of 2
d) An output of 3
e) An output of 4
f)  An output of 9
g) A checked exception will be thrown at runtime
h) An unchecked exception will be thrown at runtime
i) The code will not compile

 

Option i) is the correct answer.

The object named fragments is an array of String objects. To find the number of characters in a String you invoke the length() method, not the length property. The lack of round brackets, (), after call to fragments[3].length, triggers a compile time error.

If the compile error was removed, and the problematic line of code was replaced with: System.out.println(fragments[3].length());, then the output would be the number 9.

No comments:

Post a Comment

Followers