Ch9 Text Processing and More about Wrapper Full Test Bank - Java Control Structures 7e Test Bank by Tony Gaddis. DOCX document preview.
Starting Out with Java: From Control Structures through Objects 7e (Gaddis)
Chapter 9 Text Processing and More about Wrapper Classes
TRUE/FALSE
1. You cannot assign a value to a wrapper class object.
2. StringBuilder objects are not immutable.
3. If a string has more than one character used as a delimiter, you must write a loop to determine the tokens, one for each delimiter character.
4. The String class's valueOf method accepts a string representation as an argument and returns its equivalent integer value.
5. When working with the String and StringBuilder classes' getChars method, the character at the start position is included in the substring but the character at the end position is not included.
6. The String class's regionMatches method performs a case-insensitive comparison.
7. Most of the String comparison methods are case sensitive.
8. Trying to extract more tokens than exist from a StringTokenizer object will cause an error.
9. A wrapper class is a class that is "wrapped around" a primitive data type and allows you to create objects instead of variables.
10. The wrapper classes in Java are immutable, which means that once you create an object, you cannot change the object's value.
11. If a non-letter is passed to the toLowerCase or toUpperCase method, it is returned unchanged.
12. You can change the contents of a StringBuilder object, but you cannot change the contents of a String object.
13. If you are using characters other than whitespaces as delimiters, you will probably want to trim the string before tokenizing; otherwise, the leading and/or following whitespaces will become part of the first and/or last token.
14. If a non-letter argument is passed to the toLowerCase or toUpperCase method, the boolean value false is returned.
15. The following statement correctly creates a StringBuilder object.
StringBuilder str = "Tuna sandwich";
MULTIPLE CHOICE
1. A series of words or other items of data, separated by spaces or other characters, is known as a __________.
a. | string | b. | token | c. | delimiter | d. | caption |
2. The term __________ is commonly used to refer to a string that is part of another string.
a. | nested string | b. | literal | c. | substring | d. | delimiter |
3. What is the term used for a class that is "wrapped around" a primitive data type and allows you to create objects instead of variables?
a. | intrinsic class | c. | wrapper class |
b. | enclosed object | d. | transitional object |
4. The term ________ commonly is used to refer to a string that is part of another string.
a. | strand | c. | character fragment |
b. | substring | d. | nested string |
5. The ________ character appears at the end (the right side) of a string, after the non-space characters.
a. | leading whitespace | c. | trailing whitespace |
b. | character return | d. | line feed |
6. Autoboxing is __________.
a. | Java's process of automatically "boxing up" a value inside an object |
b. | the automatic allocation of array elements |
c. | the process of assigning a default value to primitive data types |
d. | the process of identifying tokens in a string |
7. The term used for the character that separates tokens is __________.
a. | tokenizer | b. | delimiter | c. | whitespace | d. | separator |
8. The process of breaking a string down into tokens is known as __________.
a. | tokenizing | b. | buffering | c. | simplifying | d. | parsing |
9. The process of converting a wrapper class object to a primitive type is known as __________.
a. | simplifying | b. | unboxing | c. | parsing | d. | devaluating |
10. When using the String class's trim method, a __________ cannot be trimmed.
a. | space | b. | tab | c. | semicolon | d. | newline |
11. The no-arg constructor for a StringBuilder object gives the object enough storage space to hold __________ characters.
a. | 0 | b. | 8 | c. | 16 | d. | 32 |
12. The __________ method of the String class can be used to tokenize a string.
a. | split | b. | tokenize | c. | trim | d. | length |
13. Which of the following statements converts a String object variable named str to an int and stores the value in the variable x?
a. | int x = Integer.integer(str); |
b. | int x - str; |
c. | int x = Integer.parseInteger(str); |
d. | int x = Integer.parseInt(str); |
14. Which of the following statements converts an int variable named number to a string and stores the value in the String object variable named str?
a. | String str = Integer.toString(number); |
b. | String str = number.Integer.toString(str); |
c. | String str = integer(number); |
d. | String str = integer.toString(number); |
15. Which of the following import statements is required to use the Character wrapper class?
a. | import java.String; |
b. | import java.lang.Char; |
c. | import java.Char; |
d. | No import statement is required |
16. Which of the following statements will display the maximum value that a double can hold?
a. | System.out.println(Double.MAX_VALUE); |
b. | System.out.println(Double.MAXIMUM_VALUE); |
c. | System.out.println(Double.MAX_VAL); |
d. | System.out.println(<double>(MAX_VALUE)); |
17. The __________ method returns a copy of the calling String object with all leading and trailing whitespace characters deleted.
a. | remove | b. | trim | c. | compress | d. | concat |
18. The String class's __________ method accepts a value of any primitive data type as its argument and returns a string representation of the value.
a. | trim | b. | getChar | c. | toString | d. | valueOf |
19. The __________ class is the wrapper class for the char data type.
a. | StringBuilder | c. | Character |
b. | Integer | d. | String |
20. The StringBuilder class's insert method allows you to insert a(n) __________ into the calling object's string.
a. | char array | c. | String object |
b. | primitive type | d. | All of these |
21. The Character wrapper class provides numerous methods for __________.
a. | converting objects to primitive data types |
b. | testing and converting character data |
c. | testing and converting numeric literals |
d. | performing operations with named constants |
22. You can concatenate String objects by using the __________.
a. | concat or trim methods |
b. | concat method or the + operator |
c. | concatenate or join methods |
d. | concatenate method or the + operator |
23. Any ___________ argument passed to the Character class's toLowerCase method or toUpperCase method is returned as it is.
a. | nonletter | b. | char | c. | string | d. | static |
24. Each of the numeric wrapper classes has a static ________ method that converts a number to a string.
a. | GetString | b. | Parse | c. | ToString | d. | Convert |
25. What will be printed after the following code is executed?
String str = "abc456";
int m = 0;
while ( m < 6 )
{
if (Character.isLetter(str.charAt(m)))
System.out.print(
Character.toUpperCase(str.charAt(m)));
m++;
}
a. | abc456 | b. | ABC456 | c. | ABC | d. | 456 |
26. What will be the value of position after the following code is executed?
int position;
String str = "The cow jumped over the moon.";
position = str.lastIndexOf("ov", 14);
a. | 14 | b. | 1 | c. | 0 | d. | -1 |
27. What would be the results of executing the following code?
StringBuilder str = new StringBuilder("Little Jack
Horner ");
str.append("sat on the ");
str.append("corner");
a. | The program would crash. |
b. | str would reference "Little Jack Horner ". |
c. | str would reference "Little Jac Horner sat on the ". |
d. | str would reference "Little Jack Horner sat on the corner". |
28. Given the following, which of the statements below is not true?
str.insert(8, 32);
a. | str is a StringBuilder type object. |
b. | The insert will start at position 32. |
c. | The starting position for the insert is 8. |
d. | The literal number 32 will be inserted. |
29. What will be the value of position after the following code is executed?
int position;
String str = "The cow jumped over the moon.";
position = str.indexOf("ov");
a. | 14 | b. | 15 | c. | 18 | d. | 17 |
30. In the following statement, what data type must recField be?
str.getChars(5, 10, recField, 0);
a. | int | b. | char[] | c. | char | d. | String |
31. What will be displayed after the following code is executed?
String str = "RSTUVWXYZ";
System.out.println(str.charAt(5));
a. | W | b. | X | c. | V | d. | U |
32. What will be displayed after the following code is executed?
String str = "abc456";
for (int i = 0; i < str.length(); i++)
{
char chr = str.CharAt(i);
if (!Character.isLetter(chr))
System.out.print(Character.toUpperCase(chr));
}
a. | ABC | b. | ABC456 | c. | 456 | d. | abc456 |
33. In the following code, how many times will the for loop execute?
String str = "1,2,3,4,5,6,7,8,9");
String[] tokens = str.split(",");
for (String s : tokens)
System.out.println(s);
a. | 1 | b. | 5 | c. | 7 | d. | 9 |
34. In the following code, how many times will the for loop execute?
String str = ("Ben and Jerry's ice cream is great.");
String[] tokens = str.split(" ");
for (String s : tokens)
System.out.println(s);
a. | 1 | b. | 3 | c. | 5 | d. | 7 |
35. What is the value of str after the following code has been executed?
String str;
String sourceStr = "Hey diddle, diddle, the cat
and the fiddle";
str = sourceStr.substring(12,17);
a. | diddle | b. | diddl | c. | didd | d. | iddle |
36. If str is declared as:
String str = "ABCDEFGHI";
What will be returned from the following statement?
Character.toLowerCase(str.charAt(5))
a. | e | b. | E | c. | f | d. | F |
37. Which of the following statements will convert the string, str = "285.74" to a double?
a. | double x = str; |
b. | double x = Double.parseDouble(str); |
c. | double x = Double.Double(str); |
d. | double x = str.Double.parseDouble; |
38. Which of the following statements will convert the double variable, d = 543.98 to a string?
a. | String str = Double.toString(d); |
b. | String str = double.toString(d); |
c. | String str = double(d); |
d. | String str = d.Double.toString(str); |
39. Which of the following statements will convert the string, str = "285" to an int?
a. | int x = str; |
b. | int x = Integer.parseInteger(str); |
c. | int x = Integer.integer(str); |
d. | int x = Integer.parseInt(str); |
40. Which of the following statements will print the maximum value an int variable may have?
a. | System.out.println(MAX_VALUE); |
b. | System.out.println(integer.MAX_VALUE); |
c. | System.out.println(Integer.MAX_VALUE); |
d. | System.out.println(INTEGER.MAX_VALUE); |
41. What are the tokens in the following statement?
StringTokenizer st = new StringTokenizer("9-14-2018", "-", true);
a. | 9, 14, 2018 | c. | - |
b. | 9, 14, 2018, - | d. | None of these |
42. What are the tokens in the following code?
String str = "123-456-7890";
String[] tokens = str.split("-");
a. | 123, 456,7890 | c. | - |
b. | 123, 456, 7890,- | d. | None of these |
43. What will be displayed after the following code is executed?
boolean matches;
String str1 = "The cow jumped over the moon.";
String str2 = "moon";
matches = str1.endsWith(str1);
System.out.println(matches);
a. | true | b. | moon | c. | false | d. | The cow |
44. What will be the value of matches after the following code is executed?
boolean matches;
String[] productCodes = {"456HI345", "3456hj"};
matches = productCodes[0].regionMatches(true, 1,
productCodes[1], 2, 3);
a. | 56H | b. | 56h | c. | true | d. | false |
45. What will be displayed after the following code is executed?
String str1 = "The quick brown fox jumped over the lazy dog.";
String str2 = str1.substring(20, 26);
System.out.println(str2);
a. | n fox | b. | jumped | c. | lazy d | d. | x jump |
46. What will be displayed after the following code is executed?
StringBuilder strb = new StringBuilder(12);
strb.append("The cow ");
strb.append("jumped over the ");
strb.append("moon.");
System.out.println(strb);
a. | The cow jumped over the moon. |
b. | The cow jumped over the moon. |
c. | The cow jump |
d. | 12The cow jumped over the moon. |
47. What will be displayed after the following statements are executed?
StringBuilder strb = new StringBuilder("We have lived
in Chicago, Trenton, and Atlanta.");
strb.replace(17, 24, "Tampa");
System.out.println(strb);
a. | We have lived in Tampa, Trenton, and Atlanta. |
b. | We have lived in Chicago, Trenton, and Tampa. |
c. | We have lived in Chicago,Tampaon, and Atlanta. |
d. | We have lived in Chicago, Tampa, and Atlanta. |
48. What will be displayed after the following statements are executed?
String str = "red$green&blue#orange";
String[] tokens = str.split("[$&#]");
for (String s : tokens)
System.out.print(s + " ");
a. | red green blue orange |
b. | red $ green & blue # orange |
c. | $ & # |
d. | red[$&#]green[$&#]blue[$&#]orange |