A String Constructor Cannot Be Passed ________.
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.
d. C and D.
14.2 Q2: An anonymous String ________.
a. has no value
b. is a string literal
c. can be changed
d. none of the above
:b. is a string literal.
14.3.1 Q1: A String constructor cannot be passed ________.
a. char arrays.
b. int arrays.
c. byte arrays.
d. Strings.
b. int arrays.
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
c. Cannot be changed.
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.
a. The String method length()
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: " ;
for (int i = s1.length() - 1; i >= 0; i--)
output += s1.charAt(i) + " " ;
a. 1.
b. 4.
c. 5.
d. 7.
d. 7.
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);
a. s1.regionMatches(true, 0, 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
d. All of the above
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.
c. i = 4 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.
a. i = 2 and j = 24.
14.3.5 Q1: For
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"
c. i = "the time for all" and j = "is the time "
14.3.5 Q2: The String method substring returns ________.
a. a char
b. a String
c. void
d. a char[]
b. a String.
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));
b. String r1 = a.concat(b.concat(c));
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();
c. String r1 = r.replace('t','T').replace('o', '0').replace('y', 'Y');
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
c. toCharacterArray
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;
int j = 456;
String r = String.valueOf(i) + String.valueOf(j);
d. int i = 123;
int j = 456;
String r = i + j;
b. int i = 123;
int j = 456;
String r = String.valueOf(j) + String.valueOf(i);
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
d. All of the above.
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);
b. buf = new StringBuilder(buf2, 32);
14.4.1 Q2: Given the following declaration:
StringBuilder buf = new StringBuilder();
What is the capacity of buf?
a. 0
b. 10
c. 16
d. 20
c. 16
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.
c. The length of a StringBuilder cannot exceed its capacity.
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.
b. buffer has capacity 31.
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);
b. char dest[];
sb1.getChars(2, 4, dest, 0);
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
c. charAt, with the index 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.
c. StringBuilder sb = new StringBuilder(3000);
for (int i = 1; i <= 1000; i++)
sb.append(i);
String s = new String(sb);
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
b. a landrover a
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.
b. Character.isDigit(char c);
14.5 Q2: Which class is not a type-wrapper class?
a. Character
b. Int
c. Double
d. Byte
b. Int
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 = " ";
a. 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.
b. 7.
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 ".
d. foo is "c ", bar is " 1 ".
14.7: Q1: Which of the following is not a word character?
a. w
b. 0
c. _
d. &
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.
c. Both "A" and "A+" will match "AAA", but only "A" will match an empty string.
14.7: Q3: Which of the Java strings represent the regular expression ,\s*?
a. "\,\s\*"
b. ",\\s*"
c. ",\\s\\*"
d. ".\\s\*"
b. ",\\s*"
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.
d. All of the above.