What is the output of:
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
Answer: A
BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.
Q.242Easy
What is the result of this code?
UnaryOperator<Integer> square = x -> x * x;
System.out.println(square.apply(5));
Answer: A
UnaryOperator applies the operation x * x where x = 5, resulting in 25.
Q.243Easy
Which of the following best describes a functional interface?
Answer: B
A functional interface must have exactly one abstract method, which can be implemented using lambda expressions.
Q.244Easy
What will be the output of:
Supplier<String> greeting = () -> "Hello";
System.out.println(greeting.get());
Answer: A
Supplier.get() returns the value produced by the lambda expression, which is "Hello".
Q.245Easy
Which of the following is a valid functional interface that can be used with lambda expressions?
Answer: A
A functional interface must have exactly one abstract method. This is the defining characteristic that allows it to be used with lambda expressions and method references.
Advertisement
Q.246Easy
Which annotation is used to mark an interface as a functional interface in Java?
Answer: B
@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.
Q.247Easy
Which of the following lambda expressions is syntactically incorrect?
Answer: C
The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.
Q.248Easy
What is the purpose of a Predicate functional interface in Java?
Answer: C
Predicate<T> is a functional interface that takes a single input of type T and returns a boolean. It's commonly used for filtering operations in streams.
Q.249Easy
In a lambda expression, what does the arrow (->) operator represent?
Answer: B
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.
Q.250Easy
What is the correct syntax for a lambda expression with no parameters that returns a fixed value?
Answer: B
When a lambda has no parameters, empty parentheses () are required before the arrow. Options A and D are syntactically incorrect.
Q.251Easy
Given: Function<Integer, Integer> f = x -> x * 2; Integer result = f.apply(5); What is the value of result?
Answer: B
The lambda expression x -> x * 2 multiplies the input by 2. When applied to 5, it returns 10.
Q.252Easy
Which functional interface is used to create a lambda expression that takes two integers and returns a boolean value?
Answer: A
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.
Q.253Easy
What will be the output of the following code?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
Answer: B
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.
Q.254Easy
Which annotation is used to mark a class as a Spring Bean in the latest Spring Framework?
Answer: B
@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.
Q.255Easy
What is the primary purpose of Dependency Injection (DI) in Spring Framework?
Answer: B
DI reduces coupling by injecting dependencies rather than creating them internally, making code more testable and maintainable.
Q.256Easy
Which of the following is NOT a valid Spring bean scope in Spring 5.x+?
Answer: D
Valid scopes are singleton, prototype, request, session, application, and websocket. 'eternal' is not a valid scope.
Q.257Easy
What does @Autowired annotation do in Spring?
Answer: A
@Autowired performs type-based dependency injection. Spring looks for a bean of the required type and injects it automatically.
Q.258Easy
Which XML element is used to define a bean in Spring XML configuration?
Answer: B
The <bean> element is the standard XML element used in applicationContext.xml to define Spring beans.
Q.259Easy
In Spring Framework, what is the primary purpose of the @Autowired annotation?
Answer: A
@Autowired is used for dependency injection, allowing Spring to automatically resolve and inject bean dependencies without explicit configuration.
Q.260Easy
Which of the following correctly describes the lifecycle of a singleton-scoped Spring bean?
Answer: B
Singleton beans are instantiated once per Spring container and reused for the application's lifetime, making them memory-efficient for stateless objects.