Ch 3 - Core Java APIs Flashcards

1
Q

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));

A

mals
mals
m
mals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When the indexOf() method can’t find a match, what does it do?

A

it returns -1 when no match is found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
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

a
s
StringIndexOutOfBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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”));

A

true
false
true
false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What is the difference between the following two statements:
String name = "Fluffy";
String name = new String("Fluffy");
A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
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");
A

3
ab
ab3
3c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

true/false: a String is an example of a reference type.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output of the following code:
int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);

A

64

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
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

a
s
Index out of bounds Exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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”));

A

false
true
true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What would the output of the following code be?
System.out.println(“abcabc”.replace(‘a’, ‘A’));
System.out.println(“abcabc”.replace(“a”, “A”));

A

AbcAbc

AbcAbc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is aka the intern pool, is the location in the Java virtual machine (JVM) that collects all strings?

A

The String Pool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

s += “2” means the same thing as

A

s = s + 2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A String is mutable/immutable?

A

immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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));

A

0
4
4
-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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”));

A

true
false
true
false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What would the output of the following code be?
System.out.println(“abc”.trim());
System.out.println(“\t a b c\n”.trim());

A

abc

a b c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What would the output of the following code be?
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));

A

true

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
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");
A

3
ab
ab3
3c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the rules for using the + character in string concatenation?

A
  1. If both operands are numeric, + means numeric addition.
  2. If either operand is a String, + means concatenation.
  3. The expression is evaluated left to right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

true/false: The String class is special and does not need to be instantiated with the keyword “new”.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What would the output of the following code be?
String string = “animals”;
System.out.println(string.toUpperCase());
System.out.println(“Abc123”.toUpperCase());

A

ANIMALS

ABC123

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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));

A

empty string
throws exception
throws exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How is StringBuffer different than StringBuilder?

A

StringBuffer does the same thing as StringBuilder but more slowly because it is thread safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are the three ways to construct a StringBuilder?

A
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder("animal");
StringBuilder sb3 = new StringBuilder(10);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the output of the following code?
String result = “AniMal “.trim().toLowerCase().replace(‘a’, ‘A’);
System.out.println(result);

A

AnimAl

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
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);
A

anim 7 s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What datatype does the .substring() method return?

A

String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the output of the following code?
StringBuilder sb = new StringBuilder(“ABC”);
sb.reverse();
System.out.println(sb);

A

CBA

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

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);

A

27

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
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

a=abcdef

b=abcdef

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
What is the output of the following code?
StringBuilder sb = new StringBuilder("abcdef");
sb.delete(1,3);
sb.deleteCharAt(5);
A

index out of bounds exception

sb.deleteCharAt(5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

true/false: StringBuilder is immutable

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
What is the output of the following code?
StringBuilder sb = new StringBuilder().append(1).append('c');
sb.append("-").append(true);
System.out.println(sb);
A

1c-true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

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

a=abc

b=A23

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

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);

A

-ani-mals-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What would the output of the following code be?
StringBuilder sb = new StringBuilder(“start”);
sb.append(“+middle”);
StringBuilder name = sb.append(“+end”);

A

sb and name are pointing to the same object, thus the value assigned to each is:
start+middle+end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

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);
A

Java
avaJ
avaJ

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is the output of the following code?

StringBuilder puzzle = new StringBuilder("Stephen");
System.out.println(puzzle);
puzzle.substring(1);
System.out.println(puzzle);
A

Stephen

Stephen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

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));
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is the output of the following code?

String x = “Hello World”;
String y = “ Hello World”.trim();
System.out.println(x.equals(y));

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

During an equality check, if an object does not implement an equals() method, Java determines…

A

… if the references point to the same object, which is exactly what == does.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

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);
A

false

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What would the output of the following code be?

int[] numbers = new int[3];
for (int i : numbers ) {
System.out.println(i);
}

A

0
0
0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

true/false, the following code creates two arrays of int values:
int[] ids, types;

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What would the output of the following code be?

String [] bugs = {“cricket”, “beetle”, “ladybug”};
String[] bugs2 = {“one”, “two”};
System.out.println(bugs.equals(bugs2));

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What would the output of the following code be?

String[] names;
System.out.println(names);

A

null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

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());
} }
A

true

[Ljava.lang.String;@160bc7c0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

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”);
}

A

Index out of bounds exception on this line:

newVals[0] = 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Instantiate a new array called “numbers” of int values of 25, 30, 35.

A

