Deck 14: Strings, Characters and Regular Expressions

Full screen (f)
exit full mode
Question
A String constructor cannot be passed ________.

A) char arrays.
B) int arrays.
C) byte arrays.
D) Strings.
Use Space or
up arrow
down arrow
to flip the card.
Question
An anonymous String ________.

A) has no value
B) is a string literal
C) can be changed
D) none of the above
Question
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;
Question
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();
Question
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.
Question
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.
Question
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
Question
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);
Question
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.
Question
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);
Question
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);
Question
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
Question
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
Question
Given the following declaration:
StringBuilder buf = new StringBuilder();
What is the capacity of buf?

A) 0
B) 10
C) 16
D) 20
Question
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.
Question
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
Question
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.
Question
String objects are immutable. This means they ________.

A) must be initialized
B) cannot be deleted
C) cannot be changed
D) None of the abov
Question
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.
Question
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.
Question
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.
Question
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 = " ";
Question
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 ".
Question
Which class is not a type-wrapper class?

A) Character
B) Int
C) Double
D) Byte
Question
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.
Question
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.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/26
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 14: Strings, Characters and Regular Expressions
1
A String constructor cannot be passed ________.

A) char arrays.
B) int arrays.
C) byte arrays.
D) Strings.
int arrays.
2
An anonymous String ________.

A) has no value
B) is a string literal
C) can be changed
D) none of the above
is a string literal
3
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
4
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();
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
5
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
6
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
7
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
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
8
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);
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
9
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
10
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);
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
11
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);
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
12
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
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
13
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
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
14
Given the following declaration:
StringBuilder buf = new StringBuilder();
What is the capacity of buf?

A) 0
B) 10
C) 16
D) 20
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
15
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
16
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
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
17
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
18
String objects are immutable. This means they ________.

A) must be initialized
B) cannot be deleted
C) cannot be changed
D) None of the abov
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
19
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
20
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
21
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
22
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 = " ";
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
23
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 ".
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
24
Which class is not a type-wrapper class?

A) Character
B) Int
C) Double
D) Byte
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
25
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
26
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.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 26 flashcards in this deck.