Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 212 questions
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
Consider: Function curry = x -> y -> x + y; What does curry.apply(5).apply(3) return?
A 8
B 15
C 5
D 3
Correct Answer:  A. 8
EXPLANATION

This is a curried function. curry.apply(5) returns a function that adds 5. Applying that with 3 gives 5 + 3 = 8.

Take Test
What issue can arise when using lambda expressions with flatMap() in nested streams?
A Excessive memory consumption from creating multiple intermediate streams
B flatMap() cannot accept lambda expressions
C Type erasure causes ClassCastException
D Lambda expressions become unreadable
Correct Answer:  A. Excessive memory consumption from creating multiple intermediate streams
EXPLANATION

Each flatMap creates intermediate streams. Deeply nested flatMap with lambdas can consume significant memory, especially with large datasets.

Take Test
Consider: List list = Arrays.asList("a", "b", "c"); list.stream().collect(Collectors.toMap(s -> s, s -> s.length())). What type is returned?
A Map
B Map
C List
D Set
Correct Answer:  A. Map
EXPLANATION

Collectors.toMap with key mapper (s -> s) produces String keys and value mapper (s -> s.length()) produces Integer values, resulting in Map<String, Integer>.

Take Test
What is the time complexity of reducing a stream with a lambda expression using reduce()?
A O(n) in sequential, potentially O(log n) in parallel with proper associative operation
B Always O(1)
C O(n²) regardless of parallelism
D O(n log n) always
Correct Answer:  A. O(n) in sequential, potentially O(log n) in parallel with proper associative operation
EXPLANATION

Sequential reduce is O(n). Parallel reduction can be O(log n) with a properly associative and commutative operation, enabling divide-and-conquer approach.

Take Test
Advertisement
Consider a lambda that needs to throw a checked exception. Which approach is correct?
A Wrap the checked exception in a try-catch block within the lambda
B Create a custom functional interface with throws clause
C Use @FunctionalInterface with throws declaration
D Lambda expressions cannot throw checked exceptions
Correct Answer:  A. Wrap the checked exception in a try-catch block within the lambda
EXPLANATION

Lambda expressions don't propagate checked exceptions. They must be caught within the lambda body using try-catch.

Take Test
What is the type inference mechanism in lambda expressions?
A Compiler infers parameter types from functional interface and context
B Parameters must always have explicit types
C Type inference only works with single parameter
D Lambda expressions don't support type inference
Correct Answer:  A. Compiler infers parameter types from functional interface and context
EXPLANATION

Compiler uses target type (functional interface) to infer parameter types. If context is clear, explicit types not needed: list.forEach(x -> ...).

Take Test
What will happen if you try to compile this code?
BiFunction concat = (a, b) -> a + b;
concat.apply("Java", "8", "Features");
A It will compile and run successfully
B Runtime error - extra argument
C Compilation error - too many arguments
D No output produced
Correct Answer:  C. Compilation error - too many arguments
EXPLANATION

BiFunction takes exactly 2 parameters. Passing 3 arguments is compilation error. apply() only accepts 2 arguments.

Take Test
What will be the output of this nested lambda?
Function add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
A 8
B 3
C 5
D Compilation error
Correct Answer:  A. 8
EXPLANATION

Curried function: add.apply(3) returns a Function that adds 3. .apply(5) on that adds 5 to 3, returning 8.

Take Test
Q.30 Hard Generics
A method processes collections where items need to be added back to the same collection type. Which wildcard approach ensures type safety for write operations?
A public static void addElements(List
B public static void addElements(List destination, List source)
C public static void addElements(List destination, List source)
D public static void addElements(List destination, List source)
Correct Answer:  A. public static void addElements(List
EXPLANATION

'? super T' allows writing T instances (contravariant), while '? extends T' allows safe reading (covariant). This combination enables flexible yet type-safe collection manipulation.

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