int[] numbers = new int[] {25, 30, 35};
- or -
int[] numbers = {25, 30, 35};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What would the output of the following code be?

String [] bugs = {“cricket”, “beetle”, “ladybug”};
for (String b : bugs) {
System.out.println(b);

}

A

cricket
beetle
ladybug

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

true/false, the equals method on arrays looks at the elements of the array.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

int is what data type and int[] is what data type?

A

int is a primitive, and int[] is an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

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));
	}
}
A

true
false
false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

true/false: An array is an ordered list.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

true/false, the following code creates two arrays of int values:
int ids[], types;

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

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] + “ “);

A

10 100 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

What dimensions are the arrays being declared in the code below?

int[][] vars1;
int vars2[][];
int[] vars3[];
int[] vars4[], space[][];

A

two
two
two
two and three

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

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));

A
0
1
-1
-2
-5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Will the following code compile?

String[] strings = { "stringvalue" };
Object[] objects = strings;
String[] againStrings = (String[]) objects;
againStrings[0] = new StringBuilder();
object[0] = new StringBuilder();
A
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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

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] + “ “);

A

1 6 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

true/false, the following lines of code will compile:

public static void main(String[] args)
public static void main(String args[])
public static void main(String… args)

A

true

63
Q

Considering the following code:
int[] numbers = new int[10];
for (int i=0; i < numbers.length; i++)
numbers[i] = i + 5;

Will the following lines of code compile?
numbers[10] = 5;
numbers[numbers.length] = 5;
for (int i=0; i =< numbers.length; i++)

A

numbers[10] = 5;

64
Q

What kind of results would you get when trying to do a binary search in the following array?

int[] numbers = {3, 2, 1, 5};

A

unpredictable, because the array is unsorted.

65
Q

What would the output of the following code be?

List birds = new ArrayList();
birds.add("hawk");
birds.add(1, "robin");
birds.add(0, "blue jay");
birds.add(1, "cardinal");
birds.add("sparrow");
System.out.println(birds);
A

[blue jay, cardinal, hawk, robin, sparrow]

66
Q

What would the output of the following code be?

List birds = new ArrayList();

birds. add(“hawk”);
birds. remove(1);

A

IndexOutOfBoundsException

67
Q

What would the output of the following code be?

List birds = new ArrayList();
System.out.println(birds.isEmpty());
System.out.println(birds.size());
birds.add("hawk");
birds.add("hawk");
System.out.println(birds.isEmpty());
System.out.println(birds.size());
A

true
0
false
2

68
Q

What would the output of the following code be?

List one = new ArrayList();
List two = new ArrayList();
System.out.println(one.equals(two));
one.add("a");
System.out.println(one.equals(two));
two.add("a");
System.out.println(one.equals(two));
one.add("b");
two.add(0, "b");
System.out.println(one.equals(two));
A

true
false
true
false

69
Q

true/false, the following compiles:

ArrayList list7 = new List<>();

A

false

70
Q

What would the output of the following code be?

ArrayList list = new ArrayList();
list.add(“hawk”);
list.add(Boolean.TRUE);
System.out.println(list);

A

[hawk, true]

  • it is ok to add objects of different types to this ArrayList because we didn’t declare the type when initializing the ArrayList.
71
Q

What would the output of the following code be?

List birds = new ArrayList();
birds.add("hawk");
birds.add("hawk");
birds.add("hawk");
System.out.println(birds.remove("hawk"));
System.out.println(birds);
A

true

[hawk, hawk]

72
Q

<> is called the what type of operator?

A

The diamond operator

73
Q

Name five ways to create an ArrayList

A
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList(10);
ArrayList list3 = new ArrayList(list2);
ArrayList list4 = new ArrayList();
ArrayList list5 = new ArrayList<>();
74
Q

What would the output of the following code be?

ArrayList list = new ArrayList();
System.out.println(list.add("hawk"));
System.out.println(list.add(Boolean.TRUE));
A

true

true

75
Q

What would the output of the following code be?

List birds = new ArrayList();
birds.add("hawk");
System.out.println(birds.contains("hawk"));
System.out.println(birds.contains("robin"));
A

true

false

76
Q

What would the output of the following code be?

List birds = new ArrayList();
birds.add("hawk");
System.out.println(birds.size());
birds.set(0, "robin");
System.out.println(birds.size());
birds.set(1, "robin");
A

1
1
IndexOutOfBoundsException

77
Q

What would the output of the following code be?

