Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.
Which of the following lambda expressions is invalid?
Answer: D
Lambda must have parameter list (even if empty with ()). Missing parameter list '() -> x + y' is invalid syntax.
Q.2Medium
What is the type of the following lambda expression: x -> x.length()?
Answer: A
Function<T,R> takes one argument of type T and returns R. Here String -> Integer (length), so Function<String, Integer>.
Q.3Medium
Which functional interface should be used for a lambda that filters elements?
Answer: A
Predicate<T> tests a condition and returns boolean. It's ideal for filtering. Function would return any type, Consumer doesn't return anything.
Q.4Medium
What will this code print? List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.forEach(x -> System.out.print(x + " "));
Answer: B
forEach with Consumer lambda prints each element followed by space, resulting in '1 2 3 4 5 ' (with trailing space).
Q.5Medium
Can lambda expressions access local variables from their enclosing scope?
Answer: A
Lambda expressions can access local variables only if they are final or effectively final (not modified after initialization) due to closure requirements.
Q.6Medium
Which of the following correctly represents a BiFunction?
Answer: A
BiFunction takes 2 parameters and returns a result. (int a, int b) -> a + b fits this. Others are Function, Supplier, Consumer respectively.
Q.7Medium
What is the difference between a lambda expression and an anonymous inner class?
Answer: D
Lambdas provide concise syntax, are compiled directly without separate class files, and work only with functional interfaces (single abstract method).
Q.8Medium
Which of the following statements about @FunctionalInterface is true?
Answer: B
@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.
Q.9Medium
Can a lambda expression have multiple statements in its body?
Answer: B
Lambda can have multiple statements using braces: (x,y) -> { int z = x+y; return z; }. Single expression needs no braces or return.
Q.10Medium
Which functional interface is best suited for this scenario: method that takes no parameters and returns a random number?
Answer: A
Supplier<T> takes no parameters () and returns T. Perfect for generating/providing values like random numbers.
Q.11Medium
In Java streams, what does the filter() method use?
Answer: A
filter() method in streams takes Predicate which tests condition and returns boolean to determine if element should be included.
Q.12Medium
What will be the result of executing: List<Integer> nums = Arrays.asList(1, 2, 3, 4); nums.stream().map(x -> x * 2).collect(Collectors.toList());
Answer: A
The map() with lambda x -> x * 2 doubles each element: 1*2=2, 2*2=4, 3*2=6, 4*2=8, resulting in [2, 4, 6, 8].
Q.13Medium
Consider: Function<Integer, Integer> f = x -> x * x; What will f.apply(5) return?
Answer: A
Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.
Q.14Medium
What is the purpose of the Optional class when used with lambda expressions in Stream operations?
Answer: A
Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.
Q.15Medium
Which of the following lambda expressions is INCORRECT?
Answer: C
In option C, you cannot specify types in lambda parameters when using implicit typing. It should be either (x, y) or explicitly define all parameters.
Q.16Medium
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * x). What will the terminal operation need to be?
Answer: C
Both collect() and forEach() are valid terminal operations. collect() returns a list, forEach() performs an action. reduce() is also valid but would sum the squares.
Q.17Medium
What is method reference (::) in Java and how does it relate to lambda expressions?
Answer: A
Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).
Q.18Medium
What is the difference between peek() and forEach() when used with lambda expressions in Streams?
Answer: A
peek() is an intermediate operation used for debugging; it doesn't end the stream. forEach() is a terminal operation that consumes the stream.
Q.19Medium
In the lambda expression (a, b) -> a.compareTo(b), what is the implicit functional interface?
Answer: A
The Comparator<T> functional interface has the method compare(T a, T b) which returns an int, matching this lambda's behavior.
Q.20Medium
What will be the output of: IntStream.range(1, 4).map(x -> x * 2).sum();
Answer: A
range(1, 4) generates [1, 2, 3]. After map(x -> x * 2): [2, 4, 6]. Sum = 2 + 4 + 6 = 12.