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 is the return type of a lambda expression used with IntStream.range(1, 5).map(x -> x * 2)?
Avoid
Bint
CIntStream
DFunction
Correct Answer:
C. IntStream
EXPLANATION
map() is an intermediate operation that returns an IntStream. The lambda expression (x -> x * 2) transforms each int, but map() itself returns IntStream, not the transformed value type.
Consider the following code:
List fruits = Arrays.asList("apple", "banana", "cherry");
fruits.removeIf(s -> s.length() > 5);
What will be the contents of 'fruits' list after execution?
A["apple", "banana", "cherry"]
B["apple"]
C["apple", "cherry"]
D[]
Correct Answer:
B. ["apple"]
EXPLANATION
removeIf() removes all elements that satisfy the lambda predicate. Strings with length > 5 are "banana" (6 chars) and "cherry" (6 chars). Only "apple" (5 chars) remains as its length is not greater than 5.