Ch 3 - Core Java APIs Flashcards
What would the output of the following code be?
String string = “animals”;
System.out.println(string.substring(3));
System.out.println(string.substring(string.indexOf(‘m’)));
System.out.println(string.substring(3,4));
System.out.println(string.substring(3,7));
mals
mals
m
mals
When the indexOf() method can’t find a match, what does it do?
it returns -1 when no match is found.
What would the output of the following code be? String string = "animals"; System.out.println(string.charAt(0)); System.out.println(string.charAt(6)); System.out.println(string.charAt(7));
a
s
StringIndexOutOfBoundsException
What would the output of the following code be?
System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));
true
false
true
false
What is the difference between the following two statements: String name = "Fluffy"; String name = new String("Fluffy");
String name = “Fluffy”;
uses the String pool
String name = new String("Fluffy"); creates a new object, even though it is less efficient than using the String Pool.
What is the output of the following lines of code? System.out.println(1 + 2); System.out.println("a" + "b"); System.out.println("a" + "b" + 3); System.out.println(1 + 2 + "c");
3
ab
ab3
3c
true/false: a String is an example of a reference type.
true
What is the output of the following code:
int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);
64
What would the output of the following code be? String string = "animals"; System.out.println(string.charAt(0)); System.out.println(string.charAt(6)); System.out.println(string.charAt(7));
a
s
Index out of bounds Exception
What would the output of the following code be?
System.out.println(“abc”.equals(“ABC”));
System.out.println(“ABC”.equals(“ABC”));
System.out.println(“abc”.equalsIgnoreCase(“ABC”));
false
true
true
What would the output of the following code be?
System.out.println(“abcabc”.replace(‘a’, ‘A’));
System.out.println(“abcabc”.replace(“a”, “A”));
AbcAbc
AbcAbc
What is aka the intern pool, is the location in the Java virtual machine (JVM) that collects all strings?
The String Pool
s += “2” means the same thing as
s = s + 2;
A String is mutable/immutable?
immutable
What would the output of the following code be?
String string = “animals”;
System.out.println(string.indexOf(‘a’));
System.out.println(string.indexOf(“al”));
System.out.println(string.indexOf(‘a’, 4));
System.out.println(string.indexOf(“al”, 5));
0
4
4
-1
What would the output of the following code be?
System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));
true
false
true
false
What would the output of the following code be?
System.out.println(“abc”.trim());
System.out.println(“\t a b c\n”.trim());
abc
a b c
What would the output of the following code be?
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));
true
false
What is the output of the following lines of code? System.out.println(1 + 2); System.out.println("a" + "b"); System.out.println("a" + "b" + 3); System.out.println(1 + 2 + "c");
3
ab
ab3
3c
What are the rules for using the + character in string concatenation?
- If both operands are numeric, + means numeric addition.
- If either operand is a String, + means concatenation.
- The expression is evaluated left to right.
true/false: The String class is special and does not need to be instantiated with the keyword “new”.
true
What would the output of the following code be?
String string = “animals”;
System.out.println(string.toUpperCase());
System.out.println(“Abc123”.toUpperCase());
ANIMALS
ABC123
What would the output of the following code be?
String string = “animals”;
System.out.println(string.substring(3,3));
System.out.println(string.substring(3,2));
System.out.println(string.substring(3,8));
empty string
throws exception
throws exception
How is StringBuffer different than StringBuilder?
StringBuffer does the same thing as StringBuilder but more slowly because it is thread safe.