Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.
Which of the following lambda expressions is INCORRECT?
Answer: C
In option C, you cannot specify types in lambda parameters when using implicit typing. It should be either (x, y) or explicitly define all parameters.
Q.822Medium
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * x). What will the terminal operation need to be?
Answer: C
Both collect() and forEach() are valid terminal operations. collect() returns a list, forEach() performs an action. reduce() is also valid but would sum the squares.
Q.823Medium
What is method reference (::) in Java and how does it relate to lambda expressions?
Answer: A
Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).
Q.824Medium
What is the difference between peek() and forEach() when used with lambda expressions in Streams?
Answer: A
peek() is an intermediate operation used for debugging; it doesn't end the stream. forEach() is a terminal operation that consumes the stream.
Q.825Medium
In the lambda expression (a, b) -> a.compareTo(b), what is the implicit functional interface?
Answer: A
The Comparator<T> functional interface has the method compare(T a, T b) which returns an int, matching this lambda's behavior.
Q.826Medium
What will be the output of: IntStream.range(1, 4).map(x -> x * 2).sum();
Answer: A
range(1, 4) generates [1, 2, 3]. After map(x -> x * 2): [2, 4, 6]. Sum = 2 + 4 + 6 = 12.
Q.827Hard
Consider a lambda that needs to throw a checked exception. Which approach is correct?
Answer: A
Lambda expressions don't propagate checked exceptions. They must be caught within the lambda body using try-catch.
Q.828Hard
What is the time complexity of reducing a stream with a lambda expression using reduce()?
Answer: A
Sequential reduce is O(n). Parallel reduction can be O(log n) with a properly associative and commutative operation, enabling divide-and-conquer approach.
Q.829Hard
Consider: List<String> list = Arrays.asList("a", "b", "c"); list.stream().collect(Collectors.toMap(s -> s, s -> s.length())). What type is returned?
Answer: A
Collectors.toMap with key mapper (s -> s) produces String keys and value mapper (s -> s.length()) produces Integer values, resulting in Map<String, Integer>.
Q.830Hard
What issue can arise when using lambda expressions with flatMap() in nested streams?
Answer: A
Each flatMap creates intermediate streams. Deeply nested flatMap with lambdas can consume significant memory, especially with large datasets.
Q.831Hard
Consider: Function<Integer, Function<Integer, Integer>> curry = x -> y -> x + y; What does curry.apply(5).apply(3) return?
Answer: A
This is a curried function. curry.apply(5) returns a function that adds 5. Applying that with 3 gives 5 + 3 = 8.
Q.832Easy
What is the primary advantage of using lambda expressions in Java 8+?
Answer: A
Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.
Q.833Easy
What will be the output of: List<String> list = Arrays.asList("a", "bb", "ccc"); list.forEach(s -> System.out.print(s.length() + " "));
Answer: A
The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.
Q.834Easy
Which functional interface is used for operations that take two arguments and return a single value?
Answer: B
BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.
Q.835Easy
What is a method reference (::) in Java?
Answer: A
Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.
Q.836Medium
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * 2).forEach(System.out::println); What will be the output?
Answer: A
filter(x -> x > 2) keeps 3, 4, 5. map(x -> x * 2) transforms them to 6, 8, 10. forEach prints each value.
Q.837Medium
What is the difference between peek() and forEach() in streams?
Answer: B
peek() is an intermediate operation that returns a stream for chaining, useful for debugging. forEach() is terminal and returns void, ending the stream chain.
Q.838Medium
What issue can arise when using lambda expressions with checked exceptions?
Answer: B
Since standard functional interfaces don't declare checked exceptions, you must either wrap in try-catch or create a custom functional interface that throws the checked exception.
Q.839Medium
What will be the output of: IntStream.range(1, 4).map(x -> x * x).forEach(System.out::print);
Answer: A
range(1, 4) generates 1, 2, 3. map(x -> x * x) produces 1, 4, 9. Printed without spaces gives '149'.
Q.840Medium
In the lambda expression (a, b) -> a.compareTo(b), what can we infer about the parameters?
Answer: D
Without explicit type declarations or context, the compiler must infer types from usage. We can only determine they have a compareTo() method, likely Comparable types.