June 2025 Flashcards
(43 cards)
What does ServiceLoader.load(BloggerService.class).findFirst() return?
It returns an Optional<BloggerService>, not a BloggerService directly.</BloggerService>
How should you correctly use ServiceLoader.findFirst() to call a method on the service?
By handling the Optional:
bsLoader.findFirst().ifPresent(bs -> bs.blog(“hello”));
What are the conditions for ServiceLoader to successfully discover a provider in a modular Java app?
- The module declares provides … with … in module-info.java
- The implementation class has a public no-arg constructor
- The consuming module requires the relevant module
- The service interface is accessible (public and exported)
What does the provides directive do in a module-info.java?
It registers a service implementation for an interface, making it available for consumption via ServiceLoader.
Can a module use a service without providing one?
Yes. The uses directive allows a module to consume a service without needing to provide an implementation.
Is it mandatory to declare both uses and provides in module-info.java?
No. A module can either use a service (uses) or provide one (provides) independently.
What is the main behavior of Arrays.compare() in Java?
It compares two arrays lexicographically, element by element, returning a negative, zero, or positive value based on the first difference.
What does Arrays.compare(new int[]{1, 2, 3}, new int[]{1, 2, 4}) return and why?
It returns -1 because the first differing element (3 vs. 4) makes the first array lexicographically smaller.
How does Arrays.compare() handle arrays of different lengths with identical prefixes?
The shorter array is considered smaller if all compared elements are equal.
How are Java primitives passed to methods?
Primitives are always passed by value in Java. A copy of the value is made, so changes to the parameter inside the method do not affect the original variable.
Are Java objects passed by reference?
Not exactly. Java passes object references by value. This means the method gets a copy of the reference, so it can modify the object’s contents but cannot reassign the original reference.
What happens if you reassign an object reference inside a method?
Reassigning an object reference inside a method only changes the local copy of the reference. It does not affect the original reference outside the method.
What is the purpose of calling this(…) inside a Java constructor?
this(…) is used for constructor chaining — it allows one constructor to call another constructor in the same class. This helps avoid code duplication and centralizes initialization logic.
Where must this(…) appear in a constructor if used?
this(…) must be the first statement in the constructor.
If it’s not the first line, the code will not compile.
Can this(…) be used to call a constructor from another class?
No. this(…) can only call another constructor within the same class.
To call a constructor from a parent class, use super(…) instead.
What is java.util.function.Supplier<T> in Java?</T>
It is a functional interface that takes no input and returns a result of type T. Its single abstract method is T get().
Why is Supplier<T> considered a functional interface?</T>
Because it has exactly one abstract method (T get()), making it suitable for use with lambda expressions and method references.
Give an example of using Supplier<String> with a lambda.</String>
Supplier<String> supplier = () -> "Hello, OCP!";
System.out.println(supplier.get()); // Output: Hello, OCP!</String>
When do suppressed exceptions occur in Java?
Suppressed exceptions occur when an exception is thrown in a try block and another exception is thrown while closing a resource in the try-with-resources block.
How can you access suppressed exceptions in Java?
Use Throwable.getSuppressed() to retrieve an array of exceptions that were suppressed during resource closure.
Will suppressed exceptions cause your program to fail if there’s already a primary exception?
No. The primary exception is thrown, and any exceptions during resource closure are added as suppressed exceptions, not thrown separately.
What does the write(String s) method in BufferedWriter do?
It writes a string to the stream without automatically adding a line break. You must call newLine() manually if a new line is needed.
What is the purpose of the newLine() method in BufferedWriter?
It writes the platform-specific line separator to the output (e.g., \n on Unix, \r\n on Windows).
What are some other write method overloads available in BufferedWriter?
write(char[] cbuf), write(char[] cbuf, int off, int len), and write(int c) — these allow writing portions of character arrays or individual characters.