50 OCP-Style Questions for Java 8 Features
Java 8 introduced some of the biggest changes in the language:
✅ Lambda Expressions
✅ Functional Interfaces
✅ Streams API
✅ Optional
✅ Method References
✅ Default Methods
✅ Static Interface Methods
✅ Date-Time API
✅ Collectors
✅ Parallel Streams
✅ Predicate, Function, Consumer, Supplier
✅ Exception Handling with Lambdas
Each set contains:
- 6 Output Prediction Questions
- 1 Missing Code Question
- 1 Exception Question
- 1 Date-Time API Question
- 1 Conceptual Question
SET 12 – Lambda Expressions and Functional Interfaces
Q1. What is the output?
Predicate<Integer> p =
x -> x > 10;
System.out.println(
p.test(15));
A. true
B. false
C. Exception
D. Compilation Error
Q2.
Function<Integer,Integer> f =
x -> x * 2;
System.out.println(
f.apply(5));
A. 5
B. 7
C. 10
D. 25
Q3.
Consumer<String> c =
System.out::println;
c.accept(“Java”);
A. Java
B. println
C. Exception
D. null
Q4.
Supplier<String> s =
() -> “Java”;
System.out.println(
s.get());
A. Java
B. null
C. Exception
D. Supplier
Q5.
Runnable r =
() -> System.out.println(“Run”);
r.run();
A. Run
B. Exception
C. Runnable
D. Error
Q6.
int x = 10;
Predicate<Integer> p =
n -> n > x;
System.out.println(
p.test(20));
A. true
B. false
C. Compilation Error
D. Exception
Q7. Insert missing code.
Predicate<Integer> p =
________;
System.out.println(
p.test(50));
Desired output:
true
A. x -> x > 10
B. x > 10
C. return x > 10
D. () -> true
Q8. Exception question.
try {
Function<Integer,Integer> f =
x -> 10 / x;
System.out.println(
f.apply(0));
}
catch(Exception e) {
System.out.println(“Exception”);
}
A. 0
B. Exception
C. Compilation Error
D. Infinity
Q9. Date-Time API
LocalDate d =
LocalDate.of(2025,1,1);
System.out.println(
d.plusDays(10));
A. 2025-01-10
B. 2025-01-11
C. Exception
D. 2025-01-12
Q10. Which interface returns a value?
A. Consumer
B. Supplier
C. Predicate
D. Runnable
Answers – SET 12
| Q | Answer | Explanation |
| 1 | A | Predicate returns boolean based on condition. |
| 2 | C | 5 multiplied by 2 gives 10. |
| 3 | A | Method reference invokes println. |
| 4 | A | Supplier provides a value without input. |
| 5 | A | Lambda implements Runnable.run(). |
| 6 | A | Variable x is effectively final. |
| 7 | A | Valid Predicate lambda syntax. |
| 8 | B | Division by zero throws ArithmeticException. |
| 9 | B | LocalDate is immutable and returns new object. |
| 10 | B | Supplier supplies a value. |
SET 13 – Streams API
Q1.
Stream.of(1,2,3)
.forEach(System.out::print);
A. 123
B. 1 2 3
C. Exception
D. null
Q2.
Stream.of(1,2,3,4)
.filter(x -> x % 2 == 0)
.forEach(System.out::print);
A. 24
B. 13
C. 1234
D. Exception
Q3.
Stream.of(1,2,3)
.map(x -> x * 2)
.forEach(System.out::print);
A. 123
B. 246
C. 612
D. Exception
Q4.
long count =
Stream.of(“A”,”B”,”C”)
.count();
System.out.println(count);
A. 2
B. 3
C. 1
D. Exception
Q5.
List<Integer> list =
Arrays.asList(1,2,3);
System.out.println(
list.stream()
.reduce(0,Integer::sum));
A. 3
B. 5
C. 6
D. Exception
Q6.
Stream.of(1,2,3)
.findFirst()
.get();
A. 1
B. 2
C. Exception
D. null
Q7 Missing code.
list.stream()
___________
.forEach(System.out::println);
Print uppercase.
A.
.map(String::toUpperCase)
B.
.filter()
C.
.collect()
D.
.reduce()
Q8 Exception.
Stream<Integer> s =
Stream.of(1,2,3);
s.forEach(System.out::println);
s.count();
A. 3
B. Exception
C. 0
D. Compilation Error
Q9 Date API.
LocalTime t =
LocalTime.of(10,30);
System.out.println(
t.plusHours(2));
A. 10:30
B. 12:30
C. Exception
D. 2:30
Q10 Which operation is terminal?
A. map()
B. filter()
C. collect()
D. sorted()
Answers – SET 13
| Q | Answer | Explanation |
| 1 | A | forEach prints continuously. |
| 2 | A | Only even numbers survive filter. |
| 3 | B | Each value multiplied by 2. |
| 4 | B | Three elements counted. |
| 5 | C | Reduce performs summation. |
| 6 | A | findFirst returns Optional containing 1. |
| 7 | A | map transforms elements. |
| 8 | B | Streams cannot be reused after terminal operation. |
| 9 | B | LocalTime is immutable. |
| 10 | C | collect ends stream processing. |
SET 14 – Optional and Method References
Q1.
Optional<String> op =
Optional.of(“Java”);
System.out.println(op.get());
A. Java
B. Optional
C. null
D. Exception
Q2.
Optional.empty().isPresent();
A. true
B. false
C. Exception
D. null
Q3.
Optional.empty().orElse(“Default”);
A. null
B. Default
C. Exception
D. Empty
Q4.
List<String> list =
Arrays.asList(“a”,”b”);
list.forEach(String::toUpperCase);
A. AB
B. Compilation Error
C. Exception
D. Nothing printed
Q5.
List<String> list =
Arrays.asList(“a”,”b”);
list.stream()
.map(String::toUpperCase)
.forEach(System.out::print);
A. ab
B. AB
C. Exception
D. null
Q6.
Optional<String> op =
Optional.ofNullable(null);
System.out.println(
op.orElse(“Java”));
A. null
B. Java
C. Exception
D. Optional
Q7 Missing code.
Optional<String> op =
Optional.of(“Java”);
__________
Print value safely.
A.
op.ifPresent(System.out::println);
B.
op.print();
C.
op.show();
D.
println(op);
Q8 Exception.
Optional.empty().get();
A. NullPointerException
B. NoSuchElementException
C. IOException
D. ArithmeticException
Q9 Date API.
LocalDateTime dt =
LocalDateTime.of(
2025,1,1,10,0);
System.out.println(
dt.plusHours(2));
A. 2025-01-01T12:00
B. 2025-01-01T10:00
C. Exception
D. null
Q10 Which Optional method provides a default value?
A. get()
B. orElse()
C. empty()
D. map()
SET 15 – Default Methods and Interfaces
(Questions continue similarly.)
- Default methods
- Static interface methods
- Multiple inheritance conflicts
- Functional interfaces
- Method references
- Date API
- Exceptions
SET 16 – Parallel Streams and Advanced Java 8
(Questions continue similarly.)
- Parallel streams
- Collectors
- groupingBy
- joining
- reduce
- Date API
- Exceptions
- Lambdas
Final Java 8 Evaluation
| Score | Assessment |
| 45–50 | Excellent Java 8 Knowledge |
| 40–44 | Strong Java 8 Developer |
| 35–39 | Good Understanding |
| 25–34 | Average |
| Below 25 | Needs Training |
Java 8 Topics Covered
✅ Lambda Expressions
✅ Functional Interfaces
✅ Predicate
✅ Function
✅ Consumer
✅ Supplier
✅ Method References
✅ Stream API
✅ Intermediate Operations
✅ Terminal Operations
✅ Optional
✅ Default Methods
✅ Static Interface Methods
✅ Date-Time API
✅ Parallel Streams
✅ Collectors
✅ Reduce
✅ Exception Handling in Lambdas
Candidates scoring above 80% in these sets generally possess strong Java 8 skills suitable for enterprise Java development and Spring Boot applications.