ArrayList list = new ArrayList();

list. add(“hawk”);
list. add(Boolean.TRUE);

A

Compiler error on this line:

list. add(Boolean.TRUE);
* We’ve specifically declared this ArrayList to hold Strings.

78
Q

What would the output of the following code be?

List birds = new ArrayList();
birds.add("hawk");
System.out.println(birds.remove("hawk"));
System.out.println(birds.remove("hawk"));
A

true
false

  • the second line returns false because there is no more elements in the ArrayList of “hawk”.
79
Q

What would the output of the following code be?

List birds = new ArrayList();
birds.add("hawk");
birds.add("hawk");
System.out.println(birds.isEmpty());
System.out.println(birds.size());
birds.clear();
System.out.println(birds.isEmpty());
System.out.println(birds.size());
A

false
2
true
0

80
Q

What would the output of the following code be?

ArrayList one1 = new ArrayList();
one1.add(“one”);
ArrayList one2 = new ArrayList();
one2.add(“one”);

if (one1.equals(one2)) {
	System.out.println("They are equal");
} else {
	System.out.println("They are not equal");
}
if (one1 == one2) {
	System.out.println("They are equal");
} else {
	System.out.println("They are not equal");
}
A

They are equal

They are not equal

81
Q
Which of the following code will compile:
Integer i0 = new Integer();
Integer i1 = new Integer(1);
Integer i2 = new Integer((int) 1);
Long l0 = new Long();
Long l1 = new Long(1);
Long l2 = new Long((long) 1);
A
Integer i0 = new Integer();			// no
Integer i1 = new Integer(1);		// yes
Integer i2 = new Integer((int) 1);	// yes
Long l0 = new Long();				// no
Long l1 = new Long(1);				// yes
Long l2 = new Long((long) 1);		// yes
82
Q

Convert the string “1” to a float primitive.

A

float f = Float.parseFloat(“1”);

83
Q

Convert the string “2” to a Long wrapper object.

A

Long l2 = Long.valueOf(“2”);

84
Q

What is the output of the following code?

List numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.remove(1);
System.out.println(numbers);
A

[1]
This line is removing the array member at index 1, not with the value 1:
numbers.remove(1);

85
Q

Convert the string “1” to a short primitive.

A

short s = Short.parseShort(“1”);

86
Q

Convert the string “true” to a Boolean wrapper object.

A

Boolean b2 = Boolean.valueOf(“true”);

87
Q

Convert the string “2” to a Double wrapper object.

A

Double d2 = Double.valueOf(“2”);

88
Q

Convert the string “2” to a Byte wrapper object.

A

Byte b = Byte.valueOf(“2”);

89
Q

Which of the following code will compile:

Float f0 = new Float();
Float f1 = new Float(1.0);
Float f2 = new Float((float) 1.0);
Double d0 = new Double();
Double d1 = new Double(1.0);
Double d2 = new Double((double) 1.0);
Character c0 = new Character();
Character c1 = new Character('c');
Character c2 = new Character((char) 'c');
A
Float f0 = new Float();						// no
Float f1 = new Float(1.0);					// yes
Float f2 = new Float((float) 1.0);			// yes
Double d0 = new Double();					// no
Double d1 = new Double(1.0);				// yes
Double d2 = new Double((double) 1.0);		// yes
Character c0 = new Character();				// no
Character c1 = new Character('c');			// yes
Character c2 = new Character((char) 'c');	// yes
90
Q

Convert the string “2” to a Short wrapper object.

A

Short s2 = Short.valueOf(“2”);

91
Q

Convert the string “1” to an int primitive.

A

int i = Integer.parseInt(“1”);

92
Q

Convert the string “true” to a boolean primitive.

A

boolean b = Boolean.parseBoolean(“true”);

93
Q

What is the output of the following code?

List heights = new ArrayList<>();
heights.add(null);
int h = heights.get(0);

A

NullPointerException on this line:
int h = heights.get(0);

That is because it is trying to get the int value of null.

94
Q

What is the output of the following code?

String[] array = {"Hawk", "Robin"};
List list = Arrays.asList(array);
System.out.println(list.size());
list.set(1, "test");
array[0] = "new";
for (String b : array) System.out.print(b + " ");
list.remove(1);
A

2
new test
UnsupportedOperation Exception

95
Q

What is the output of the following code?

List list = new ArrayList();
list.add("hawk");
list.add("robin");
Object[] objectArray = list.toArray();
System.out.println(objectArray.length);
String[] stringArray = list.toArray();
System.out.println(stringArray.length);
A

