Which annotation is used to mark a class as a Spring Bean in the latest Spring Framework?
A@Bean
B@Component
C@Service
D@Controller
Correct Answer:
B. @Component
EXPLANATION
@Component is a generic stereotype annotation for any Spring-managed component. @Bean is used on methods, while @Service and @Controller are specialized versions of @Component.
What will be the output of the following code?
List numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
A1 3 5
B2 4
C1 2 3 4 5
DCompilation error
Correct Answer:
B. 2 4
EXPLANATION
The filter operation with lambda expression (n -> n % 2 == 0) filters even numbers only. So 2 and 4 are printed, each on a new line due to println.
Which functional interface is used to create a lambda expression that takes two integers and returns a boolean value?
ABiPredicate
BBiFunction
CBiConsumer
DSupplier
Correct Answer:
A. BiPredicate
EXPLANATION
BiPredicate<T, U> is a functional interface that takes two parameters and returns a boolean. This matches the requirement of taking two integers and returning a boolean value.
In a lambda expression, what does the arrow (->) operator represent?
AAssignment operator
BSeparation between parameters and body
CComparison operator
DLogical AND operator
Correct Answer:
B. Separation between parameters and body
EXPLANATION
The arrow (->) in lambda expressions separates the parameter list on the left from the method body on the right. It's a syntax element specific to lambda expressions.