Strings Questions
Section 4.8-4.9
Questions: Q4-7 - Q4-11, pages 234 - 235
Answers: A4-7 - A4-11, pages 240-242
Q 4.7
What is the output of the following code?
class EJavaGuruString {
public static void main(String args[]) {
String ejg1 = new String("E Java");
String ejg2 = new String("E Java");
String ejg3 = "E Java";
String ejg4 = "E Java";
do {
System.out.println(ejg1.equals(ejg2));
} while (ejg3 == ejg4);
}
}- a. true printed once
- b. false printed once
- c. true printed in an infinite loop
- d. false printed in an infinite loop
A 4.7
Answer: c
Explanation:
String objects that are created without using the new operator are placed in a pool of String s. Hence, the String object referred to by the variable ejg3 is placed in a pool of String s. The variable ejg4 is also defined without using the new operator. Before Java creates another String object in the String pool for the variable ejg4, it looks for a String object with the same value in the pool. Because this value already exists in the pool, it makes the variable ejg4 refer to the same String object. This, in turn, makes the variables ejg3 and ejg4 refer to the same String objects. Hence, both of the following comparisons will return true:
- ejg3 == ejg4 (compare the object references)
- ejg3.equals(ejg4) (compare the object values)
Even though the variables ejg1 and ejg2 refer to different String objects, they define the same values. So ejg1.equals(ejg2) also returns true. Because the loop condition (ejg3==ejg4) always returns true, the code prints true in an infinite loop.
Q 4.8
What is the output of the following code?
class EJavaGuruString2 {
public static void main(String args[]) {
String ejg = "game".replace('a', 'Z').trim().concat("Aa");
ejg.substring(0, 2);
System.out.println(ejg);
}
}
- a. gZmeAZ
- b. gZmeAa
- c. gZm
- d. gZ
- e. game
A 4.8
Answer: b
Explanation:
When chained, methods are evaluated from left to right. The first method to execute is replace, not concat.
Strings are immutable. Calling the method substring on the reference variable ejg doesn’t change the contents of the variable ejg. It returns a String object that isn’t referred to by any other variable in the code. In fact, none of the methods defined in the String class modifies the object’s own value. They all create and return new String objects.
Q 4.9
What is the output of the following code?
class EJavaGuruString2 {
public static void main(String args[]) {
String ejg = "game";
ejg.replace('a', 'Z').trim().concat("Aa");
ejg.substring(0, 2);
System.out.println(ejg);
}
}
- a. gZmeAZ
- b. gZmeAa
- c. gZm
- d. gZ
- e. game
A 4.9
Answer: e
Explanation:
String objects are immutable - it doesn't matter how many methods you execute on a String object; its value won’t change.
Variable ejg is initialized with the String value "game" . This value won’t change, and the code prints game.
Q 4.10
What is the output of the following code?
class EJavaGuruStringBuilder {
public static void main(String args[]) {
StringBuilder ejg = new StringBuilder(10 + 2 + "SUN" + 4 + 5);
ejg.append(ejg.delete(3, 6));
System.out.println(ejg);
}
}
- a. 12S512S5
- b. 12S12S
- c. 1025102S
- d. Runtime exception
A 4.10
Answer: a
Explanation: This question tests you on your understanding of operators, String, and StringBuilder. The following line of code returns 12SUN45:
10 + 2 + "SUN" + 4 + 5
The + operator adds two numbers but concatenates the last two numbers. When the + operator encounters a String object, it treats all the remaining operands as String objects. Unlike the String objects, StringBuilder objects are mutable. The append and delete methods defined in this class change its value. ejg.delete(3, 6) modifies the existing value of the StringBuilder to 12S5, and returns the StringBuilder instance. It then appends the same value to itself when calling ejg.append(), resulting in the value 12S512S5.
Q 4.11
What is the output of the following code?
class EJavaGuruStringBuilder2 {
public static void main(String args[]) {
StringBuilder sb1 = new StringBuilder("123456");
sb1.subSequence(2, 4);
sb1.deleteCharAt(3);
sb1.reverse();
System.out.println(sb1);
}
}
- a. 521
- b. Runtime exception
- c. 65321
- d. 65431
A 4.11
Answer: c
Explanation: Like the method substring, the method subSequence doesn’t modify the contents of a StringBuilder. Hence, the value of the variable sb1 remains 123456, even after the execution of the following line of code:
sb1.subSequence(2, 4);The method deleteCharAt deletes a char value at position 3. Because the positions are zero-based, the digit 4 is deleted from the value 123456, resulting in 12356. The method reverse modifies the value of a StringBuilder by assigning to it the reverse representation of its value. The reverse of 12356 is 65321.
Strings Questions
By mitchellmebane
Strings Questions
- 348