Java MCQ

Previous Java Multiple Choice Questions Next

Java Tricky Java 8 Features Questions

1

What is the output?
List<String> list = Arrays.asList("a", "b", "c");
list.stream()
  .map(String::toUpperCase)
  .forEach(System.out::print);

Correct Answer: B) ABC

The map operation converts each string to uppercase using method reference String::toUpperCase, and forEach prints them without spaces.

2

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);

Correct Answer: B) 12

Filter keeps even numbers (2,4), map doubles them (4,8), reduce sums them (4+8=12).

3

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));
  }
}

Correct Answer: A) 5
6

Lambda expressions provide implementations for the functional interface. add: 2+3=5, multiply: 2*3=6.

4

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);

Correct Answer: A) hello

Filter keeps words with length > 4 ("hello"=5, "world"=5). findFirst() returns the first match "hello".

5

What is the output?
Optional<String> opt = Optional.ofNullable(null);
System.out.println(opt.orElse("default"));

Correct Answer: B) default

Optional.ofNullable(null) creates an empty Optional. orElse() returns the default value when Optional is empty.

6

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);

Correct Answer: B) 2

Map squares numbers: 1,4,9,16,25. Filter keeps numbers >10: 16,25. Count returns 2.

7

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();
  }
}

Correct Answer: C) Hello World

The class overrides the default method and calls the interface's default method using InterfaceName.super.methodName().

8

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);

Correct Answer: A) C1C2

Filter keeps "c1","c2", map converts to uppercase "C1","C2", sorted arranges them as "C1","C2".

9

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);

Correct Answer: A) 15

parallelStream processes elements in parallel but reduce with identity and associative operation works correctly. 1+2+3+4+5=15.

10

What will be printed?
LocalDate date = LocalDate.of(2023, 2, 28);
LocalDate newDate = date.plusDays(2);
System.out.println(newDate);

Correct Answer: B) 2023-03-02

February 2023 has 28 days. Adding 2 days to Feb 28 goes to March 2 (Feb 28 → Mar 1 → Mar 2).

Previous Java Multiple Choice Questions Next