Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 270
Topics in Java Programming
Which XML element is used to define a bean in Spring XML configuration?
A
B
C
D
Correct Answer:  B.
EXPLANATION

The <bean> element is the standard XML element used in applicationContext.xml to define Spring beans.

Test
What does @Autowired annotation do in Spring?
A Automatically injects dependencies by type
B Creates a new bean instance
C Marks a class as deprecated
D Enables auto-scanning of packages
Correct Answer:  A. Automatically injects dependencies by type
EXPLANATION

@Autowired performs type-based dependency injection. Spring looks for a bean of the required type and injects it automatically.

Test
Which of the following is NOT a valid Spring bean scope in Spring 5.x+?
A singleton
B prototype
C request
D eternal
Correct Answer:  D. eternal
EXPLANATION

Valid scopes are singleton, prototype, request, session, application, and websocket. 'eternal' is not a valid scope.

Test
What is the primary purpose of Dependency Injection (DI) in Spring Framework?
A To increase code complexity
B To reduce coupling between classes and improve testability
C To replace all design patterns
D To automatically generate SQL queries
Correct Answer:  B. To reduce coupling between classes and improve testability
EXPLANATION

DI reduces coupling by injecting dependencies rather than creating them internally, making code more testable and maintainable.

Test
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.

Test
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);
A 1 3 5
B 2 4
C 1 2 3 4 5
D Compilation 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.

Test
Which functional interface is used to create a lambda expression that takes two integers and returns a boolean value?
A BiPredicate
B BiFunction
C BiConsumer
D Supplier
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.

Test
Given: Function f = x -> x * 2; Integer result = f.apply(5); What is the value of result?
A 5
B 10
C 15
D 25
Correct Answer:  B. 10
EXPLANATION

The lambda expression x -> x * 2 multiplies the input by 2. When applied to 5, it returns 10.

Test
What is the correct syntax for a lambda expression with no parameters that returns a fixed value?
A -> "Hello"
B () -> "Hello"
C ( ) -> "Hello"
D { } -> "Hello"
Correct Answer:  B. () -> "Hello"
EXPLANATION

When a lambda has no parameters, empty parentheses () are required before the arrow. Options A and D are syntactically incorrect.

Test
In a lambda expression, what does the arrow (->) operator represent?
A Assignment operator
B Separation between parameters and body
C Comparison operator
D Logical 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.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips