Deck 16: Strings

Full screen (f)
exit full mode
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.
Use Space or
up arrow
down arrow
to flip the card.
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
The String method substring returns:

A)A char.
B)A String.
C)void.
D)A char[].
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
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
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
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 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
Given the following declaration: StringBuilder buf = new StringBuilder();
What is the capacity of buf?

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

A)Must be initialized.
B)Cannot be deleted.
C)Cannot be changed.
D)None of the above
Question
A String constructor cannot be passed variables of type:

A)char arrays.
B)int arrays.
C)byte arrays.
D)Strings.
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
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
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
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
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
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
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
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 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
Which class is not a type-wrapper class?

A)Character
B)Int
C)Double
D)Byte
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
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 );
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 16: Strings
1
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
2
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
3
The String method substring returns:

A)A char.
B)A String.
C)void.
D)A char[].
B
4
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
5
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
6
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
7
An anonymous String:

A)has no value.
B)is a string literal.
C)can be changed.
D)none of the above.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
8
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
9
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
10
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
11
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
12
String objects are immutable.This means they:

A)Must be initialized.
B)Cannot be deleted.
C)Cannot be changed.
D)None of the above
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
13
A String constructor cannot be passed variables of type:

A)char arrays.
B)int arrays.
C)byte arrays.
D)Strings.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
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.