Question 15
Back to Exercise
What is the output of the following Java program?
import java.util.function.DoubleUnaryOperator;
import java.util.function.IntBinaryOperator;
import java.util.function.IntPredicate;
import java.util.function.ToIntFunction;
public class Main {
public static void main(String[] args) {
IntPredicate prime = n -> { if (n < 2) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; };
IntBinaryOperator gcd = (a, b) -> { while (b != 0) { int t = a % b; a = b; b = t; } return a; };
ToIntFunction<String> vowels = s -> (int) s.chars().filter(c -> "aeiou".indexOf(c) >= 0).count();
DoubleUnaryOperator round2 = d -> Math.round(d * 100) / 100.0;
System.out.println(prime.test(97) + " " + prime.negate().test(1) + " " + gcd.applyAsInt(84, 120) + " " + vowels.applyAsInt("functional") + " " + round2.applyAsDouble(3.14159));
}
}
- A true true 12 4 3.14 Correct
- B true false 12 4 3.14
- C true true 6 4 3.14
- D false true 12 5 3.142
Correct Answer: Option A
Lambda parameters behave like local variables, so the gcd lambda can reassign a and b. "functional" has u, i, o, a.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!