Java Programming
Java OOP, collections, multithreading
270 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 270 questions
What is the purpose of a Predicate functional interface in Java?
A To transform one type of object to another
B To perform an action without returning a value
C To test a condition and return a boolean value
D To supply a value without taking any input
Correct Answer:  C. To test a condition and return a boolean value
EXPLANATION

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.

Take Test
Which of the following lambda expressions is syntactically incorrect?
A () -> System.out.println("Hello")
B (int x) -> x * x
C (x, y,) -> x + y
D x -> x > 5
Correct Answer:  C. (x, y,) -> x + y
EXPLANATION

The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.

Take Test
Which annotation is used to mark an interface as a functional interface in Java?
A @Function
B @FunctionalInterface
C @Lambda
D @Interface
Correct Answer:  B. @FunctionalInterface
EXPLANATION

@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.

Take Test
Which of the following is a valid functional interface that can be used with lambda expressions?
A An interface with exactly one abstract method
B An interface with multiple abstract methods
C An interface with no abstract methods
D An interface with static methods only
Correct Answer:  A. An interface with exactly one abstract method
EXPLANATION

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.

Take Test
What will be the output of:
Supplier greeting = () -> "Hello";
System.out.println(greeting.get());
A Hello
B null
C Compilation error
D Empty string
Correct Answer:  A. Hello
EXPLANATION

Supplier.get() returns the value produced by the lambda expression, which is "Hello".

Take Test
Advertisement
Which of the following best describes a functional interface?
A An interface that can only have lambda expressions
B An interface with exactly one abstract method
C An interface with all methods as default
D An interface with multiple abstract methods that are related
Correct Answer:  B. An interface with exactly one abstract method
EXPLANATION

A functional interface must have exactly one abstract method, which can be implemented using lambda expressions.

Take Test
What is the result of this code?
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
A 25
B 10
C 5
D Compilation error
Correct Answer:  A. 25
EXPLANATION

UnaryOperator applies the operation x * x where x = 5, resulting in 25.

Take Test
What is the output of:
BiFunction add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
A 8
B 53
C Compilation error
D Runtime exception
Correct Answer:  A. 8
EXPLANATION

BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.

Take Test
What is the return type of the lambda expression: (a, b) -> a > b?
A int
B boolean
C void
D Cannot be determined
Correct Answer:  B. boolean
EXPLANATION

The expression uses comparison operator (>), which returns a boolean value.

Take Test
A lambda expression can only be used with which type of interface?
A Regular interface
B Functional interface with single abstract method
C Interface with multiple abstract methods
D Interface with default methods only
Correct Answer:  B. Functional interface with single abstract method
EXPLANATION

Lambda expressions can only be assigned to functional interfaces that have exactly one abstract method.

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