AP Computer Science - String Class
What method is used to compare two strings?
compareTo()
int test = "fox".compareTo( "fox" ); What is the value of test?
0
"foxtrot".substring( 5, 6 ).length() What is returned by length?
1
"foxtrot".substring( 3, 5 ).length() What is returned by length?
2
int letterNum = 1+ "tictactoe".indexOf( 'c' ) What does letterNum equal?
3
"tictactoe".indexOf( 'a' ) What is returned by indexOf?
4
String str = "Mocha"; int len = str.length; What will happen?
The code will not compile. To access the length property of a String object, you must use the length() method. Note: You can access the length property of an array directly... int len = arr.length;
Consider this method, called test: boolean test( String str ) { return str == "fox" ); } If str contains "fox", will the method return true?
Unpredictable.. == compares the memory location in which Strings are stored, so it would probably return false, but when it sees "fox", it might say , Oh - I already have that string in the str location, so I'll just point to that.. thus the method would return true.
int test = "fox".compareTo( "Fox" ); What is the approximate value of test?
greater than 0
int test = "fox".compareTo( "Hen" ); What is the approximate value of test?
greater than 0
What are the signatures of the two overloads of the substring method?
substring( int, int ) substring( int )
int x = "tictactoe".indexOf( 'T' ) What does x equal?
-1
int test = "fox".compareTo( "hen" ); What is the value of test?
-2
"foxtrot".substring( 4, 4 ).length() What is returned by length?
0
"tictactoe".indexOf( 't' ) What is returned by indexOf?
0
"tictactoe".indexOf( 'e' ) What is returned by indexOf?
8
"foxtrot".substring( 0, 0 ) What is returned by substring?
"" (the empty string)
"foxtrot".substring( 0 ) What is returned by substring?
"foxtrot"
"foxtrot".substring( 3, 5 ) What is returned by substring?
"tr"
"foxtrot".substring( 3 ) What is returned by substring?
"trot"
"foxtrot".substring( 2, 6 ) What is returned by substring?
"xtro"
int test = "hen".compareTo( "fox" ); What is the value of test?
+2
What method is used to find the length of a string?
length()
int test = "Hen".compareTo( "fox" ); What is the approximate value of test?
less than 0
"foxtrot".substring( 0, 1 ) What is returned by substring?
"f"