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.