Chapter 3: Core Java APIs Flashcards

1
Q

What operation is performed?

“string” + 10

A

concatenation

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

What is the result of this expression?

10 + 3 + “” + 3 + 5

A

1335

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

Define immutability

A

An object is immutable if it cannot be change after being created

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

Which of the following are immutable?

int
String
Integer

A

String and Integer

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

What strings go in the string pool?

A

String literal values that appear in the program

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

How do you explicitly create a string that doesn’t go in the string pool?

A

new String(“string”)

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

T or F: If s is a String, the following line would convert all characters in s to upper case

s.toUpperCase()

A

False. Strings are immutable. toUpperCase() returns a new string and that reference needs to be stored.

s = s.toUpperCase()

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

String e = “example”;
String s = e.substring(1,4)

What is the value of s?

A

“xam”. index 1 is included (x) and index 4 is excluded (p)

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

String s1 = “test”;
String s2 = “test”;

boolean b = s1 == s2;

What is the value of b?

A

True. Both strings are literals, so they point to the same object in the string pool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
String s1 = "test";
String s2 = new String("test");

boolean b = s1 == s2;

What is the value of b?

A

False. s1 is a literal and is stored in the string pool, s2 is not. Therefore, they are referencing different objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
String s1 = "test";
String s2 = new String("test");

boolean b = s1.equals(s2);

What is the value of b?

A

True. The contents of both strings are the same despite them being different objects.

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

What can you use if you want a mutable representation of a string?

A

StringBuilder

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

Why is StringBuilder more efficient in cases with lots of concatenation than String?

A

StringBuilder doesn’t store interim String values whereas every time String is changed, a new object is created

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

T or F. This is a valid array declaration:

int arr[];

A

True. The brackets can come before or after the name of the array and there can be a space as well.

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

T or F. .length can be used to find how many of an array’s slots are full.

A

False. .length is the total number of spaces allocated to the array

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

T or F. When an array runs out of space, it’s size can be increased?

A

False. Arrays have a fixed size. A new array would need to be created.

17
Q

What happens when binarySearch is run on an unsorted array?

A

The result is unpredictable

18
Q

What happens when a binary search can’t find a value in an array?

A

It returns the negative of the index that value would have if it was in the array

19
Q

What is the correct syntax for varargs?

A

datatype… name

20
Q

T or F. This is a valid definition for a multidimensional array.

int [] arr [];

21
Q

T or F. The following is a valid array initialization.

int[][] arr = new int[][];

A

False. The size of the inner array needs to be specified.

new int[5][]

22
Q

How would you check if two different ArrayList objects are equal?

A

Use the equals method

23
Q

How would you check if two ArrayList reference variables refer to the same object?

A

Use == to compare the reference variables

24
Q

What is the return type of parseInt() ?

25
What is the return type of valueOf("9") ?
Integer
26
T or F. LocalDate is mutable
False
27
What class would you use for an object that stores the date, the time, but not the time zone?
LocalDateTime
28
What method of LocalDate gets the current date?
LocalDate.now()
29
What method of LocalDate allows you to specify the date?
LocalDate.of()
30
What is the Period class used for?
Specifying a time interval
31
When creating a Period, can you chain multiple 'of' calls?
No. The last 'of' call overwrites the previous one.
32
What method is used to create a custom DateTimeFormatter?
DateTimeFormatter.ofPattern()