Java Programming
Java OOP, collections, multithreading
476 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 476 questions
A Spring application requires constructor-based dependency injection for a mandatory dependency. Which approach is best?
A @Autowired on setter method
B @Autowired on constructor
C @Resource annotation
D Manual object creation
Correct Answer:  B. @Autowired on constructor
EXPLANATION

Constructor-based DI ensures mandatory dependencies are provided at object creation time and is considered a best practice for required dependencies.

Take Test
Q.32 Medium Lambda Expressions
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.

Take Test
Q.33 Medium Lambda Expressions
A stream processes a list of strings and applies a lambda to convert them to uppercase. Which intermediate operation should be used?
A filter(s -> s.toUpperCase())
B map(s -> s.toUpperCase())
C peek(s -> s.toUpperCase())
D collect(s -> s.toUpperCase())
Correct Answer:  B. map(s -> s.toUpperCase())
EXPLANATION

The map() intermediate operation transforms each element using the provided lambda function. filter() returns boolean, peek() doesn't transform, and collect() is a terminal operation. map() is the correct choice for transformation.

Take Test
Q.34 Medium Lambda Expressions
Which of the following lambda expressions is INVALID in Java?
A () -> System.out.println("Hello")
B (int x) -> x * x
C (x, y) -> { return x + y; }
D (x, y) -> return x + y;
Correct Answer:  D. (x, y) -> return x + y;
EXPLANATION

In a lambda expression, if the body uses curly braces, 'return' keyword is required. But without curly braces, 'return' keyword cannot be used. Option D is invalid because it has 'return' without curly braces.

Take Test
Q.35 Medium Lambda Expressions
Which stream operation uses a lambda expression to transform elements from one type to another?
A filter()
B map()
C forEach()
D reduce()
Correct Answer:  B. map()
EXPLANATION

map() is used to transform elements. It takes a Function lambda that converts each element from one type/value to another.

Take Test
Advertisement
Q.36 Medium Lambda Expressions
Which of the following correctly uses a method reference as an alternative to a lambda expression?
A System.out::println instead of (x) -> System.out.println(x)
B String::length instead of (s) -> s.length()
C Integer::parseInt instead of (s) -> Integer.parseInt(s)
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three are valid method references that can replace their equivalent lambda expressions. Method references are a shorthand notation introduced in Java 8.

Take Test
Q.37 Medium Lambda Expressions
Consider: Comparator comp = (a, b) -> b - a; List list = Arrays.asList(3,1,2); Collections.sort(list, comp); What is the result?
A [1, 2, 3]
B [3, 2, 1]
C [2, 1, 3]
D [1, 3, 2]
Correct Answer:  B. [3, 2, 1]
EXPLANATION

The comparator (a, b) -> b - a returns negative values when b < a, causing elements to be sorted in descending order. Result is [3, 2, 1].

Take Test
Q.38 Medium Lambda Expressions
Which lambda expression correctly implements a custom functional interface: public interface Math { int calculate(int a, int b); }
A (a, b) -> a + b
B () -> 0
C (int a, int b) -> a - b
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both expressions are valid. Option A uses type inference while Option C explicitly specifies types. Both can implement the calculate method.

Take Test
Q.39 Medium Lambda Expressions
What is the output of: List nums = Arrays.asList(1,2,3); nums.stream().filter(x -> x > 1).forEach(System.out::println);
A 1 2 3
B 2 3
C 1
D Nothing is printed
Correct Answer:  B. 2 3
EXPLANATION

filter(x -> x > 1) keeps only elements greater than 1, which are 2 and 3. These are then printed on separate lines using forEach.

Take Test
Q.40 Medium Lambda Expressions
Consider the code: List names = Arrays.asList("Alice", "Bob"); names.forEach(name -> System.out.println(name)); What type of functional interface is used in forEach?
A Function
B Consumer
C Supplier
D Predicate
Correct Answer:  B. Consumer
EXPLANATION

forEach accepts a Consumer functional interface. Consumer<T> takes an input and performs an action without returning anything, which matches the System.out.println action.

Take Test
IGET
iget AI
Online · Ask anything about exams
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