Type mismatch error on this line:
String[] stringArray = list.toArray();

cannot convert from Object[] to String[]

96
Q

Convert the string “1” to a double primitive.

A

double d = Double.parseDouble(“1”);

97
Q

Which of the following code will compile:

Boolean boo0 = new Boolean();
Boolean boo1 = new Boolean(true);
Byte bte0 = new Byte();
Byte bte1 = new Byte(1);
Byte bte2 = new Byte((byte) 1);
Short sh0 = new Short();
Short sh1 = new Short(1);
Short sh2 = new Short((short) 1);
A
Boolean boo0 = new Boolean();		// no
Boolean boo1 = new Boolean(true);	// yes
Byte bte0 = new Byte();				// no
Byte bte1 = new Byte(1);			// no
Byte bte2 = new Byte((byte) 1);		// yes
Short sh0 = new Short();			// no
Short sh1 = new Short(1);			// no
Short sh2 = new Short((short) 1);	// yes
98
Q

Convert the string “1” to a byte primitive.

A

byte b0 = Byte.parseByte(“1”);

99
Q

What is the output of the following code?

List list = new ArrayList();
list.add("hawk");
list.add("robin");
Object[] objectArray = list.toArray();
System.out.println(objectArray.length);
String[] stringArray = list.toArray(new String[5]);
System.out.println(stringArray.length);
A

2

5

100
Q

Convert the string “2” to an Integer wrapper object.

A

Integer i2 = Integer.valueOf(“2”);

101
Q

What is meant by “Autoboxing”?

A

You can just type the primitive value and Java will convert it to the relevant wrapper class for you.

102
Q

What is the output of the following code?

List numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.remove(new Integer(1));
System.out.println(numbers);
A

[2]
This line is removing the array member with value of 1:
numbers.remove(new Integer(1));

103
Q

Convert the string “2” to a Float wrapper object.

A

Float f2 = Float.valueOf(“2”);

104
Q

What is the output of the following code?

List list = new ArrayList();
list.add("hawk");
list.add("robin");
Object[] objectArray = list.toArray();
System.out.println(objectArray.length);
String[] stringArray = list.toArray(new String[0]);
System.out.println(stringArray.length);
A

2

2

105
Q

Convert the string “1” to a long primitive.

A

long l = Long.parseLong(“1”);

106
Q

What is the output of the following code?

List numbers = new ArrayList();
numbers.add(99);
numbers.add(5);
numbers.add(81);
Collections.sort(numbers);
System.out.println(numbers);
A

[5, 81, 99]

107
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(11, 12, 34);
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
System.out.println(shortDateTime.format(dateTime));
System.out.println(shortDateTime.format(date));
System.out.println(shortDateTime.format(time));

A

1/20/20
1/20/20
UnsupportedTemporalTypeException

108
Q

When using Arrays.binarySearch, if no match is found, what happens?

A

it negates the position where the element would need to be inserted and subtracts 1.

109
Q

What is the output of the following code?

DateTimeFormatter f = DateTimeFormatter.ofPattern(“MM dd yyyy”);
LocalDate date = LocalDate.parse(“01 02 2015”, f);
LocalTime time = LocalTime.parse(“11:22”);
System.out.println(date);
System.out.println(time);

A

2015-01-02

11:22

110
Q

What is the package containing the java date and time classes?

A

java.time.*

111
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date.plusDays(10);
System.out.println(date);

A

2020-01-20

112
Q

What is the output of the following code?

DateTimeFormatter f = DateTimeFormatter.ofPattern(“MMMM dd, yyyy, hh:mm”);
System.out.println(dateTime.format(f));

A

January 20, 2020, 11:12

113
Q

Calling equals() on String objects will check what?

A

Whether the sequence of characters is the same.

114
Q

Provide an example of using the LocalTime class to instantiate a time variable:

A

LocalTime time1 = LocalTime.of(6, 15); // hours, minutes
or
LocalTime time2 = LocalTime.of(6, 15, 30); // hours, minutes, seconds
or
LocalTime time3 = LocalTime.of(6, 15, 30, 200); // hours, minutes, seconds, nanoseconds

115
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2014, Month.JANUARY, 20);
System.out.println(date);
date = date.plusDays(2);
System.out.println(date);
date = date.plusWeeks(1);
System.out.println(date);
date = date.plusMonths(1);
System.out.println(date);
date = date.plusYears(5);
System.out.println(date);
A
2014-01-20
2014-01-22
2014-01-29
2014-02-28
2019-02-28
116
Q

