Java How to Program, 11/e Multiple Choice Test Bank 1 of 7
Chapter 14
Section 14.2 Fundamentals of Characters and Strings
14.2 Q1: Consider the examples below:
A. a string.
B. ‘a string’.
C. “a string”.
D. “1234”.
E. integer.
Which could be the value of a Java variable of type String?
a. A and B.
b. B and E.
c. B and C.
d. C and D.
14.2 Q2: An anonymous String ________.
a. has no value
b. is a string literal
c. can be changed
Section 14.3 Class String
Section 14.3.1 String Constructors
14.3.1 Q1: A String constructor cannot be passed ________.
a. char arrays.
b. int arrays.
c. byte arrays.
d. Strings.
14.3.1 Q2: String objects are immutable. This means they ________.
a. must be initialized
b. cannot be deleted
c. cannot be changed
d. None of the abov
Section 14.3.2 String Methods length, charAt and getChars
14.3.2 Q1: The length of a string can be determined by ________.
a. the String method length()
b. the String instance variable length
c. the String method strlen()
d. All of the above.
14.3.2 Q2: How many String objects are instantiated by the following code segment (not including the literals)?
String s1, output;
s1 = “hello”;
output = “\nThe string reversed is: ” ;
Java How to Program, 11/e Multiple Choice Test Bank 2 of 7
for (int i = s1.length() – 1; i >= 0; i—)
output += s1.charAt(i) + ” ” ;
a. 1.
b. 4.
c. 5.
d. 7.
Section 14.3.3 Comparing Strings
14.3.3 Q1: The statement
s1.equalsIgnoreCase(s4)
is equivalent to which of the following?
a. s1.regionMatches(true, 0, s4, 0, s4.length());
b. s1.regionMatches(0, s4, 0, s4.length());
c. s1.regionMatches(0, s4, 0, s4.length);
d. s1.regionMatches(true, s4, 0, s4.length);
14.3.3 Q2: The statement
s1.startsWith(“art”)
has the same result as which of the following?
a. s1.regionMatches(0, “art”, 0, 3);
b. s2 = s1.getChars(0, 3);
s2.equals(“art”);
c. s1.regionMatches(true, 0, “art”, 0, 3);
d. All of the above
Section 14.3.4 Locating Characters and Substrings in Strings
14.3.4 Q1: For
String c = “hello world”;
The Java statements
int i = c.indexOf(‘o’);
int j = c.lastIndexOf(‘l’);
will result in:
a. i = 4 and j = 8.
b. i = 5 and j = 8.
c. i = 4 and j = 9.
d. i = 5 and j = 9.
14.3.4 Q2: For
String c = “Hello. She sells sea shells”;
The Java statements
int i = c.indexOf(“ll”);
int j = c.lastIndexOf(“ll”);
will result in:
a. i = 2 and j = 24.
b. i = 3 and j = 24.
c. i = 2 and j = 25.
d. i = 3 and j = 23.
Section 14.3.5 Extracting Substrings from Strings
14.3.5 Q1: For
Java How to Program, 11/e Multiple Choice Test Bank 3 of 7
String c = “Now is the time for all”;
The Java statements
String i = c.substring(7);
String j = c.substring(4, 15);
will result in:
a. i = “he time for all” and j = “is the time”
b. i = “the time for all” and j = “s the time”
c. i = “the time for all” and j = “is the time “
d. i = “he time for all” and j = “s the time”
14.3.5 Q2: The String method substring returns ________.
a. a char
b. a String
c. void
d. a char[]
Section 14.3.6 Concatenating Strings
14.3.6 Q1: Consider the statements below:
String a = “JAVA: “;
String b = “How to “;
String c = “Program”;
Which of the statements below will create the String r1 = “JAVA: How to Program”?
a. String r1 = c.concat(b.concat(a));
b. String r1 = a.concat(b.concat(c));
c. String r1 = b.concat(c.concat(a));
d. String r1 = c.concat(c.concat(b));
Section 14.3.7 Miscellaneous String Methods
14.3.7 Q1: Consider the String below:
String r = “a toyota”;
Which of the following will create the String r1 = “a TOYOTa”?
a. String r1 = r.replace(“toyot”, TOYOT”);
b. String r1 = r.replace(‘t’,’T’);
r1 = r.replace(‘o’,’0′);
r1 = r.replace(‘y’,’Y’);
c. String r1 = r.replace(‘t’,’T’).replace(‘o’, ‘0’).replace(‘y’, ‘Y’);
d. String r1 = r.substring(2, 4).toUpperCase();
14.3.7 Q2: Which of the following is not a method of class String?
a. toUpperCase
b. trim
c. toCharacterArray
d. All of the above are methods of class String
Section 14.3.8 String Method valueOf
14.3.8 Q1: Which of the following will create a String different from the other three?
a. String r = “123456”
b. int i = 123;
int j = 456;
String r = String.valueOf(j) + String.valueOf(i);
c. int i = 123;
Java How to Program, 11/e Multiple Choice Test Bank 4 of 7
int j = 456;
String r = String.valueOf(i) + String.valueOf(j);
d. int i = 123;
int j = 456;
String r = i + j;
Section 14.4 Class StringBuilder
14.4 Q1: StringBuilder objects can be used in place of String objects if ________.
a. the string data is not constant
b. the string data size may grow
c. the programs frequently perform string concatenation
Section 14.4.1 StringBuilder Constructors
14.4.1 Q1: Given the following declarations:
StringBuilder buf;
StringBuilder buf2 = new StringBuilder();
String c = new String(“test”);
Which of the following is not a valid StringBuilder constructor?
a. buf = new StringBuilder();
b. buf = new StringBuilder(buf2, 32);
c. buf = new StringBuilder(32);
d. buf = new StringBuilder(c);
14.4.1 Q2: Given the following declaration:
StringBuilder buf = new StringBuilder();
What is the capacity of buf?
a. 0
b. 10
c. 16
Section 14.4.2 StringBuilder Methods length, capacity, setLength and
ensureCapacity
14.4.2 Q1: Which of the following statements is true?
a. The capacity of a StringBuilder is equal to its length.
b. The capacity of a StringBuilder cannot exceed its length.
c. The length of a StringBuilder cannot exceed its capacity.
d. Both a and b are true.
14.4.2 Q2: Given the following declarations:
StringBuilder buffer = new StringBuilder(“Testing Testing”);
buffer.setLength(7);
buffer.ensureCapacity(5);
Which of the following is true?
a. buffer has capacity 5.
b. buffer has capacity 31.
c. buffer has content “Testin”.
d. buffer has length 15.
Java How to Program, 11/e Multiple Choice Test Bank 5 of 7
© Copyright 1992-2018 by Deitel & Associates, Inc. and Pearson Education, Inc.
Section 14.4.3 StringBuilder Methods charAt, setCharAt, getChars and reverse
14.4.3 Q1: Consider the statement below:
StringBuilder sb1 = new StringBuilder(“a toyota”);
Which of the following creates a String object with the value “toy”?
a. String res = sb1.subString(2, 5);
b. char dest[] = new char[sb1.length()];
sb1.getChars(2, 5, dest, 0);
String res = new String(dest);
c. char dest[] = new char[sb1.length];
dest = sb1.getChars(2, 5);
String res = new String(dest);
d. char dest[] = new char[sb1.length()];
dest = sb1.getChars(0, 3);
String res = new String(dest);
14.4.3 Q2: To find the character at a certain index position within a String, use the method ________.
a. getChars, with the index as an argument
b. getCharAt, with the index as an argument
c. charAt, with the index as an argument
d. charAt, with the character you are searching for as an argument
Section 14.4.4 StringBuilder append Methods
14.4.4 Q1: Which of the following creates the string of the numbers from 1 to 1000 most efficiently?
a. String s;
for (int i = 1; i <= 1000; i++)
s += i;
b. StringBuilder sb = new StringBuilder(10);
for (int i = 1; i <= 1000; i++)
sb.append(i);
String s = new String(sb);
c. StringBuilder sb = new StringBuilder(3000);
for (int i = 1; i <= 1000; i++)
sb.append(i);
String s = new String(sb);
Section 14.4.5 StringBuilder Insertion and Deletion Methods
14.4.5 Q1: Consider the statements below:
StringBuilder sb = new StringBuilder(“a toyota”);
sb.insert(2, “landrover”);
sb.delete(11, 16);
sb.insert(11, ” “);
The StringBuilder contents at the end of this segment will be ________.
a. a landrovertoyota
b. a landrover a
c. a landrov a
d. a landrover toy a
Java How to Program, 11/e Multiple Choice Test Bank 6 of 7
© Copyright 1992-2018 by Deitel & Associates, Inc. and Pearson Education, Inc.
Section 14.5 Class Character
14.5 Q1: Which of the following are static Character methods?
a. Character.hashcode(char c);
b. Character.isDigit(char c);
c. Character.equals(char c);
d. All of the above.
14.5 Q2: Which class is not a type-wrapper class?
a. Character
b. Int
c. Double
d. Byte
Section 14.6 Tokenizing Strings
14.6 Q1: Consider the Java segment:
String line1 = new String(“c = 1 + 2 + 3”) ;
StringTokenizer tok = new StringTokenizer(line1, delimArg);
For the String line1 to have 4 tokens, delimArg should be:
a. String delimArg = “+=”;
b. String delimArg = “123”
c. String delimArg = “c+”;
d. String delimArg = ” “;
14.6 Q2: Consider the Java segment:
String line1 = new String(“c = 1 + 2 + 3”) ;
StringTokenizer tok = new StringTokenizer(line1);
int count = tok.countTokens();
The value of count is ________.
a. 8.
b. 7.
c. 13.
d. 4.
14.6 Q3: Consider the Java segment:
String line1 = new String(“c = 1 + 2 + 3”) ;
StringTokenizer tok = new StringTokenizer(line1, “+=”);
String foo = tok.nextToken();
String bar = tok.nextToken();
The values of foo and bar are:
a. foo is “c ”, bar is “ = ”.
b. foo is “c”, bar is “ ”.
c. foo is “ = ”, bar is “ + ”.
d. foo is “c ”, bar is “ 1 ”.
Section 14.7 Regular Expressions, Class Pattern and Class Matcher
14.7: Q1: Which of the following is not a word character?
a. w
Java How to Program, 11/e Multiple Choice Test Bank 7 of 7
b. 0
c. _
d. &
14.7 Q2: Which of the following statements is true?
a. Ranges of characters can be represented by placing a ~ between two characters.
b. [^Z] is the same as [A~Y].
c. Both “A*” and “A+” will match “AAA”, but only “A*” will match an empty string.
d. All of above.
Replacing Substrings and Splitting Strings
14.7: Q3: Which of the Java strings represent the regular expression ,\s*?
a. “\,\s\*”
b. “,\\s*”
c. “,\\s\\*”
d. “.\\s\*”
Classes Pattern and Matcher
14.7: Q4: Which of the following statements is true?
a. Class Matcher provides methods find, lookingAt, replaceFirst and replaceAll.
b. Method matches (from class String, Pattern or Matcher) will return true only if the entire search object
matches the regular expression.
c. Methods find and lookingAt (from class Matcher) will return true if a portion of the search object matches the
regular expression.
d. All of the above.