Strings Flashcards
Suppose s is a string with the value “java”. What will be assigned to x if you execute the following code?
char x = s.charAt(4);
Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException
What is the output of the following code?
public class Test { public static void main(String[] args) { String s1 = "Welcome to Java!"; String s2 = s1;
if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } }
s1 and s2 reference to the same String object
What is the output of the following code?
public class Test { public static void main(String[] args) { String s1 = "Welcome to Java!"; String s2 = "Welcome to Java!";
if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } }
s1 and s2 reference to the same String object.
Explanation: Since strings are immutable and are ubiquitous in programming, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned
What is the output of the following code?
public class Test { public static void main(String[] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!");
if (s1.equals(s2)) System.out.println("s1 and s2 have the same contents"); else System.out.println("s1 and s2 have different contents"); } }
s1 and s2 have the same contents
What is the output of the following code?
public class Test { public static void main(String[] args) { String s1 = new String("Welcome to Java!"); String s2 = s1.toUpperCase();
if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else if (s1.equals(s2)) System.out.println("s1 and s2 have the same contents"); else System.out.println("s1 and s2 have different contents"); } }
s1 and s2 have different contents
What is the output of the following code?
String s = “University”;
s.replace(“i”, “ABC”);
System.out.println(s);
University. Explanation: No method in the String class can change the content of the string. String is an immutable class.
What is the return value of “SELECT”.substring(0, 5)?
“SELECT”.substring(0, 5)
== “SELEC”
Analyze the following code.
class Test { public static void main(String[] args) { String s; System.out.println("s is " + s); } }
The program has a compilation error because s is not initialized, but it is referenced in the println statement
public class Test { public static void main(String[] args) { String s = "Java"; StringBuilder buffer = new StringBuilder(s); change(s); System.out.println(s); }
private static void change(String s) {
s = s + “ and HTML”;
}
}
What is printed?
“Java”
Analyze the following code.
class Test { public static void main(String[] args) { StringBuilder strBuf = new StringBuilder(4); strBuf.append("ABCDE"); System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(5)); } }
The program has a runtime error because the length of the string in the buffer is 5 after “ABCDE” is appended into the buffer. Therefore, strBuf.charAt(5) is out of range
The following program displays ___.
public class Test { public static void main(String[] args) { String s = "Java"; StringBuilder buffer = new StringBuilder(s); change(buffer); System.out.println(buffer); }
private static void change(StringBuilder buffer) {
buffer.append(“ and HTML”);
}
}
Java and HTML