Which of the following lines of code will throw an exception?

DateTimeFormatter f = DateTimeFormatter.ofPattern(“hh:mm”);

f. format(dateTime);
f. format(date);
f. format(time);

A

This line:
f.format(date);
The reason is because we’ve constructed this formatter with hour (hh) and minute (mm), thus we can only use this formatter with objects containing times.

117
Q

What method in the StringBuilder class does not modify the value of the object?

A

substring()

118
Q

What is the method implemented by the Java 8 date/time classes to display the current date and time?

A

now()

119
Q

Provide an example of using the LocalDate class to instantiate a date variable:

A

LocalDate date1 = LocalDate.of(2020, Month.JANUARY, 8);
or
LocalDate date2 = LocalDate.of(2020, 1, 8);

120
Q

What is the output of the following code?

LocalDate d = LocalDate.of(2020, Month.JANUARY, 32);

A

DateTimeException

121
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(5, 15);
LocalDateTime dateTime = LocalDateTime.of(date, time).minusDays(1).minusHours(10).minusSeconds(30);
System.out.println(dateTime);

A

2020-01-18T19:14:30

122
Q

Calling == on String objects will check what?

A

Whether they point to the same object in the pool.

123
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date = date.plusMinutes(1);
System.out.println(date);

A

Compiler error on this line:

date = date.plusMinutes(1);

124
Q

What character does java use to separate the date and time when produced by the LocalDateTime class?

A

T

125
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(5, 15);
LocalDateTime dateTime = LocalDateTime.of(date, time);
System.out.println(dateTime);
datetime = dateTime.minusDays(1);
System.out.println(dateTime);
dateTime = dateTime.minusHours(10);
System.out.println(dateTime);
dateTime = dateTime.minusSeconds(30);
System.out.println(dateTime);
A

2020-01-20T05:15
2020-01-19T05:15
2020-01-18T19:15
2020-01-18T19:14:30

126
Q

Calling == on StringBuilder references will check what?

A

Whether they are pointing to the same StringBuilder object.

127
Q

true/false, the LocalDate class provides date, time, and timezone information.

A

false

it only provides date

128
Q

What information is provided by the LocalTime and LocalDateTime classes?

A

LocalTime - provides just time, no date or timezone

LocalDateTime - provides both date and time but no timezone

129
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2015, 1, 20);
LocalTime time = LocalTime.of(6, 15);
LocalDateTime dateTime = LocalDateTime.of(date, time);
Period period = Period.ofMonths(1);
System.out.println(date.plus(period));
System.out.println(dateTime.plus(period));
System.out.println(time.plus(period));

A

2015-02-20
2015-02-20T06:15
UnsupportedTemporalTypeException

130
Q

Arrays.binarySearch() searches a sorted array and returns what?

A

The index of a match.

131
Q

Provide an example of using the LocalDateTime class to instantiate a datetime variable:

A

LocalDateTime datetime1 = LocalDateTime.of(2020, Month.JANUARY, 8, 6, 15, 30);
or
LocalDateTime datetime1 = LocalDateTime.of(date1, time1);

132
Q

true/false, the Local date and time classes are immutable.

A

true

133
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(11, 12, 34);
LocalDateTime dateTime = LocalDateTime.of(date, time);
DateTimeFormatter shortF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
DateTimeFormatter mediumF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(shortF.format(dateTime));
System.out.println(mediumF.format(dateTime));

A

1/20/20 11:12 AM

Jan 20, 2020 11:12:34 AM

134
Q

true/false, the following code compiles:

LocalDate d = new LocalDate();

A

false

135
Q

What is the output of the following code?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
System.out.println(date.getDayOfWeek());
System.out.println(date.getMonth());
System.out.println(date.getYear());
System.out.println(date.getDayOfYear());
A

MONDAY
JANUARY
2020
20

136
Q

Calling equals() on StringBuilder objects will check what?

A

Whether they are pointing to the same object rather than looking at the values inside.

137
Q

Why does the following code fail to compile and what could you do to fix it?

List list = new ArrayList();
list.add(“hawk”);
list.add(“robin”);
String[] myArray = list.toArray();

A

This line fails:
String[] myArray = list.toArray();
list.toArray() returns a type of Object[], and the way to fix it would be to change the line to this:
String[] myArray = (String[]) list.toArray();

138
Q

