June 2025 Flashcards

(43 cards)

1
Q

What does ServiceLoader.load(BloggerService.class).findFirst() return?

A

It returns an Optional<BloggerService>, not a BloggerService directly.</BloggerService>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How should you correctly use ServiceLoader.findFirst() to call a method on the service?

A

By handling the Optional:
bsLoader.findFirst().ifPresent(bs -> bs.blog(“hello”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the conditions for ServiceLoader to successfully discover a provider in a modular Java app?

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the provides directive do in a module-info.java?

A

It registers a service implementation for an interface, making it available for consumption via ServiceLoader.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can a module use a service without providing one?

A

Yes. The uses directive allows a module to consume a service without needing to provide an implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Is it mandatory to declare both uses and provides in module-info.java?

A

No. A module can either use a service (uses) or provide one (provides) independently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the main behavior of Arrays.compare() in Java?

A

It compares two arrays lexicographically, element by element, returning a negative, zero, or positive value based on the first difference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does Arrays.compare(new int[]{1, 2, 3}, new int[]{1, 2, 4}) return and why?

A

It returns -1 because the first differing element (3 vs. 4) makes the first array lexicographically smaller.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does Arrays.compare() handle arrays of different lengths with identical prefixes?

A

The shorter array is considered smaller if all compared elements are equal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are Java primitives passed to methods?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are Java objects passed by reference?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens if you reassign an object reference inside a method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of calling this(…) inside a Java constructor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Where must this(…) appear in a constructor if used?

A

this(…) must be the first statement in the constructor.
If it’s not the first line, the code will not compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can this(…) be used to call a constructor from another class?

A

No. this(…) can only call another constructor within the same class.
To call a constructor from a parent class, use super(…) instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is java.util.function.Supplier<T> in Java?</T>

A

It is a functional interface that takes no input and returns a result of type T. Its single abstract method is T get().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why is Supplier<T> considered a functional interface?</T>

A

Because it has exactly one abstract method (T get()), making it suitable for use with lambda expressions and method references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Give an example of using Supplier<String> with a lambda.</String>

A

Supplier<String> supplier = () -> "Hello, OCP!";
System.out.println(supplier.get()); // Output: Hello, OCP!</String>

19
Q

When do suppressed exceptions occur in Java?

A

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.

20
Q

How can you access suppressed exceptions in Java?

A

Use Throwable.getSuppressed() to retrieve an array of exceptions that were suppressed during resource closure.

21
Q

Will suppressed exceptions cause your program to fail if there’s already a primary exception?

A

No. The primary exception is thrown, and any exceptions during resource closure are added as suppressed exceptions, not thrown separately.

22
Q

What does the write(String s) method in BufferedWriter do?

A

It writes a string to the stream without automatically adding a line break. You must call newLine() manually if a new line is needed.

23
Q

What is the purpose of the newLine() method in BufferedWriter?

A

It writes the platform-specific line separator to the output (e.g., \n on Unix, \r\n on Windows).

24
Q

What are some other write method overloads available in BufferedWriter?

A

write(char[] cbuf), write(char[] cbuf, int off, int len), and write(int c) — these allow writing portions of character arrays or individual characters.

25
Which is a valid module declaration? module { requires java.base; } b) module app { imports java.base; } module app { requires java.base; } d) app module { uses java.base; }
C
26
What is the correct syntax to declare a Java module?
module { requires ; }
27
Why is requires java.base; valid in a module declaration?
Because java.base is a core module, and requires is the correct keyword to declare dependencies on other modules.
28
Which is a valid module declaration? a) module { requires java.base; } b) module app { imports java.base; } c) module app { requires java.base; } d) app module { uses java.base; }
A: ✅ c) module app { requires java.base; }
29
What is the primary purpose of InputStreamReader in Java I/O?
To convert byte streams to character streams using a specified charset (e.g., UTF-8). ✅ (Answer A)
30
Can InputStreamReader wrap a FileInputStream to handle character input?
Yes. InputStreamReader can wrap any InputStream, including FileInputStream, to read character data. ✅ (Answer D)
31
Is it true about InputStreamReader: It does not read binary data directly from a file
false, (that’s InputStream)
32
Why is Reader preferred over InputStream when working with String data?
Because Reader handles character encoding, making it easier to work with text compared to the byte-based InputStream.
33
What key feature does Reader provide that InputStream does not?
Reader offers character stream handling with built-in support for character encoding.
34
How do character stream classes like BufferedReader assist with String processing?
They include convenience methods like readLine() for efficiently reading and processing text data.
35
Does Path.resolve(String other) check if the resolved path actually exists on the file system?
❌ No. resolve() is purely string-based path manipulation. It does not access the file system or validate existence.
36
Will the following code throw an exception if the resulting path doesn’t exist? Path d1 = Paths.get("/work"); Path p = d1.resolve("ocpjp/code/sample");
✅ No exception is thrown. It simply returns a new Path object representing /work/ocpjp/code/sample.
37
Why is resolve() safe to use even if the target path is missing?
Because resolve() just appends paths logically. To check existence, you’d need Files.exists(path) or access the file with I/O operations.
38
What is the unnamed module in Java?
It refers to code on the classpath that is not part of any named module.
39
Which option correctly describes the unnamed module? * A module that uses module-info.java * A legacy JAR not placed on the classpath * Code on the classpath not part of any named module * A deprecated feature in Java 17
Code on the classpath not part of any named module
40
Does the unnamed module require a module-info.java file?
No, the unnamed module does not use module-info.java; it consists of traditional classpath-based code.
41
What does Files.walk(Path start, int maxDepth) return?
It returns a Stream that lazily walks the file tree starting from the given path up to the specified depth.
42
Is the stream returned by Files.walk() recursive?
Yes, it walks the directory structure recursively to the depth specified by maxDepth.
43
How does Files.walk(path, 1) behave?
It lists the contents of the starting directory without descending into subdirectories.