Finals Mock Exam Review
(Questions & Solutions)
2025
©2025
, Question 1:
A developer uses the `volatile` keyword in a multithreaded Java
application. Which of the following properties is guaranteed by declaring
a variable as volatile?
- A. Mutual exclusivity of updates
- B. Immediate visibility of changes among threads
- C. Atomicity of compound operations
- D. Lock-free synchronization for all operations
ANS: B. Immediate visibility of changes among threads
Rationale: Declaring a variable volatile ensures that writes to and reads
from that variable are immediately visible to all threads. It does not
provide atomicity for compound actions, nor does it guarantee mutual
exclusion.
---
Question 2:
Which Java concurrency utility provides a higher-level abstraction over
threads and allows for tasks to be executed asynchronously while
managing a pool of threads?
- A. Thread class
- B. Runnable interface
- C. ExecutorService
- D. TimerTask
ANS: C. ExecutorService
Rationale: The ExecutorService framework abstracts thread
management by providing a pool of threads, allowing tasks to be
executed asynchronously and efficiently managing resources compared
to manually handling Thread objects.
---
©2025
, Question 3:
Which feature introduced in Java 8 allows the creation of inline,
anonymous functions that can be used to implement functional
interfaces?
- A. Generics
- B. Default methods
- C. Lambda expressions
- D. Annotations
ANS: C. Lambda expressions
Rationale: Lambda expressions allow for concise, inline
implementations of single-method (functional) interfaces, thereby
enabling a functional programming style and simplifying code.
---
Question 4:
In Java generics, what concept explains why type parameters do not exist
at runtime?
- A. Type encapsulation
- B. Type aliasing
- C. Type erasure
- D. Type reflection
ANS: C. Type erasure
Rationale: Type erasure is the process by which the Java compiler
removes generic type information during compilation, meaning that
generic types are replaced with their upper bound (often Object), and
runtime type information is not available.
---
Question 5:
Which of the following statements about the Stream API is correct?
©2025
, - A. Terminal operations in streams are lazily evaluated.
- B. Intermediate operations in streams are stateless and lazily
evaluated.
- C. Streams cannot be processed in parallel.
- D. Once a stream has been consumed, it can be reused for additional
operations.
ANS: B. Intermediate operations in streams are stateless and lazily
evaluated.
Rationale: Intermediate operations (such as filter, map, and sorted) are
lazily evaluated, meaning they build a pipeline but are not executed until
a terminal operation is invoked. Terminal operations trigger the
execution of the stream pipeline and cannot be reused thereafter.
---
Question 6:
Consider the following code snippet using a try-with-resources statement
in Java:
```java
try (BufferedReader reader = new BufferedReader(new
FileReader("data.txt"))) {
// Read file
}
```
What is the key benefit of this construct?
- A. It improves file read speeds.
- B. It automatically closes the resource after use.
- C. It avoids the need for exception handling.
- D. It caches the data in memory for faster access.
ANS: B. It automatically closes the resource after use.
Rationale: The try-with-resources statement ensures that any object
that implements AutoCloseable is automatically closed at the end of the
try block, simplifying resource management and preventing resource
©2025