Java 8 Advanced | Optional | Streams | HashMap | Comparable | SQL | Programming
Duration: 20 Minutes
Experience: 3–8 Years
This assessment focuses on advanced Java 8 concepts commonly asked in backend interviews and OCP Java 8 Programmer certification.
Skills Tested:
- Optional
- map() vs flatMap()
- Comparable vs Comparator
- HashMap behavior
- Method References
- Stream Operations
- SQL Joins & Aggregation
- Programming Logic
Section A – Java OCP Style MCQs (5 Questions)
Question 1 – Optional
What is the output?
import java.util.Optional;
public class Test {
public static void main(String[] args) {
Optional<String> value =
Optional.ofNullable(null);
System.out.println(
value.orElse("Java"));
}
}
A. null
B. Java
C. Optional.empty
D. NullPointerException
Question 2 – Stream map()
import java.util.*;
public class Test {
public static void main(String[] args) {
List<String> list =
Arrays.asList("java","spring");
list.stream()
.map(String::toUpperCase)
.forEach(System.out::print);
}
}
Output?
A.
java spring
B.
JAVASPRING
C.
JAVA SPRING
D. Compilation Error
Question 3 – Comparable vs Comparator
import java.util.*;
class Employee {
int id;
Employee(int id){
this.id=id;
}
public String toString(){
return String.valueOf(id);
}
}
public class Test {
public static void main(String[] args){
List<Employee> list=
Arrays.asList(
new Employee(3),
new Employee(1),
new Employee(2));
Collections.sort(list);
System.out.println(list);
}
}
What happens?
A. [1,2,3]
B. [3,1,2]
C. Compilation Error
D. Runtime Exception
Question 4 – HashMap
Map<String,Integer> map =
new HashMap<>();
map.put("Java",1);
map.put("Java",2);
System.out.println(map.size());
System.out.println(map.get("Java"));
Output?
A.
2
2
B.
1
2
C.
2
1
D.
Compilation Error
Question 5 – Method Reference
import java.util.*;
public class Test {
public static void main(String[] args){
Arrays.asList("A","B","C")
.forEach(System.out::print);
}
}
Output?
A.
ABC
B.
A B C
C.
[A,B,C]
D.
Compilation Error
Section B – SQL Challenge
Question 6
Tables
EMPLOYEE
| EMP_ID | NAME | DEPT_ID |
|---|---|---|
| 1 | John | 10 |
| 2 | Mary | 20 |
| 3 | Steve | 10 |
| 4 | David | 30 |
DEPARTMENT
| DEPT_ID | NAME |
|---|---|
| 10 | IT |
| 20 | HR |
| 30 | Finance |
Query
SELECT D.NAME,
COUNT(E.EMP_ID)
FROM DEPARTMENT D
LEFT JOIN EMPLOYEE E
ON D.DEPT_ID=E.DEPT_ID
GROUP BY D.NAME;
Which statement is correct?
A. INNER JOIN output
B. Departments without employees are excluded
C. Every department appears at least once
D. Query throws an error
Section C – Programming
Question 7 – First Duplicate Character
Write a Java program to find the first duplicate character.
Example
programming
Output
r
Bonus
Solve using Java Streams.
Question 8 – Group Strings
Given
List<String> list =
Arrays.asList(
"Java",
"Spring",
"AWS",
"Angular",
"AI");
Using Java 8,
Group strings by their length.
Expected Output
2 -> [AI]
3 -> [AWS]
4 -> [Java]
6 -> [Spring]
7 -> [Angular]
Question 9 – Programming Logic
Write a Java program to determine whether a String is a palindrome without using reverse().
Example
madam
Output
true
Answer Key & Explanations
Question 1
Answer: B
Optional.ofNullable(null) creates an empty Optional.
orElse("Java") supplies the default value.
Output
Java
Question 2
Answer: B
map() transforms each element.
java
↓
JAVA
spring
↓
SPRING
forEach(System.out::print) prints without spaces.
Output
JAVASPRING
Question 3
Answer: D
Collections.sort() requires elements to implement Comparable unless a Comparator is supplied.
The code compiles because the raw sort method exists, but at runtime a ClassCastException is thrown when Java attempts to compare Employee objects.
Interview Follow-up:
- How would you fix it?
- Which approach is preferable:
ComparableorComparator?
Question 4
Answer: B
A HashMap cannot contain duplicate keys.
The second put() replaces the previous value.
Key Value
Java 2
Size remains 1.
Question 5
Answer: A
System.out::print is a method reference equivalent to
s -> System.out.print(s)
Output
ABC
Question 6
Answer: C
LEFT JOIN guarantees that every row from the left table (DEPARTMENT) appears.
Even if a department has zero employees, it will still be returned with a count of 0.
Concepts tested:
- LEFT JOIN
- GROUP BY
- Aggregate Functions
Question 7 – Sample Solution
Traditional
Set<Character> set = new HashSet<>();
for(char c : input.toCharArray()){
if(!set.add(c)){
System.out.println(c);
break;
}
}
Complexity
Time
O(n)
Space
O(n)
Question 8 – Sample Solution
list.stream()
.collect(Collectors.groupingBy(String::length))
.forEach((k,v)->
System.out.println(k+" -> "+v));
Concepts
- groupingBy
- Method Reference
- Collectors
Question 9 – Sample Solution
boolean palindrome=true;
for(int i=0,j=input.length()-1;
i<j;
i++,j--){
if(input.charAt(i)!=
input.charAt(j)){
palindrome=false;
break;
}
}
Complexity
Time
O(n)
Space
O(1)
Evaluation Guide
| Score | Recommendation |
|---|---|
| 9/9 | Excellent |
| 7–8 | Strong Java 8 Developer |
| 5–6 | Average |
| Below 5 | Needs Improvement |
Interviewer’s Notes
This set is designed to identify candidates who understand how Java behaves internally, rather than simply remembering APIs.
Look for candidates who can explain:
- Why
OptionalavoidsNullPointerException. - The difference between
map()andflatMap()(even though onlymap()is used here). - Why
Collections.sort()requiresComparableor aComparator. - Why
HashMap.put()replaces values for duplicate keys. - Why a
LEFT JOINreturns all rows from the left table. - Time and space complexity of their programming solutions.