Chapter 4: Core APIs Flashcards

1
Q

LocalDate belongs to the java.time package. True or False?

A

True

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

Which package does DateTimeFormatter belong to?

A

java.time.format

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

What does the put method do in a Java Map implementation?

A

The put method associates a specified key with a specified value in the map, replacing any existing value for that key.

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

What does the put method return on Hashmap?

A

It returns the previous value associated with the key, or null if there was no mapping (or if null was previously mapped to the key).

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

What happens if the key already exists in the Hashmap when calling put?

A

The old value is replaced with the new value, and the method returns the previous value.

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

What is the role of putVal(hash(key), key, value, false, true) in the put method in Hashmap?

A

It is an internal method that handles the actual insertion logic, including hashing, bucket assignment, and collision resolution.

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

Can put return null even if the key was previously mapped for hashmap?

A

Yes, if the previous mapping was explicitly set to null, the method will return null.

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

Can a HashMap contain null keys and values?

A

Yes, HashMap allows one null key and multiple null values.

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

Does Hashtable allow null keys or values?

A

No, Hashtable does not allow null keys or null values.

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

How do you declare and initialize a String array in Java?

A

String[] arr = {“Java”, “17”, “Arrays”};
or
String[] arr = new String[]{“Java”, “17”, “Arrays”};

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

Q: What is the default value of elements in a String[] array when using new String[5]?
Example: String[] arr = new String[5];

A

All elements are initialized to null.

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

What is Function<’T, R’>?

A

✔ Function<T, R> is a functional interface in java.util.function.
✔ Represents a function that takes one argument of type T and returns a result of type R.

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

Why is @FunctionalInterface used?

A

✔ Ensures the interface has exactly one abstract method (apply(T t)).
✔ Allows usage in lambda expressions and method references.

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

How can Function<T, R> be composed?

A

✔ Supports function composition using andThen() and compose().

Function<Integer, Integer> multiplyBy2 = x -> x * 2;
Function<Integer, Integer> add3 = x -> x + 3;

Function<Integer, Integer> combined = multiplyBy2.andThen(add3);
System.out.println(combined.apply(5)); // (5 * 2) + 3 = 13

📌 andThen(f) applies this first, then f.
📌 compose(f) applies f first, then this.

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

What does the java.time.Instant class represent in Java?

A

Instant represents a specific moment on the time-line in UTC, with nanosecond precision. It is the closest equivalent to java.util.Date in the java.time API.

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

Is the Instant class mutable, and why?

A

No, Instant is immutable. This design ensures thread safety and avoids bugs from unintended modifications, making it safe for use in concurrent applications.

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

How do you get the current time and add time to an Instant?

A

Use Instant.now() to get the current moment. Use methods like plusSeconds(), minusMillis(), etc., to return new Instant objects with adjusted times.

18
Q

What does the strip() method do in the String class?

A

It removes leading and trailing whitespace using Unicode-aware rules.

19
Q

How is strip() different from trim() in Java?

A

strip() uses Unicode whitespace rules, while trim() only removes characters ≤ U+0020 (ASCII whitespace).

20
Q

What is the output of “ \u2003Hello\u2003 “.strip()? (\u2003 = em space)

A

“Hello” — strip() removes Unicode spaces like \u2003.

21
Q

What does the isEmpty() method check in a String?

A

It returns true if the string has length 0, otherwise false.

22
Q

What is the result of “ “.isEmpty()?

A

false — because it contains a space character and length is not 0.

23
Q

How can you check if a string is empty or only contains whitespace?

A

Use s.strip().isEmpty() — this strips whitespace and then checks for emptiness.

24
Q

What does the isBlank() method return?

A

It returns true if the string is empty or contains only whitespace characters (including Unicode whitespace).

25
What is the difference between isEmpty() and isBlank()?
isEmpty() checks for length 0 only, while isBlank() returns true for strings that are either empty or contain only whitespace.
26
What is the result of " \t\n ".isBlank()?
true — it contains only whitespace characters (spaces, tabs, newlines).
27
What is the default format of LocalDateTime.toString() in Java?
ISO 8601 format: ```"yyyy-MM-dd'T'HH:mm:ss"``` Example: "2025-04-10T15:30:00"
28
How do you parse an ISO 8601 date-time string into a LocalDateTime?
LocalDateTime dt = LocalDateTime.parse("2025-04-10T15:30:00");
29
What is ISO 8601 format used for in Java date-time APIs?
It’s the standard format for representing date and time in a machine-readable way, used by default in most of the Java Time API (LocalDate, LocalDateTime, Instant, etc.).
30
What is the relationship between CharSequence and String?
String implements the CharSequence interface.
31
Why would you use CharSequence instead of String in a method parameter?
To allow the method to accept any character sequence type like String, StringBuilder, or StringBuffer.
32
Name three classes that implement the CharSequence interface.
String, StringBuilder, and StringBuffer.
33
If String is a subtype of CharSequence, is ```List``` a subtype of ```List```?
No — Java generics are invariant, so ```List``` is not a subtype of ```List```.
34
What does it mean that Java generics are invariant?
35
Can you override a method returning ```List``` with one returning ```List```?
No — because ```List``` is not a covariant return of ```List``` due to generic invariance.
36
What must be true about the array before calling binarySearch(Object[] a, Object key)?
The array must be sorted in ascending order according to the natural ordering of its elements.
37
What happens if the array passed to binarySearch() is not sorted?
The results are undefined — there is no guarantee of correct behavior.
38
What does binarySearch() return if the key is not found in the array?
It returns (-(insertion point) - 1), where the insertion point is where the key would fit into the array.
39
In System.out.println(a[(a = b)[3]]);, which version of a is used to access the array?
The original a is used because the array reference is evaluated before the assignment (a = b) happens.
40
In compound expressions like a[(a = b)[i]], when does the assignment a = b take effect?
The assignment happens during index evaluation, but after the array reference a[...] is already resolved.
41
How can reversing the assignment change the behavior? (e.g. (a = b)[a[3]])
Now a = b happens before the index access, so both the array and the index come from b, not the original a.
42
Why does a text block like ```s3 = """Hello World """``` compare equal to a string like s4 = "Hello World\n"?
Because when a text block ends with the closing """ on its own line, Java automatically inserts a newline (\n) at the end of the string. So even though \n isn’t typed explicitly, it’s part of the string content.