Question 1
What is the output of the following program?
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<String, Integer> parser = s -> Integer.parseInt(s);
try {
System.out.println(parser.apply("abc"));
} catch (NumberFormatException e) {
System.out.println("error");
}
}
}A. abc
"abc" is the input string; parsing it does not echo it back — it triggers an exception instead.
B. errorCorrect answer
Integer.parseInt("abc")throws the unchecked NumberFormatException, which propagates out of apply and is caught, so "error" prints; lambdas may throw unchecked exceptions freely (JLS §15.27.2).C. 0
0 assumes a fallback value on parse failure, but Java supplies none — parseInt throws rather than returning a default.
D. Compilation fails because lambdas cannot throw exceptions
Lambdas can throw exceptions; only checked exceptions require the functional interface to declare them, and NumberFormatException is unchecked, so this compiles.
Explanation
A lambda may throw any unchecked (RuntimeException) type without the functional interface declaring it; only checked exceptions must appear in the interface method's throws clause (JLS §15.27.2, §11.2). Parsing a non-numeric string raises an unchecked exception that propagates out of the functional call and is handled by the surrounding try/catch.