Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 270
Topics in Java Programming
Which of the following is a valid lambda expression in Java?
A () -> System.out.println("Hello")
B (int x, int y) -> { return x + y; }
C (String s) -> s.length()
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three are valid lambda expression syntaxes with different parameter types and return statements.

Test
Which stream operation would you use to transform elements from one type to another?
A filter()
B map()
C forEach()
D collect()
Correct Answer:  B. map()
EXPLANATION

map() is the intermediate operation used to transform elements from one type/value to another. It applies a function to each element.

Test
What is the purpose of the @FunctionalInterface annotation in Java?
A To indicate an interface can be used with lambda expressions
B To enforce that an interface has exactly one abstract method
C To improve performance of lambda expressions
D To allow multiple lambda expressions in one interface
Correct Answer:  B. To enforce that an interface has exactly one abstract method
EXPLANATION

@FunctionalInterface is a compile-time check that ensures an interface has exactly one abstract method, making it safe for lambda implementation.

Test
What is a method reference (::) in Java?
A A shorthand syntax for lambda expressions that call existing methods
B An operator for accessing private members
C A way to create anonymous classes
D A reference to a method's memory address
Correct Answer:  A. A shorthand syntax for lambda expressions that call existing methods
EXPLANATION

Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.

Test
Which functional interface is used for operations that take two arguments and return a single value?
A Function
B BiFunction
C Consumer
D Supplier
Correct Answer:  B. BiFunction
EXPLANATION

BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.

Test
What will be the output of: List list = Arrays.asList("a", "bb", "ccc"); list.forEach(s -> System.out.print(s.length() + " "));
A 1 2 3
B a bb ccc
C 3 2 1
D 1 1 1
Correct Answer:  A. 1 2 3
EXPLANATION

The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.

Test
What is the primary advantage of using lambda expressions in Java 8+?
A They reduce boilerplate code and enable functional programming style
B They increase execution speed significantly
C They eliminate the need for classes
D They are required for all stream operations
Correct Answer:  A. They reduce boilerplate code and enable functional programming style
EXPLANATION

Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.

Test
Which functional interface is used for operations that take no arguments and return a value?
A Supplier
B Function
C Predicate
D BiConsumer
Correct Answer:  A. Supplier
EXPLANATION

Supplier<T> is a functional interface with method get() that takes no parameters and returns a value of type T.

Test
Consider the code: List names = Arrays.asList("Raj", "Priya", "Amit"); names.forEach(n -> System.out.println(n)); What does this code do?
A Prints each name with a newline
B Creates a new list with uppercase names
C Filters names starting with 'A'
D Sorts the names alphabetically
Correct Answer:  A. Prints each name with a newline
EXPLANATION

forEach with the lambda expression n -> System.out.println(n) iterates through each element and prints it with a newline.

Test
What is the return type of the map() method when used with lambda expressions in Java Streams?
A Stream
B List
C Set
D Map
Correct Answer:  A. Stream
EXPLANATION

The map() method in Stream API returns a new Stream with transformed elements of type R, maintaining the stream pipeline.

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