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.
What are the three ways to construct a StringBuilder?
StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder("animal"); StringBuilder sb3 = new StringBuilder(10);
What is the output of the following code?
String result = “AniMal “.trim().toLowerCase().replace(‘a’, ‘A’);
System.out.println(result);
AnimAl
What would the output of the following code be? StringBuilder sb = new StringBuilder("animals"); String sub = sb.substring(sb.indexOf("a"), sb.indexOf("al")); int len = sb.length(); char ch = sb.charAt(6); System.out.println(sub + " " + len + " " + ch);
anim 7 s
What datatype does the .substring() method return?
String
What is the output of the following code?
StringBuilder sb = new StringBuilder(“ABC”);
sb.reverse();
System.out.println(sb);
CBA
How many objects do you think this piece of code creates?
String alpha = “”;
for (char current = ‘a’; current <= ‘z’; current++)
alpha += current;
System.out.println(alpha);
27
What is the output of the following code? StringBuilder a = new StringBuilder("abc"); StringBuilder b = a.append("de"); b = b.append("f").append("g"); System.out.println("a=" + a); System.out.println("b=" + b);
a=abcdef
b=abcdef
What is the output of the following code? StringBuilder sb = new StringBuilder("abcdef"); sb.delete(1,3); sb.deleteCharAt(5);
index out of bounds exception
sb.deleteCharAt(5);
true/false: StringBuilder is immutable
false
What is the output of the following code? StringBuilder sb = new StringBuilder().append(1).append('c'); sb.append("-").append(true); System.out.println(sb);
1c-true
What is the output of the following code?
String a = “abc”;
String b = a.toUpperCase();
b = b.replace(“B”, “2”).replace(‘C’, ‘3’);
System.out.println(“a=” + a);
System.out.println(“b=” + b);
a=abc
b=A23
What is the output of the following code?
StringBuilder sb = new StringBuilder(“animals”);
sb.insert(7, “-“);
sb.insert(0, “-“);
sb.insert(4, “-“);
System.out.println(sb);
-ani-mals-
What would the output of the following code be?
StringBuilder sb = new StringBuilder(“start”);
sb.append(“+middle”);
StringBuilder name = sb.append(“+end”);
sb and name are pointing to the same object, thus the value assigned to each is:
start+middle+end
What is the output of the following code?
StringBuilder puzzle = new StringBuilder("Java"); System.out.println(puzzle); System.out.println(puzzle.reverse()); System.out.println(puzzle);
Java
avaJ
avaJ
What is the output of the following code?
StringBuilder puzzle = new StringBuilder("Stephen"); System.out.println(puzzle); puzzle.substring(1); System.out.println(puzzle);
Stephen
Stephen
What is the output of the following code?
String x = new String("Hello World"); String y = "Hello World"; System.out.println(x == y); System.out.println(x.equals(y));
false
true
The first line forces the creation of a new object, and the second line uses the string pool, so the first comparison is false.
What is the output of the following code?
String x = “Hello World”;
String y = “ Hello World”.trim();
System.out.println(x.equals(y));
true
During an equality check, if an object does not implement an equals() method, Java determines…
… if the references point to the same object, which is exactly what == does.
What is the output of the following code?
StringBuilder one = new StringBuilder(); StringBuilder two = new StringBuilder(); StringBuilder three = one.append("a"); System.out.println(one == two); System.out.println(one == three);
false
true
What would the output of the following code be?
int[] numbers = new int[3];
for (int i : numbers ) {
System.out.println(i);
}
0
0
0
true/false, the following code creates two arrays of int values:
int[] ids, types;
true
What would the output of the following code be?
String [] bugs = {“cricket”, “beetle”, “ladybug”};
String[] bugs2 = {“one”, “two”};
System.out.println(bugs.equals(bugs2));
false
What would the output of the following code be?
String[] names;
System.out.println(names);
null
What would the output of the following code be?
public class ArrayType {
public static void main(String args[]) { String [] bugs = {"cricket", "beetle", "ladybug"}; String [] alias = bugs; System.out.println(bugs.equals(alias)); System.out.println(bugs.toString()); } }
true
[Ljava.lang.String;@160bc7c0
What would the output of the following code be?
int[] newVals = {}; newVals[0] = 1; newVals[1] = 2; if (newVals == null) { System.out.println("newVals is null");
} else {
System.out.println(“newVals is not null”);
}
Index out of bounds exception on this line:
newVals[0] = 1;
Instantiate a new array called “numbers” of int values of 25, 30, 35.
int[] numbers = new int[] {25, 30, 35};
- or -
int[] numbers = {25, 30, 35};
What would the output of the following code be?
String [] bugs = {“cricket”, “beetle”, “ladybug”};
for (String b : bugs) {
System.out.println(b);
}
cricket
beetle
ladybug
true/false, the equals method on arrays looks at the elements of the array.
false
int is what data type and int[] is what data type?
int is a primitive, and int[] is an object
What does the following code output?
public class Tiger { String name; public static void main(String[] args) { Tiger t1 = new Tiger(); Tiger t2 = new Tiger(); Tiger t3 = t1;
System.out.println(t1 == t3); System.out.println(t1 == t2); System.out.println(t1.equals(t2)); } }
true
false
false
true/false: An array is an ordered list.
true
true/false, the following code creates two arrays of int values:
int ids[], types;
false
What would the output of the following code be?
String[] numbers = {“10”, “9”, “100”};
Arrays.sort(numbers);
for (String string : numbers)
System.out.println(numbers[string] + “ “);
10 100 9
What dimensions are the arrays being declared in the code below?
int[][] vars1;
int vars2[][];
int[] vars3[];
int[] vars4[], space[][];
two
two
two
two and three
What would the output of the following code be?
int[] numbers = {2, 4, 6, 8};
System.out.println(Arrays.binarySearch(numbers, 2));
System.out.println(Arrays.binarySearch(numbers, 4));
System.out.println(Arrays.binarySearch(numbers, 1));
System.out.println(Arrays.binarySearch(numbers, 3));
System.out.println(Arrays.binarySearch(numbers, 9));
0 1 -1 -2 -5
Will the following code compile?
String[] strings = { "stringvalue" }; Object[] objects = strings; String[] againStrings = (String[]) objects; againStrings[0] = new StringBuilder(); object[0] = new StringBuilder();
no. The following line produces a compiler error: againStrings[0] = new StringBuilder();
The following line doesn't create compiler error, but it throws and ArrayStoreException when you try to run it. object[0] = new StringBuilder();
What would the output of the following code be?
int[] numbers = {6, 9, 1};
Arrays.sort(numbers);
for (int i=0; i< numbers.length; i++)
System.out.println(numbers[i] + “ “);
1 6 9