Which of the following correctly uses method reference with constructor?
AString::new
Bnew String::
CString->new
Dnew::String
Correct Answer:
A. String::new
EXPLANATION
Constructor method references use ClassName::new syntax. String::new refers to the String constructor and can be used wherever a Supplier<String> is expected.
Consider: Function curried = a -> b -> a + b; What is this pattern called?
AHigher-order function with currying
BNested lambda expression
CFunction composition
DStream reduction
Correct Answer:
A. Higher-order function with currying
EXPLANATION
This is a curried function - a higher-order function that takes one argument and returns another function taking the next argument. It transforms multi-argument functions into sequences of single-argument functions.
What is the time complexity of reducing a stream with a stateful lambda operation?
AO(n) for sequential, O(log n) for parallel
BO(n) for both sequential and parallel streams
CO(1) regardless of stream size
DDepends 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.
In the lambda expression (a, b) -> a.compareTo(b), what can we infer about the parameters?
ABoth a and b are integers
BBoth a and b are Comparable objects
Ca is a String and b is a number
DTypes 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.
What issue can arise when using lambda expressions with checked exceptions?
AChecked exceptions cannot be thrown directly in lambda expressions
BThey must be wrapped in try-catch or a custom functional interface must be created
CChecked exceptions automatically become unchecked in lambdas
DLambda 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.
What is the difference between peek() and forEach() in streams?
Apeek() is terminal, forEach() is intermediate
Bpeek() is intermediate and returns a stream, forEach() is terminal and returns void
CThere is no difference, they are aliases
Dpeek() 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.
AA shorthand syntax for lambda expressions that call existing methods
BAn operator for accessing private members
CA way to create anonymous classes
DA 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.