Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 151–160 of 958
Topics in Java Programming
Q.151 Medium Lambda Expressions
Which functional interface should be used for a lambda that filters elements?
A Predicate
B Function
C Consumer
D Supplier
Correct Answer:  A. Predicate
EXPLANATION

Predicate<T> tests a condition and returns boolean. It's ideal for filtering. Function would return any type, Consumer doesn't return anything.

Take Test
Q.152 Medium Lambda Expressions
What is the type of the following lambda expression: x -> x.length()?
A Function
B Consumer
C Supplier
D Predicate
Correct Answer:  A. Function
EXPLANATION

Function<T,R> takes one argument of type T and returns R. Here String -> Integer (length), so Function<String, Integer>.

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

Lambda must have parameter list (even if empty with ()). Missing parameter list '() -> x + y' is invalid syntax.

Take Test
What will be the output of the following code?
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
A 25
B 5
C Compilation error
D Runtime error
Correct Answer:  A. 25
EXPLANATION

UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.

Take Test
Which package contains the built-in functional interfaces in Java?
A java.util.function
B java.lang.function
C java.io.function
D java.util.interface
Correct Answer:  A. java.util.function
EXPLANATION

The java.util.function package contains functional interfaces like Predicate, Consumer, Function, Supplier, and BiFunction introduced in Java 8.

Take Test
What is a functional interface in Java?
A An interface with exactly one abstract method
B An interface that extends multiple interfaces
C An interface with default methods only
D An interface with no methods
Correct Answer:  A. An interface with exactly one abstract method
EXPLANATION

A functional interface has exactly one abstract method. It can have multiple default methods. The @FunctionalInterface annotation can be used to mark it.

Take Test
Which of the following is a valid lambda expression syntax in Java?
A (int x, int y) -> x + y
B (x, y) => x + y
C [x, y] -> x + y
D (x, y) : x + y
Correct Answer:  A. (int x, int y) -> x + y
EXPLANATION

Valid Java lambda syntax uses arrow operator (->). Parameters can be typed or untyped, and the expression or block follows the arrow.

Take Test
What is the primary purpose of lambda expressions introduced in Java 8?
A To provide a concise syntax for implementing functional interfaces
B To replace all anonymous inner classes
C To improve memory management
D To simplify exception handling
Correct Answer:  A. To provide a concise syntax for implementing functional interfaces
EXPLANATION

Lambda expressions provide a concise way to implement single abstract method (functional interfaces) without verbose anonymous class syntax.

Take Test
Q.159 Medium Generics
In a microservices architecture, you're designing a Response wrapper class that should work with any data type, including primitives when boxed. Which implementation is correct?
A public class Response { private T data; private int statusCode; }
B public class Response { private Object data; private int statusCode; }
C public class Response { private T data; private int statusCode; }
D public class Response { private T data; private S statusCode; }
Correct Answer:  A. public class Response { private T data; private int statusCode; }
EXPLANATION

Option A provides proper generic type safety without unnecessary constraints. While Option C adds a Serializable bound (sometimes desirable), Option A is the most flexible and commonly used pattern for generic response wrappers.

Take Test
Q.160 Hard Generics
A method processes collections where items need to be added back to the same collection type. Which wildcard approach ensures type safety for write operations?
A public static void addElements(List
B public static void addElements(List destination, List source)
C public static void addElements(List destination, List source)
D public static void addElements(List destination, List source)
Correct Answer:  A. public static void addElements(List
EXPLANATION

'? super T' allows writing T instances (contravariant), while '? extends T' allows safe reading (covariant). This combination enables flexible yet type-safe collection manipulation.

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