Question 1
What does this print? ```java import java.util.function.*; public class Main { public static void main(String[] args) { Predicate<String> nonEmpty = s -> !s.isEmpty(); Predicate<String> tiny = s -> s.length() < 4; Predicate<String> p = nonEmpty.and(tiny).negate(); System.out.print(p.test("abc") + " " + p.test("")); } } ```
A. false trueCorrect answer
The predicate is NOT(nonEmpty AND tiny). For "abc" both conditions hold so the and is true and negate flips it to false; for "" nonEmpty is false so the and short-circuits to false and negate flips it to true, giving false true.
B. true false
true false is the un-negated nonEmpty.and(tiny); the trailing negate() flips both results.
C. true true
"abc" satisfies both conditions, so the negated result for it cannot be true.
D. false false
"" fails nonEmpty, so the negated result for it is true, not false.
Explanation
The composed predicate is the negation of (nonEmpty AND tiny), and negate() applies to the whole chain built so far, not just the last clause. A three-letter non-empty string satisfies both parts, so the conjunction is true and its negation is false; the empty string fails the non-empty test, which short-circuits the conjunction to false, so its negation is true. Like &&, Predicate.and short-circuits, so the second predicate is skipped once the first yields false.