Showing 41–50 of 270 questions
In Java 8, a lambda expression can access local variables from its enclosing scope. What is the constraint on these variables?
A
They must be declared as final or effectively final
B
They must be static variables
C
They must be instance variables of the class
D
They can be modified freely within the lambda
Correct Answer:
A. They must be declared as final or effectively final
EXPLANATION
Lambda expressions can only access local variables that are final or effectively final to ensure thread safety and immutability.
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
EXPLANATION
Consumer accepts one argument and returns nothing. print.accept("java") calls lambda which prints s.toUpperCase() = "JAVA".
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
EXPLANATION
UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.
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.
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.
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.
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.
When using bounded type parameters with multiple interfaces, what is the correct syntax in Java?
EXPLANATION
Java uses ampersand (&) to combine multiple bounds. The class must appear first, followed by interfaces. Commas, pipes, and 'implements' keyword are not valid syntax for type parameter bounds.
In a Spring Boot application, you need to create a generic repository interface that works with any entity type. Which declaration correctly implements this pattern?
A
public interface GenericRepository { T findById(Long id); void save(T entity); }
B
public interface GenericRepository { Object findById(Long id); void save(Object entity); }
C
public interface GenericRepository { T findById(Long id); void save(T entity); }
D
public interface GenericRepository { T findById(K id); void save(T entity); }
Correct Answer:
A. public interface GenericRepository { T findById(Long id); void save(T entity); }
EXPLANATION
Option A uses single type parameter T for entity type, which is the standard pattern for generic repositories. Option C is redundant (all classes extend Object), and Option D adds unnecessary complexity.
Which declaration is valid in Java Generics?
A
List list = new ArrayList();
B
List list = new ArrayList();
C
List list = new ArrayList();
D
List
Correct Answer:
B. List list = new ArrayList();
EXPLANATION
Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.