Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 111–120 of 958
Topics in Java Programming
What is the time complexity of reducing a stream with a stateful lambda operation?
A O(n) for sequential, O(log n) for parallel
B O(n) for both sequential and parallel streams
C O(1) regardless of stream size
D Depends on the operation and stream characteristics
Correct Answer:  D. Depends on the operation and stream characteristics
EXPLANATION

Time complexity depends on the specific reduction operation, whether the lambda is stateless, and stream characteristics. Generally O(n), but parallelization overhead affects actual performance.

Take Test
Q.112 Medium Lambda Expressions
In the lambda expression (a, b) -> a.compareTo(b), what can we infer about the parameters?
A Both a and b are integers
B Both a and b are Comparable objects
C a is a String and b is a number
D Types cannot be determined without context
Correct Answer:  D. Types cannot be determined without context
EXPLANATION

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.

Take Test
Q.113 Medium Lambda Expressions
What will be the output of: IntStream.range(1, 4).map(x -> x * x).forEach(System.out::print);
A 149
B 123
C 111
D 000
Correct Answer:  A. 149
EXPLANATION

range(1, 4) generates 1, 2, 3. map(x -> x * x) produces 1, 4, 9. Printed without spaces gives '149'.

Take Test
Q.114 Medium Lambda Expressions
What issue can arise when using lambda expressions with checked exceptions?
A Checked exceptions cannot be thrown directly in lambda expressions
B They must be wrapped in try-catch or a custom functional interface must be created
C Checked exceptions automatically become unchecked in lambdas
D Lambda expressions cannot handle any exceptions
Correct Answer:  B. They must be wrapped in try-catch or a custom functional interface must be created
EXPLANATION

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.

Take Test
Q.115 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.116 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
What is a method reference (::) in Java?
A A shorthand syntax for lambda expressions that call existing methods
B An operator for accessing private members
C A way to create anonymous classes
D A reference to a method's memory address
Correct Answer:  A. A shorthand syntax for lambda expressions that call existing methods
EXPLANATION

Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.

Take Test
Which functional interface is used for operations that take two arguments and return a single value?
A Function
B BiFunction
C Consumer
D Supplier
Correct Answer:  B. BiFunction
EXPLANATION

BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.

Take Test
What will be the output of: List list = Arrays.asList("a", "bb", "ccc"); list.forEach(s -> System.out.print(s.length() + " "));
A 1 2 3
B a bb ccc
C 3 2 1
D 1 1 1
Correct Answer:  A. 1 2 3
EXPLANATION

The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.

Take Test
What is the primary advantage of using lambda expressions in Java 8+?
A They reduce boilerplate code and enable functional programming style
B They increase execution speed significantly
C They eliminate the need for classes
D They are required for all stream operations
Correct Answer:  A. They reduce boilerplate code and enable functional programming style
EXPLANATION

Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.

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