9.1 Suppose that s1, s2, s3, and s4 are four strings, given as follows:

String s1 = "Welcome to Java";

String s2 = s1;

String s3 = new String("Welcome to Java");

String s4 = "Welcome to Java";

What are the results of the following expressions?

(1) s1 == s2

(2) s2 == s3

(3) s1.equals(s2)

(4) s2.equals(s3)

(5) s1.compareTo(s2)

(6) s2.compareTo(s3)

(7) s1 == s4

(8) s1.charAt(0)

(9) s1.indexOf('j')

(10) s1.indexOf("to")

(11) s1.lastIndexOf('a')

(12) s1.lastIndexOf("o", 15)

(13) s1.length()

(14) s1.substring(5)

(15) s1.substring(5, 11)

(16) s1.startsWith("Wel")

(17) s1.endsWith("Java")

(18) s1.toLowerCase()

(19) s1.toUpperCase()

(20) " Welcome ".trim()

(21) s1.replace('o', 'T')

(22) s1.replaceAll("o", "T")

(23) s1.replaceFirst("o", "T")

(24) s1.toCharArray()

To create a string “Welcome to Java”, you may use a statement like this:

String s = "Welcome to Java";

or

String s = new String("Welcome to Java);

Which one is better? Why?

2.Suppose that s1 and s2 are two strings. Which of the following statements orexpressions are incorrect?

String s = new String("new string");

String s3 = s1 + s2;

String s3 = s1 - s2;

s1 == s2;

s1 >= s2;

s1.compareTo(s2);

inti = s1.length();

char c = s1(0);

char c = s1.charAt(s1.length());

3.What is the printout of the following code?

String s1 = "Welcome to Java";

String s2 = s1.replace("o", "abc");

System.out.println(s1);

System.out.println(s2);

9.4 Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following

statements:

■ Check whether s1 is equal to s2 and assign the result to a Boolean variableisEqual.

■ Check whether s1 is equal to s2, ignoring case, and assign the result to aBoolean variable isEqual.

■ Compare s1 with s2 and assign the result to an int variable x.

■ Compare s1 with s2, ignoring case, and assign the result to an int variable x.

■ Check whether s1 has prefix "AAA" and assign the result to a Boolean variable b.

■ Check whether s1 has suffix "AAA" and assign the result to a Boolean variable b.

■ Assign the length of s1 to an int variable x.

■ Assign the first character of s1 to a char variable x.

■ Create a new string s3 that combines s1 with s2.

■ Create a substring of s1 starting from index 1.

■ Create a substring of s1 from index 1 to index 4.

■ Create a new string s3 that converts s1 to lowercase.

■ Create a new string s3 that converts s1 to uppercase.

■ Create a new string s3 that trims blank spaces on both ends of s1.

■ Replace all occurrences of character e with E in s1 and assign the new string to s3.

■ Split "Welcome to Java and HTML" into an array tokens delimited by a space.

■ Assign the index of the first occurrence of character e in s1 to an int variable x.

■ Assign the index of the last occurrence of string abc in s1 to an int variable x.

5.Does any method in the String class change the contents of the string?

6.Suppose string s is created using new String(); what is s.length()?

7.How do you convert a char, an array of characters, or a number to a string?

8.Why does the following code cause a NullPointerException?

1 public class Test {

2 private String text;

3

4 public Test(String s) {

5

6 }

7

8 public static void main(String[] args) {

9 Test test = new Test("ABC");

10System.out.println(test.text.toLowerCase());

11 }

12 }

9.What is wrong in the following program?

1 public class Test {

2 String text;

3

4 public Test(String s) {

5 this.text = s;

6 }

7

8 public static void main(String[] args) {

9 Test test = new Test("ABC");

10 System.out.println(test);

11 }

12 }