Java Programming
Java OOP, collections, multithreading
476 Questions 10 Topics Take Test
Advertisement
Showing 61–70 of 476 questions
Q.61 Medium Lambda Expressions
What is the difference between peek() and forEach() in streams?
A peek() is terminal, forEach() is intermediate
B peek() is intermediate and returns a stream, forEach() is terminal and returns void
C There is no difference, they are aliases
D peek() modifies elements, forEach() doesn't
Correct Answer:  B. peek() is intermediate and returns a stream, forEach() is terminal and returns void
EXPLANATION

peek() is an intermediate operation that returns a stream for chaining, useful for debugging. forEach() is terminal and returns void, ending the stream chain.

Take Test
Q.62 Medium Lambda Expressions
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?
A 6 8 10
B 2 4 6 8 10
C 3 4 5
D 1 2 3 4 5
Correct Answer:  A. 6 8 10
EXPLANATION

filter(x -> x > 2) keeps 3, 4, 5. map(x -> x * 2) transforms them to 6, 8, 10. forEach prints each value.

Take Test
Q.63 Medium Lambda Expressions
What will be the output of: IntStream.range(1, 4).map(x -> x * 2).sum();
A 12
B 9
C 6
D 24
Correct Answer:  A. 12
EXPLANATION

range(1, 4) generates [1, 2, 3]. After map(x -> x * 2): [2, 4, 6]. Sum = 2 + 4 + 6 = 12.

Take Test
Q.64 Medium Lambda Expressions
In the lambda expression (a, b) -> a.compareTo(b), what is the implicit functional interface?
A Comparator
B BiFunction
C BiPredicate
D BinaryOperator
Correct Answer:  A. Comparator
EXPLANATION

The Comparator<T> functional interface has the method compare(T a, T b) which returns an int, matching this lambda's behavior.

Take Test
Q.65 Medium Lambda Expressions
What is the difference between peek() and forEach() when used with lambda expressions in Streams?
A peek() is intermediate and doesn't consume the stream, forEach() is terminal and consumes it
B forEach() is faster than peek()
C peek() can only be used with Collectors
D forEach() returns a value while peek() returns void
Correct Answer:  A. peek() is intermediate and doesn't consume the stream, forEach() is terminal and consumes it
EXPLANATION

peek() is an intermediate operation used for debugging; it doesn't end the stream. forEach() is a terminal operation that consumes the stream.

Take Test
Advertisement
Q.66 Medium Lambda Expressions
What is method reference (::) in Java and how does it relate to lambda expressions?
A Method reference is a shorthand for lambda expressions that call a single method
B Method reference is used for declaring new methods
C Method reference is faster than lambda but less readable
D Method reference can only be used with static methods
Correct Answer:  A. Method reference is a shorthand for lambda expressions that call a single method
EXPLANATION

Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).

Take Test
Q.67 Medium Lambda Expressions
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * x). What will the terminal operation need to be?
A collect(Collectors.toList())
B forEach(System.out::println)
C Either a or b
D reduce(0, Integer::sum)
Correct Answer:  C. Either a or b
EXPLANATION

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.

Take Test
Q.68 Medium Lambda Expressions
Which of the following lambda expressions is INCORRECT?
A () -> 42
B x -> x + 1
C (int x, int y) -> x + y
D (x, y) -> (x > y) ? x : y
Correct Answer:  C. (int x, int y) -> x + y
EXPLANATION

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.

Take Test
Q.69 Medium Lambda Expressions
What is the purpose of the Optional class when used with lambda expressions in Stream operations?
A To handle null values and avoid NullPointerException
B To increase stream processing speed
C To convert streams to lists automatically
D To sort elements in reverse order
Correct Answer:  A. To handle null values and avoid NullPointerException
EXPLANATION

Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.

Take Test
Q.70 Medium Lambda Expressions
Consider: Function f = x -> x * x; What will f.apply(5) return?
A 25
B 10
C 5
D 30
Correct Answer:  A. 25
EXPLANATION

Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips