Java How to Program, 10/e Multiple Choice Test Bank 5 of 7
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
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);
d. All are equivalently efficient.
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