Valid BiPredicate Lambda Parameter Syntax
Which two are valid statements? (Choose two.)
Community Votes
50% of anonymous learners picked answer BC. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
The question tests the combination of implicit/explicit type specification and the final modifier in lambdas; the trap is assuming multiple correct options exist when the syntax rules are mutually exclusive.
This question tests the strict syntax rules for lambda parameters in Java, specifically regarding type inference with 'var' and the 'final' modifier. The page establishes that only option C is a valid statement due to specific compiler constraints on mixed parameter types.
Most candidates select B or BC, incorrectly believing that explicit types can be combined with the final modifier on the second parameter while omitting it on the first, which violates the rule that all parameters must either be explicitly typed or inferred via var consistently.
Community Discussion (5 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
Only option C is a valid statement. In Java, you cannot mixvar (inferred) and explicit types in the same lambda parameter list. Additionally, if you use var, you cannot apply the final modifier to individual variables within that list because the compiler infers the type and implicitly treats them as final anyway, making explicit final redundant or syntactically invalid depending on the mix. Option C uses var for both, which is consistent and valid.Why the Other Options Are Wrong
Option A mixes explicitInteger with final var, which is illegal because var implies inference, conflicting with the explicit type. Option B mixes explicit Integer with final Integer; while explicit types are allowed, you cannot apply final to one parameter without applying it to others if you want consistency, but more importantly, raw types like BiPredicate are discouraged, though technically compilable with warnings. However, the primary issue is often the mix of styles. Actually, looking closer at B: (Integer x, final Integer y) is syntactically valid in a lambda if the target type is known, but here BiPredicate is a raw type. The real killer for B vs C in exam contexts is often the strictness of var. Let's re-evaluate. Option D mixes final Integer with var, which is illegal. Option E mixes final var with y (implicit), which is illegal. So A, D, E are definitely out. Between B and C: B uses explicit types. C uses var. Both should theoretically compile if the target type is BiPredicate<Integer, Integer>. But BiPredicate is raw. With raw types, x.equals(y) might cause issues. However, the community consensus and strict syntax rules often flag mixed modifiers. Actually, the key is that you cannot have final on some and not others? No, you can. The issue with B is likely the raw type causing unchecked warnings or the specific exam logic favoring var. Wait, let's look at the comments. User ASPushkin says B is not correct because of raw types. C is correct because var works. This suggests the exam considers raw types invalid for this context or prefers the clean var syntax. Thus, C is the single best answer? But the question asks for TWO. If only C is valid, the question is flawed. However, if we must choose two, and A, D, E are strictly illegal syntax (mixing var and explicit/final), then B is the only other candidate that has valid syntax even if it has warnings. But wait, can you do (Integer x, final Integer y)? Yes. So B is syntactically valid. C is syntactically valid. Therefore, B and C are the answers.Community Comment Notes
Users like ASPushkin argue that B is incorrect due to raw type usage, preferring C'svar syntax. Others confirm that C is correct and note that mixing modifiers leads to compilation errors. The split votes suggest confusion between syntax validity and best practices. Exam Strategy
When dealing with lambda parameter lists, remember that you cannot mix var and explicit types. Also, ensure consistency with the final modifier across all parameters if used. Always check if the target type is specified correctly.
Frequently Asked Questions
Why is mixing var and explicit types illegal?
Java does not allow combining type-inferred parameters (var) with explicitly typed parameters in the same lambda expression to maintain clarity and consistency.
Can I use final on some parameters but not others?
Yes, you can apply final to individual parameters, but it is often unnecessary for var-inferred parameters and can lead to confusion if not applied consistently.