Java Concurrency | CompletableFuture | ExecutorService | SQL | Programming
Duration: 20 Minutes
Experience: 4–8 Years
This assessment evaluates practical Java concurrency concepts used in backend applications and microservices.
Section A – Java OCP Style MCQs (5 Questions)
Question 1 – Runnable vs Callable
What is the output?
import java.util.concurrent.*;
public class Test {
public static void main(String[] args) throws Exception {
ExecutorService executor =
Executors.newSingleThreadExecutor();
Future<Integer> future =
executor.submit(() -> 10);
System.out.println(future.get());
executor.shutdown();
}
}
A. 10
B. null
C. Compilation Error
D. ExecutionException
Question 2 – CompletableFuture
import java.util.concurrent.*;
public class Test {
public static void main(String[] args) throws Exception {
CompletableFuture<String> future =
CompletableFuture
.completedFuture("Java")
.thenApply(s -> s + " 21");
System.out.println(future.get());
}
}
Output?
A.
Java
B.
Java21
C.
Java 21
D.
Compilation Error
Question 3 – ExecutorService
ExecutorService executor =
Executors.newFixedThreadPool(2);
executor.submit(() ->
System.out.print("Hello"));
Which statement is TRUE?
A. submit() accepts only Callable
B. submit() accepts both Runnable and Callable
C. submit() returns void
D. submit() always creates a new thread
Question 4 – ConcurrentHashMap
Map<String,Integer> map =
new ConcurrentHashMap<>();
map.put("A",1);
map.put("A",2);
System.out.println(map.size());
System.out.println(map.get("A"));
Output?
A.
2
2
B.
1
2
C.
1
1
D.
Compilation Error
Question 5 – Synchronization
Which keyword is used to ensure that only one thread executes a critical section at a time?
A. volatile
B. synchronized
C. transient
D. final
Section B – SQL Challenge
Question 6
Tables
CUSTOMER
| ID | NAME |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
ORDERS
| ORDER_ID | CUSTOMER_ID |
|---|---|
| 101 | 1 |
| 102 | 1 |
| 103 | 2 |
Query
SELECT C.NAME,
COUNT(O.ORDER_ID)
FROM CUSTOMER C
LEFT JOIN ORDERS O
ON C.ID = O.CUSTOMER_ID
GROUP BY C.NAME;
Which statement is correct?
A. Charlie is excluded
B. Charlie appears with count 0
C. Query throws an error
D. Only customers with orders are returned
Section C – Programming Questions
Question 7 – Find Duplicate Numbers
Given
List<Integer> list =
Arrays.asList(1,2,3,2,4,5,1,6,5);
Write a Java program to print only the duplicate numbers.
Expected Output
1
2
5
Bonus:
Solve using Java 8 Streams.
Question 8 – Employee Grouping
Given
class Employee{
String department;
int salary;
}
Using Java 8 Streams, print the average salary of each department.
Expected Output
IT -> 65000
HR -> 55000
Finance -> 70000
Question 9 – Second Largest Number
Given
List<Integer> list =
Arrays.asList(5,8,2,9,4,9,7);
Write a Java program to find the second largest distinct number.
Expected Output
8
Bonus:
Solve using Java 8 Streams.
Answer Key & Detailed Explanations
Question 1
Correct Answer: A
Explanation
submit() accepts a lambda returning an Integer, so it is treated as a Callable<Integer>.
Future.get() blocks until the computation completes.
Output
10
Interview Follow-up
- Difference between
RunnableandCallable. - Why does
Callablereturn a value?
Question 2
Correct Answer: C
Explanation
completedFuture("Java")
creates an already completed future.
thenApply()
transforms the result.
Java
↓
Java 21
Output
Java 21
Interview Follow-up
- Difference between
thenApply()andthenCompose(). - Which one is similar to
map()and which one toflatMap()?
Question 3
Correct Answer: B
Explanation
ExecutorService.submit() has overloaded methods.
It accepts both:
RunnableCallable<T>
The returned Future can be used to retrieve results or wait for completion.
Question 4
Correct Answer: B
Explanation
Like HashMap, ConcurrentHashMap does not allow duplicate keys.
The second put() replaces the value.
Size = 1
Value = 2
The difference between HashMap and ConcurrentHashMap is thread safety, not duplicate-key behavior.
Question 5
Correct Answer: B
Explanation
synchronized ensures that only one thread at a time can execute the protected code block or method using the same monitor.
Interview Follow-up
- Difference between
synchronizedandvolatile. - Can
volatilereplacesynchronized?
(Expected answer: No. volatile provides visibility but not mutual exclusion.)
Question 6
Correct Answer: B
Explanation
A LEFT JOIN returns every row from the left table.
Charlie has no matching orders, so the count is 0.
Result
Alice 2
Bob 1
Charlie 0
Question 7 – Sample Solution
Traditional
Set<Integer> seen = new HashSet<>();
Set<Integer> duplicates = new LinkedHashSet<>();
for(Integer n : list){
if(!seen.add(n)){
duplicates.add(n);
}
}
duplicates.forEach(System.out::println);
Java 8
Set<Integer> seen = new HashSet<>();
list.stream()
.filter(n -> !seen.add(n))
.distinct()
.forEach(System.out::println);
Complexity
Time: O(n)
Space: O(n)
Question 8 – Sample Solution
employees.stream()
.collect(Collectors.groupingBy(
Employee::getDepartment,
Collectors.averagingInt(
Employee::getSalary)))
.forEach((k,v)->
System.out.println(k+" -> "+v));
Concepts Tested
groupingBy()averagingInt()- Method References
- Collectors
Question 9 – Sample Solution
Traditional
int result =
list.stream()
.distinct()
.sorted(Comparator.reverseOrder())
.skip(1)
.findFirst()
.get();
System.out.println(result);
Output
8
Complexity
Time: O(n log n)
Space: O(n)
Alternative discussion point: A single-pass solution can achieve O(n) time by tracking the largest and second-largest distinct values without sorting.
Evaluation Guide
| Score | Recommendation |
|---|---|
| 9/9 | Excellent – Strong Backend Java Developer |
| 7–8 | Good – Recommended for Technical Round |
| 5–6 | Average – Requires Further Evaluation |
| Below 5 | Not Recommended |
Interviewer’s Notes
This set focuses on practical concurrency concepts that backend developers use daily.
A strong candidate should be able to explain:
- Why
Callableis preferred when a task returns a value. - The difference between
Runnable,Callable,Future, andCompletableFuture. - Why
ConcurrentHashMapis thread-safe and when to use it. - The difference between
synchronizedandvolatile. - How
LEFT JOINbehaves when there are no matching rows. - Time and space complexity of each programming solution.
- Alternative implementations with better complexity where applicable.
Candidates who can confidently answer the follow-up questions are generally well-prepared for Spring Boot and microservices development, where asynchronous programming and concurrent processing are common.