Java Backend Screening Test – Set 3

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_IDNAMEDEPT_ID
1John10
2Mary20
3Steve10
4David30

DEPARTMENT

DEPT_IDNAME
10IT
20HR
30Finance

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: Comparable or Comparator?

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

ScoreRecommendation
9/9Excellent
7–8Strong Java 8 Developer
5–6Average
Below 5Needs 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 Optional avoids NullPointerException.
  • The difference between map() and flatMap() (even though only map() is used here).
  • Why Collections.sort() requires Comparable or a Comparator.
  • Why HashMap.put() replaces values for duplicate keys.
  • Why a LEFT JOIN returns all rows from the left table.
  • Time and space complexity of their programming solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *