Chapter 9 - Strings Flashcards

1
Q

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);

A

Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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");   } }
A

s1 and s2 reference to the same String object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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");   } }
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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");   } }
A

s1 and s2 have the same contents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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");
  }
}
A

s1 and s2 have different contents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the output of the following code?

String s = “University”;
s.replace(“i”, “ABC”);
System.out.println(s);

A
University.
Explanation: No method in the String class can change the content of the string. String is an immutable class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the return value of “SELECT”.substring(0, 5)?

A

“SELECT”.substring(0, 5)

== “SELEC”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Analyze the following code.

class Test {  
  public static void main(String[] args) {
    String s;
    System.out.println("s is " + s);
  }
}
A

The program has a compilation error because s is not initialized, but it is referenced in the println statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
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?

A

“Java”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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));
  }
}
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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”);
}
}

A

Java and HTML

How well did you know this?
1
Not at all
2
3
4
5
Perfectly