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.
What does the strip() method do in the String class?
It removes leading and trailing whitespace using Unicode-aware rules.
How is strip() different from trim() in Java?
strip() uses Unicode whitespace rules, while trim() only removes characters ≤ U+0020 (ASCII whitespace).
What is the output of “ \u2003Hello\u2003 “.strip()? (\u2003 = em space)
“Hello” — strip() removes Unicode spaces like \u2003.
What does the isEmpty() method check in a String?
It returns true if the string has length 0, otherwise false.
What is the result of “ “.isEmpty()?
false — because it contains a space character and length is not 0.
How can you check if a string is empty or only contains whitespace?
Use s.strip().isEmpty() — this strips whitespace and then checks for emptiness.
What does the isBlank() method return?
It returns true if the string is empty or contains only whitespace characters (including Unicode whitespace).
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.
What is the result of “ \t\n “.isBlank()?
true — it contains only whitespace characters (spaces, tabs, newlines).
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”
How do you parse an ISO 8601 date-time string into a LocalDateTime?
LocalDateTime dt = LocalDateTime.parse(“2025-04-10T15:30:00”);
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.).
What is the relationship between CharSequence and String?
String implements the CharSequence interface.
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.
Name three classes that implement the CharSequence interface.
String, StringBuilder, and StringBuffer.
If String is a subtype of CharSequence, is
List<String>a subtype of
List<CharSequence>?
No — Java generics are invariant, so
List<String>is not a subtype of
List<CharSequence>.
What does it mean that Java generics are invariant?
It means that
Generic<A>and
Generic<B>are not related types, even if A is a subtype of B.
Can you override a method returning
List<CharSequence>with one returning
List<String>?
No — because
List<String>is not a covariant return of
List<CharSequence>due to generic invariance.
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.
What happens if the array passed to binarySearch() is not sorted?
The results are undefined — there is no guarantee of correct behavior.
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.