What is the output of the following code?

StringBuilder sb2 = new StringBuilder("animals");
sb2.insert(8, "-");
System.out.println(sb2);
A

StringIndexOutOfBoundsException

139
Q

What is the output of the following code?

String[] myStrings = {“one”, “two”, “three”};
String[] myStrings2 = {“one”, “two”, “three”};

if (myStrings == myStrings2) {
	System.out.println("is ==");
} else {
	System.out.println("not ==");
}
if (myStrings.equals(myStrings2)) {
	System.out.println("is equals");
} else {
	System.out.println("not equals");
}
A

not ==

not equals

140
Q

What is the output of the following code?

ArrayList list1 = new ArrayList<>();
list1.add(“a”);

ArrayList list2 = new ArrayList<>();
list2.add(“a”);

if (list1 == list2) {
	System.out.println("is ==");
} else {
	System.out.println("not ==");
}
if (list1.equals(list2)) {
	System.out.println("is equals");
} else {
	System.out.println("not equals");
}
A

not ==

is equals

141
Q

Which method do you use to convert an array to a List?

A

Arrays.asList();

Example:
String[] stephen = {“Stephen”, “Spalding”};
List list = Arrays.asList(stephen);

142
Q

Which method do you use to convert a List to an array?

A
list.toArray();
Example:
List list = new ArrayList();
list.add("hawk");
list.add("robin");
Object[] objectArray = list.toArray();
143
Q

Which of the following lines of code compile?

  1. int[][] ints = new int[][];
  2. int[][] ints = new int[1][];
  3. int[][] ints = new int[][1];
  4. int[1][] ints = new int[][];
  5. int[][] ints = new int[1][1];
A

2 and 5

144
Q

What is the result of the following:

int[] random = {6, -4, 12, 0, -10};
int x = 12;
int y = Arrays.binarySearch(random, x);
System.out.println(y);

A

2

145
Q

true/false, an array is mutable.

A

true

146
Q

What is the output of the following code?

LocalDateTime d2 = LocalDateTime.of(2015, 5, 10, 11, 22, 33);
Period p2 = Period.ofDays(1).ofYears(2);
d = d.minus(p2);
DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
System.out.println(f.format(d));

A

5/10/13 11:22 AM

147
Q

What is the output of the following code?

List ages = new ArrayList<>();
ages.add(Integer.parseInt("5"));
ages.add(Integer.valueOf("6"));
ages.add(7);
ages.add(null);
for (int age : ages) System.out.println(age);
A

5
6
7
NullPointerException from this line:
for (int age : ages) System.out.println(age);
The problem has to do with autoboxing the null value when retrieving it from the array.

148
Q

What is the output of the following code?

LocalDateTime d = LocalDateTime.of(2015, 5, 10, 11, 22, 33);
Period p = Period.of(1, 2, 3);
d = d.minus(p);
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
System.out.println(d.format(f));

A

11:22 AM

149
Q

true/false, an ArrayList is mutable.

A

true

150
Q

What is the output of the following code and why?

LocalDateTime d3 = LocalDateTime.now();
Period p3 = Period.ofDays(1).ofYears(2);
d3 = d3.minus(p3);
System.out.println(d3);

A

2018-03-11T13:49:43.703

When chaining methods on the Period class, only the last is recognized. The rest are ignored.

151
Q
What is the output of the following code?
String s = "Hello";
String t = new String(s);
if ("Hello".equals(s)) System.out.println("one");
if (t == s) System.out.println("two");
if (t.equals(s)) System.out.println("three");
if ("Hello" == s) System.out.println("four");
if ("Hello" == t) System.out.println("five");
A

one
three
four

152
Q
What is the output of the following code?
String letters = "abcdef";
System.out.println(letters.length());
System.out.println(letters.charAt(3));
System.out.println(letters.charAt(6));
A

6
d
an exception is thrown

153
Q

What is the output of the following code?
LocalDate date = LocalDate.of(2018, Month.APRIL, 30);
date.plusDays(2);
date.plusYears(3);
System.out.println(date.getYear() + “ “ + date.getMonth() + “ “ + date.getDayOfMonth());

A

2018 APRIL 30

154
Q

What is the output of the following code?
LocalDate date = LocalDate.of(2018, Month.APRIL, 30);
date = date.plusDays(2);
date = date.plusYears(3);
System.out.println(date.getYear() + “ “ + date.getMonth() + “ “ + date.getDayOfMonth());

A

2021 MAY 2