Which of the following methods are methods of the String class a delete b append c reverse d replace

Due to the print book page limit, we cannot inlcude all good CheckPoint questions in the physical book. The CheckPoint on this Website may contain extra questions not printed in the book. The questions in some sections may have been reordered as a result. Nevertheless, it is easy to find the CheckPoint questions in the book on this Website. Please send suggestions and errata to Dr. Liang at . Indicate the book, edition, and question number in your email. Thanks!

Chapter 10 Check Point Questions

Section 10.2

▼10.2.1

If you redefine the Loan class in Listing 10.2 without setter methods, is the class immutable?

Section 10.3

▼10.3.1

Is the BMI class defined in Listing 10.4 immutable?

Section 10.4

▼10.4.1

What are common relationships among classes?

▼10.4.2

What is association? What is aggregation? What is composition?

▼10.4.3

What is UML notation of aggregation and composition?

▼10.4.4

Why both aggregation and composition are together referred to as composition?

Section 10.5

▼10.5.1

Replace the statement in line 17 in Listing 10.5 TestCourse.java so that the loop displays each student name followed by a comma except the last student name.

Section 10.6

▼10.6.1

What happens when invoking the pop() method on a stack while size is 0?

Section 10.7

▼10.7.1

Describe primitive-type wrapper classes.

▼10.7.2

Can each of the following statements be compiled?

a. Integer i = new Integer("23"); b. Integer i = new Integer(23); c. Integer i = Integer.valueOf("23"); d. Integer i = Integer.parseInt("23", 8); e. Double d = new Double(); f. Double d = Double.valueOf("23.45"); g. int i = (Integer.valueOf("23")).intValue(); h. double d = (Double.valueOf("23.4")).doubleValue(); i. int i = (Double.valueOf("23.4")).intValue(); j. String s = (Double.valueOf("23.4")).toString();

▼10.7.3

How do you convert an integer into a string? How do you convert a numeric string into an integer? How do you convert a double number into a string? How do you convert a numeric string into a double value?

▼10.7.4

Show the output of the following code.

public class Test { public static void main(String[] args) { Integer x = new Integer(3); System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } }

▼10.7.5

What is the output of the following code?

public class Test { public static void main(String[] args) { System.out.println(Integer.parseInt("10")); System.out.println(Integer.parseInt("10", 10)); System.out.println(Integer.parseInt("10", 16)); System.out.println(Integer.parseInt("11")); System.out.println(Integer.parseInt("11", 10)); System.out.println(Integer.parseInt("11", 16)); } }

Section 10.8

▼10.8.1

What are autoboxing and autounboxing? Are the following statements correct?

a. Integer x = 3 + new Integer(5); b. Integer x = 3; c. Double x = 3; d. Double x = 3.0; e. int x = new Integer(3); f. int x = new Integer(3) + new Integer(4);

▼10.8.2

Show the output of the following code?

public class Test { public static void main(String[] args) { Double x = 3.5; System.out.println(x.intValue()); System.out.println(x.compareTo(4.5)); } }

Section 10.9

▼10.9.1

What is the output of the following code?

public class Test { public static void main(String[] args) { java.math.BigInteger x = new java.math.BigInteger("3"); java.math.BigInteger y = new java.math.BigInteger("7"); java.math.BigInteger z = x.add(y); System.out.println("x is " + x); System.out.println("y is " + y); System.out.println("z is " + z); } }

Section 10.10

▼10.10.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?

a. s1 == s2 b. s1 == s3 c. s1 == s4 d. s1.equals(s3) e. s1.equals(s4) f. "Welcome to Java".replace("Java", "HTML") g. s1.replace('o', 'T') h. s1.replaceAll("o", "T") i. s1.replaceFirst("o", "T") j. s1.toCharArray()

▼10.10.2

To create the 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?

▼10.10.3

What is the output of the following code?

String s1 = "Welcome to Java"; String s2 = s1.replace("o", "abc"); System.out.println(s1); System.out.println(s2);

▼10.10.4

Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements:
a. Replace all occurrences of the character e with E in s1 and assign the new string to s3.
b. Split Welcome to Java and HTML into an array tokens delimited by a space and assign the first two tokens into s1 and s2.

▼10.10.5

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

▼10.10.6

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

▼10.10.7

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

▼10.10.8

Why does the following code cause a NullPointerException?

1 public class Test { 2 private String text; 3 4 public Test(String s) { 5 String text = s; 6 } 7 8 public static void main(String[] args) { 9 Test test = new Test("ABC"); 10 System.out.println(test.text.toLowerCase()); 11 } 12 }

▼10.10.9

What is wrong in the following program?

1 public class Test { 2 String text; 3 4 public void Test(String s) { 5 text = s; 6 } 7 8 public static void main(String[] args) { 9 Test test = new Test("ABC"); 10 System.out.println(test); 11 } 12 }

▼10.10.10

Show the output of the following code.

public class Test { public static void main(String[] args) { System.out.println("Hi, ABC, good".matches("ABC ")); System.out.println("Hi, ABC, good".matches(".*ABC.*")); System.out.println("A,B;C".replaceAll(",;", "#")); System.out.println("A,B;C".replaceAll("[,;]", "#")); String[] tokens = "A,B;C".split("[,;]"); for (int i = 0; i < tokens.length; i++) System.out.print(tokens[i] + " "); } }

▼10.10.11

Show the output of the following code.

public class Test { public static void main(String[] args) { String s = "Hi, Good Morning"; System.out.println(m(s)); } public static int m(String s) { int count = 0; for (int i = 0; i < s.length(); i++) if (Character.isUpperCase(s.charAt(i))) count++; return count; } }

Section 10.11

▼10.11.1

What is the difference between StringBuilder and StringBuffer?

▼10.11.2

How do you create a string builder from a string? How do you return a string from a string builder?

▼10.11.3

Write three statements to reverse a string s using the reverse method in the StringBuilder class.

▼10.11.4

Write three statements to delete a substring from a string s of 20 characters, starting at index 4 and ending with index 10. Use the delete method in the StringBuilder class.

▼10.11.5

What is the internal storage for characters in a string and a string builder?

▼10.11.6

Suppose that s1 and s2 are given as follows:

StringBuilder s1 = new StringBuilder("Java"); StringBuilder s2 = new StringBuilder("HTML");

Show the value of s1 after each of the following statements. Assume that the statements are independent.

a. s1.append(" is fun"); b. s1.append(s2); c. s1.insert(2, "is fun"); d. s1.insert(1, s2); e. s1.charAt(2); f. s1.length(); g. s1.deleteCharAt(3); h. s1.delete(1, 3); i. s1.reverse(); j. s1.replace(1, 3, "Computer"); k. s1.substring(1, 3); l. s1.substring(2);

▼10.11.7

Show the output of the following program:

public class Test { public static void main(String[] args) { String s = "Java"; StringBuilder builder = new StringBuilder(s); change(s, builder); System.out.println(s); System.out.println(builder); } private static void change(String s, StringBuilder builder) { s = s + " and HTML"; builder.append(" and HTML"); } }