Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 141–150 of 958
Topics in Java Programming
What will happen if you try to compile this code?
BiFunction concat = (a, b) -> a + b;
concat.apply("Java", "8", "Features");
A It will compile and run successfully
B Runtime error - extra argument
C Compilation error - too many arguments
D No output produced
Correct Answer:  C. Compilation error - too many arguments
EXPLANATION

BiFunction takes exactly 2 parameters. Passing 3 arguments is compilation error. apply() only accepts 2 arguments.

Take Test
Q.142 Medium Lambda Expressions
Which functional interface is best suited for this scenario: method that takes no parameters and returns a random number?
A Supplier
B Consumer
C Predicate
D Function
Correct Answer:  A. Supplier
EXPLANATION

Supplier<T> takes no parameters () and returns T. Perfect for generating/providing values like random numbers.

Take Test
Q.143 Medium Lambda Expressions
Can a lambda expression have multiple statements in its body?
A No, lambda can only have single expression
B Yes, using curly braces and explicit return statement
C Yes, but only if using arrow syntax
D No, compile will throw error
Correct Answer:  B. Yes, using curly braces and explicit return statement
EXPLANATION

Lambda can have multiple statements using braces: (x,y) -> { int z = x+y; return z; }. Single expression needs no braces or return.

Take Test
What is the output of this code?
Consumer print = s -> System.out.println(s.toUpperCase());
print.accept("java");
A java
B JAVA
C Compilation error
D java JAVA
Correct Answer:  B. JAVA
EXPLANATION

Consumer accepts one argument and returns nothing. print.accept("java") calls lambda which prints s.toUpperCase() = "JAVA".

Take Test
Q.145 Medium Lambda Expressions
Which of the following statements about @FunctionalInterface is true?
A It is mandatory for all functional interfaces
B It's optional but helps compiler verify exactly one abstract method
C It prevents inheritance of the interface
D It makes the interface static
Correct Answer:  B. It's optional but helps compiler verify exactly one abstract method
EXPLANATION

@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.

Take Test
What will be the output of this nested lambda?
Function add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
A 8
B 3
C 5
D Compilation error
Correct Answer:  A. 8
EXPLANATION

Curried function: add.apply(3) returns a Function that adds 3. .apply(5) on that adds 5 to 3, returning 8.

Take Test
Q.147 Medium Lambda Expressions
What is the difference between a lambda expression and an anonymous inner class?
A Lambda is more concise, doesn't create separate class file
B Anonymous inner class is faster
C Lambda can only be used with functional interfaces
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Lambdas provide concise syntax, are compiled directly without separate class files, and work only with functional interfaces (single abstract method).

Take Test
Q.148 Medium Lambda Expressions
Which of the following correctly represents a BiFunction?
A (int a, int b) -> a + b
B () -> 5
C x -> x * 2
D () -> System.out.println("Hi")
Correct Answer:  A. (int a, int b) -> a + b
EXPLANATION

BiFunction takes 2 parameters and returns a result. (int a, int b) -> a + b fits this. Others are Function, Supplier, Consumer respectively.

Take Test
Q.149 Medium Lambda Expressions
Can lambda expressions access local variables from their enclosing scope?
A Yes, but only if they are final or effectively final
B Yes, any local variable can be accessed
C No, lambda expressions cannot access local variables
D Only static variables can be accessed
Correct Answer:  A. Yes, but only if they are final or effectively final
EXPLANATION

Lambda expressions can access local variables only if they are final or effectively final (not modified after initialization) due to closure requirements.

Take Test
Q.150 Medium Lambda Expressions
What will this code print?
List list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(x -> System.out.print(x + " "));
A 1 2 3 4 5
B 1 2 3 4 5
C Compilation error
D 15
Correct Answer:  B. 1 2 3 4 5
EXPLANATION

forEach with Consumer lambda prints each element followed by space, resulting in '1 2 3 4 5 ' (with trailing space).

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