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.

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
Q

What is the difference between isEmpty() and isBlank()?

A

isEmpty() checks for length 0 only, while isBlank() returns true for strings that are either empty or contain only whitespace.

26
Q

What is the result of “ \t\n “.isBlank()?

A

true — it contains only whitespace characters (spaces, tabs, newlines).

27
Q

What is the default format of LocalDateTime.toString() in Java?

A

ISO 8601 format:

"yyyy-MM-dd'T'HH:mm:ss"

Example: “2025-04-10T15:30:00”
28
Q

How do you parse an ISO 8601 date-time string into a LocalDateTime?

A

LocalDateTime dt = LocalDateTime.parse(“2025-04-10T15:30:00”);

29
Q

What is ISO 8601 format used for in Java date-time APIs?

A

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
Q

What is the relationship between CharSequence and String?

A

String implements the CharSequence interface.

31
Q

Why would you use CharSequence instead of String in a method parameter?

A

To allow the method to accept any character sequence type like String, StringBuilder, or StringBuffer.

32
Q

Name three classes that implement the CharSequence interface.

A

String, StringBuilder, and StringBuffer.

33
Q

If String is a subtype of CharSequence, is

List<String>
a subtype of
List<CharSequence>
?
A

No — Java generics are invariant, so

List<String>
is not a subtype of
List<CharSequence>
.
34
Q

What does it mean that Java generics are invariant?

A

It means that

Generic<A>
and
Generic<B>
are not related types, even if A is a subtype of B.
35
Q

Can you override a method returning

List<CharSequence>
with one returning
List<String>
?
A

No — because

List<String>
is not a covariant return of
List<CharSequence>
due to generic invariance.
36
Q

What must be true about the array before calling binarySearch(Object[] a, Object key)?

A

The array must be sorted in ascending order according to the natural ordering of its elements.

37
Q

What happens if the array passed to binarySearch() is not sorted?

A

The results are undefined — there is no guarantee of correct behavior.

38
Q

What does binarySearch() return if the key is not found in the array?

A

It returns (-(insertion point) - 1), where the insertion point is where the key would fit into the array.