Chapter 4: Core APIs Flashcards
LocalDate belongs to the java.time package. True or False?
True
Which package does DateTimeFormatter belong to?
java.time.format
What does the put method do in a Java Map implementation?
The put method associates a specified key with a specified value in the map, replacing any existing value for that key.
What does the put method return on Hashmap?
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).
What happens if the key already exists in the Hashmap when calling put?
The old value is replaced with the new value, and the method returns the previous value.
What is the role of putVal(hash(key), key, value, false, true) in the put method in Hashmap?
It is an internal method that handles the actual insertion logic, including hashing, bucket assignment, and collision resolution.
Can put return null even if the key was previously mapped for hashmap?
Yes, if the previous mapping was explicitly set to null, the method will return null.
Can a HashMap contain null keys and values?
Yes, HashMap allows one null key and multiple null values.
Does Hashtable allow null keys or values?
No, Hashtable does not allow null keys or null values.
How do you declare and initialize a String array in Java?
String[] arr = {“Java”, “17”, “Arrays”};
or
String[] arr = new String[]{“Java”, “17”, “Arrays”};
Q: What is the default value of elements in a String[] array when using new String[5]?
Example: String[] arr = new String[5];
All elements are initialized to null.
What is Function<’T, R’>?
✔ 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.
Why is @FunctionalInterface used?
✔ Ensures the interface has exactly one abstract method (apply(T t)).
✔ Allows usage in lambda expressions and method references.
How can Function<T, R> be composed?
✔ 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.
What does the java.time.Instant class represent in Java?
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.
Is the Instant class mutable, and why?
No, Instant is immutable. This design ensures thread safety and avoids bugs from unintended modifications, making it safe for use in concurrent applications.
How do you get the current time and add time to an Instant?
Use Instant.now() to get the current moment. Use methods like plusSeconds(), minusMillis(), etc., to return new Instant objects with adjusted times.