Java MCQ
Java Tricky Java 8 Features Questions
What is the output?
List<String> list = Arrays.asList("a", "b", "c");
list.stream()
.map(String::toUpperCase)
.forEach(System.out::print);
list.stream()
.map(String::toUpperCase)
.forEach(System.out::print);
The map operation converts each string to uppercase using method reference String::toUpperCase, and forEach prints them without spaces.
What will be printed?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int result = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * 2)
.reduce(0, Integer::sum);
System.out.println(result);
int result = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * 2)
.reduce(0, Integer::sum);
System.out.println(result);
Filter keeps even numbers (2,4), map doubles them (4,8), reduce sums them (4+8=12).
What is the output?
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
public class Test {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;
System.out.println(add.calculate(2, 3));
System.out.println(multiply.calculate(2, 3));
}
}
interface Calculator {
int calculate(int a, int b);
}
public class Test {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;
System.out.println(add.calculate(2, 3));
System.out.println(multiply.calculate(2, 3));
}
}
6
Lambda expressions provide implementations for the functional interface. add: 2+3=5, multiply: 2*3=6.
What will be printed?
List<String> words = Arrays.asList("hello", "world", "java");
String result = words.stream()
.filter(s -> s.length() > 4)
.findFirst()
.orElse("none");
System.out.println(result);
String result = words.stream()
.filter(s -> s.length() > 4)
.findFirst()
.orElse("none");
System.out.println(result);
Filter keeps words with length > 4 ("hello"=5, "world"=5). findFirst() returns the first match "hello".
What is the output?
Optional<String> opt = Optional.ofNullable(null);
System.out.println(opt.orElse("default"));
System.out.println(opt.orElse("default"));
Optional.ofNullable(null) creates an empty Optional. orElse() returns the default value when Optional is empty.
What will be printed?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
long count = numbers.stream()
.map(n -> n * n)
.filter(n -> n > 10)
.count();
System.out.println(count);
long count = numbers.stream()
.map(n -> n * n)
.filter(n -> n > 10)
.count();
System.out.println(count);
Map squares numbers: 1,4,9,16,25. Filter keeps numbers >10: 16,25. Count returns 2.
What is the output?
interface Greeting {
default void sayHello() {
System.out.print("Hello ");
}
}
class Person implements Greeting {
public void sayHello() {
Greeting.super.sayHello();
System.out.print("World");
}
}
public class Test {
public static void main(String[] args) {
new Person().sayHello();
}
}
default void sayHello() {
System.out.print("Hello ");
}
}
class Person implements Greeting {
public void sayHello() {
Greeting.super.sayHello();
System.out.print("World");
}
}
public class Test {
public static void main(String[] args) {
new Person().sayHello();
}
}
The class overrides the default method and calls the interface's default method using InterfaceName.super.methodName().
What will be printed?
List<String> list = Arrays.asList("a1", "a2", "b1", "c2", "c1");
list.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::print);
list.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::print);
Filter keeps "c1","c2", map converts to uppercase "C1","C2", sorted arranges them as "C1","C2".
What is the output?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.parallelStream()
.reduce(0, (a, b) -> a + b);
System.out.println(sum);
int sum = numbers.parallelStream()
.reduce(0, (a, b) -> a + b);
System.out.println(sum);
parallelStream processes elements in parallel but reduce with identity and associative operation works correctly. 1+2+3+4+5=15.
What will be printed?
LocalDate date = LocalDate.of(2023, 2, 28);
LocalDate newDate = date.plusDays(2);
System.out.println(newDate);
LocalDate newDate = date.plusDays(2);
System.out.println(newDate);
February 2023 has 28 days. Adding 2 days to Feb 28 goes to March 2 (Feb 28 → Mar 1 → Mar 2).