Unit 1: Collections, Streams, Filters, and Lambdas - Subjective Questions

CSE406 — Advanced Java Programming • Practice Questions with Detailed Answers

20 questions

1

Define the Builder design pattern. Explain its structure, advantages, and use in Java with a suitable example.

2

Explain how a Java collection can be iterated using lambda syntax. Compare it with the traditional enhanced for loop.

3

Describe the Java Stream interface. How does a stream differ from a collection?

4

How can a collection be filtered using lambda expressions? Explain the role of the Predicate interface with an example.

5

Explain method chaining in Java stream processing. Illustrate how multiple stream methods are chained together.

6

Define a stream pipeline in terms of lambdas and collections. Explain its stages with a suitable example.

7

Describe how to make a stream pipeline execute in parallel. Discuss the benefits, limitations, and safety considerations of parallel streams.

8

Define reduction in Java streams. Distinguish between mutable and immutable reduction.

9

Explain the overloaded forms of the reduce method. Write a stream expression to calculate the sum and product of a list of integers.

10

Describe the process of decomposing and merging work in a parallel stream. Explain the roles of the identity, accumulator, and combiner.

11

Explain how the map operation extracts or transforms data from an object. Differentiate map from flatMap.

12

Describe the different types of stream operations. Distinguish between intermediate, terminal, stateless, stateful, short-circuiting, and non-short-circuiting operations.

13

Describe the Java Optional class. Explain how it helps avoid null-related errors and discuss its important methods.

14

What is lazy processing in Java streams? Explain how laziness and short-circuiting can improve efficiency.

15

Explain how to sort a stream in natural order and custom order. How can objects be sorted using Comparator?

16

Explain how stream results are saved into collections using the collect method. Describe the supplier, accumulator, and combiner used in mutable reduction.

17

Explain how data is grouped using Collectors.groupingBy. Demonstrate simple grouping and downstream aggregation with examples.

18

What is partitioning in the Java Stream API? Compare Collectors.partitioningBy with Collectors.groupingBy using examples.

19

Write a Java program using lambda expressions to perform arithmetic operations. Explain the functional interface and lambda expressions used.

20

Develop a stream-based solution that filters employees, extracts data, sorts results, performs reduction, and groups employees. Explain the complete pipeline and mention how it can be parallelized safely.