50+Collection Based Objective Question

1. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        list.forEach(System.out::println);

    }

}

a) one two three
b) three two one
c) one two
d) Compilation Error

Check Answer

2. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        list.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);

    }

}

a) one two three
b) three two one
c) two three one
d) Compilation Error

Check Answer

3. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Optional<String> result = list.stream().filter(s -> s.length() > 3).findFirst();

        System.out.println(result.isPresent());

    }

}

a) true
b) false
c) Compilation Error
d) Runtime Error

Check Answer

4. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Optional<String> result = list.stream().filter(s -> s.length() > 3).findAny();

        System.out.println(result.orElse(“none”));

    }

}

a) one
b) two
c) three
d) none

Check Answer

5. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(e -> System.out.println(e.getValue()));

    }

}

a) one two three
b) three two one
c) two three one
d) Compilation Error

Check Answer

6. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(e -> System.out.println(e.getKey()));

    }

}

a) 1 2 3
b) 3 1 2
c) 1 3 2
d) Compilation Error

Check Answer

7. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.entrySet().stream().filter(e -> e.getValue().length() > 3).forEach(e -> System.out.println(e.getKey()));

    }

}

a) 1
b) 2
c) 3
d) Compilation Error

Check Answer

8. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        boolean result = map.entrySet().stream().anyMatch(e -> e.getValue().startsWith(“t”));

        System.out.println(result);

    }

}

a) true
b) false
c) Compilation Error
d) Runtime Error

Check Answer

9. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, List<String>> map = list.stream().collect(Collectors.groupingBy(String::length));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 0

Check Answer

10. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, List<String>> map = list.stream().collect(Collectors.groupingBy(String::length));

        System.out.println(map.get(3).size());

    }

}

a) 1
b) 2
c) 3
d) 0

Check Answer

11. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(String::length, Collectors.counting()));

        System.out.println(map.get(5));

    }

}

a) 1
b) 2
c) 3
d) 0

Check Answer

12. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Boolean, List<String>> map = list.stream().collect(Collectors.partitioningBy(s -> s.length() > 3));

        System.out.println(map.get(true).size());

    }

}

a) 1
b) 2
c) 3
d) 0

Check Answer

13. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Boolean, List<String>> map = list.stream().collect(Collectors.partitioningBy(s -> s.length() < 4));

        System.out.println(map.get(false).size());

    }

}

a) 1
b) 2
c) 3
d) 0

Check Answer

14. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Boolean, Long> map = list.stream().collect(Collectors.partitioningBy(s -> s.length() > 3, Collectors.counting()));

        System.out.println(map.get(true));

    }

}

a) 1
b) 2
c) 3
d) 0

Check Answer

15. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        String result = list.stream().collect(Collectors.joining(“, “));

        System.out.println(result);

    }

}

a) one, two, three
b) one two three
c) one, two, three
d) Compilation Error

Check Answer

16. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        String result = list.stream().collect(Collectors.joining(“-“));

        System.out.println(result);

    }

}

a) one-two-three
b) one, two, three
c) one two three
d) Compilation Error

Check Answer

17. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        int result = list.stream().reduce(0, (a, b) -> a + b);

        System.out.println(result);

    }

}

a) 10
b) 15
c) 20
d) 0

Check Answer

18. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        int result = list.stream().reduce(1, (a, b) -> a * b);

        System.out.println(result);

    }

}

a) 10
b) 15
c) 20
d) 120

Check Answer

19. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> result = list.stream().reduce(Integer::max);

        System.out.println(result.get());

    }

}

a) 1
b) 5
c) 10
d) 15

Check Answer

20. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> result = list.stream().reduce(Integer::min);

        System.out.println(result.get());

    }

}

a) 1
b) 5
c) 10
d) 15

Check Answer

21. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));

        set.removeIf(n -> n % 2 == 0);

        System.out.println(set);

    }

}

a) [1, 2, 3, 4, 5]
b) [1, 3, 5]
c) [2, 4]
d) Compilation Error

Check Answer

22. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));

        set.removeIf(n -> n % 2 != 0);

        System.out.println(set);

    }

}

a) [1, 2, 3, 4, 5]
b) [1, 3, 5]
c) [2, 4]
d) Compilation Error

Check Answer

23. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        list.replaceAll(n -> n * 2);

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [2, 4, 6, 8, 10]
c) [2, 4]
d) Compilation Error

Check Answer

24. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        list.sort(Comparator.reverseOrder());

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [5, 4, 3, 2, 1]
c) [2, 4]
d) Compilation Error

Check Answer

25. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Collections.shuffle(list);

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [5, 4, 3, 2, 1]
c) Random order of elements
d) Compilation Error

Check Answer

26. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.replace(2, “TWO”);

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) null
d) Compilation Error

Check Answer

27. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.replaceAll((k, v) -> v.toUpperCase());

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) null
d) Compilation Error

Check Answer

28. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.merge(2, “TWO”, String::concat);

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) twoTWO
d) Compilation Error

Check Answer

29. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.computeIfAbsent(4, k -> “four”);

        System.out.println(map.get(4));

    }

}

a) null
b) four
c) Compilation Error
d) Runtime Error

Check Answer

30. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.computeIfPresent(2, (k, v) -> v.toUpperCase());

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) null
d) Compilation Error

Check Answer

31. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.computeIfPresent(4, (k, v) -> v.toUpperCase());

        System.out.println(map.get(4));

    }

}

a) null
b) four
c) Compilation Error
d) Runtime Error

Check Answer

32. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.computeIfAbsent(2, k -> “TWO”);

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) null
d) Compilation Error

Check Answer

33. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length));

        System.out.println(map.get(“two”));

    }

}

a) 2
b) 3
c) 4
d) Compilation Error

Check Answer

34. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length, (v1, v2) -> v1));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

35. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length, (v1, v2) -> v2));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

36. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, String> map = list.stream().collect(Collectors.toMap(String::length, Function.identity(), (v1, v2) -> v1));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

37. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, String> map = list.stream().collect(Collectors.toMap(String::length, Function.identity(), (v1, v2) -> v2));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

38. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, List<String>> map = list.stream().collect(Collectors.groupingBy(String::length));

        System.out.println(map.get(3).size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

39. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Boolean, List<String>> map = list.stream().collect(Collectors.partitioningBy(s -> s.length() > 3));

        System.out.println(map.get(true).size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

40. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        String result = list.stream().collect(Collectors.joining(“, “));

        System.out.println(result);

    }

}

a) one, two, three
b) one two three
c) one, two, three
d) Compilation Error

Check Answer

41. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        int result = list.stream().reduce(0, (a, b) -> a + b);

        System.out.println(result);

    }

}

a) 10
b) 15
c) 20
d) 0

Check Answer

42. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        int result = list.stream().reduce(1, (a, b) -> a * b);

        System.out.println(result);

    }

}

a) 10
b) 15
c) 20
d) 120

Check Answer

43. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> result = list.stream().reduce(Integer::max);

        System.out.println(result.get());

    }

}

a) 1
b) 5
c) 10
d) 15

Check Answer

44. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> result = list.stream().reduce(Integer::min);

        System.out.println(result.get());

    }

}

a) 1
b) 5
c) 10
d) 15

Check Answer

45. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));

        set.removeIf(n -> n % 2 == 0);

        System.out.println(set);

    }

}

a) [1, 2, 3, 4, 5]
b) [1, 3, 5]
c) [2, 4]
d) Compilation Error

Check Answer

46. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));

        set.removeIf(n -> n % 2 != 0);

        System.out.println(set);

    }

}

a) [1, 2, 3, 4, 5]
b) [1, 3, 5]
c) [2, 4]
d) Compilation Error

Check Answer

47. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        list.replaceAll(n -> n * 2);

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [2, 4, 6, 8, 10]
c) [2, 4]
d) Compilation Error

Check Answer

48. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        list.sort(Comparator.reverseOrder());

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [5, 4, 3, 2, 1]
c) [2, 4]
d) Compilation Error

Check Answer

49. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Collections.shuffle(list);

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [5, 4, 3, 2, 1]
c) Random order of elements
d) Compilation Error

Check Answer

50. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.replace(2, “TWO”);

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) null
d) Compilation Error

Check Answer

51. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.replaceAll((k, v) -> v.toUpperCase());

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) null
d) Compilation Error

Check Answer

52. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.merge(2, “TWO”, String::concat);

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) twoTWO
d) Compilation Error

Check Answer

53. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.computeIfAbsent(4, k -> “four”);

        System.out.println(map.get(4));

    }

}

a) null
b) four
c) Compilation Error
d) Runtime Error

Check Answer

54. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.computeIfPresent(2, (k, v) -> v.toUpperCase());

        System.out.println(map.get(2));

    }

}

a) two
b) TWO
c) null
d) Compilation Error

Check Answer

55. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();

        map.put(1, “one”);

        map.put(2, “two”);

        map.put(3, “three”);

        map.computeIfPresent(4, (k, v) -> v.toUpperCase());

        System.out.println(map.get(4));

    }

}

a) null
b) four
c) Compilation Error
d) Runtime Error

Check Answer

56. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length));

        System.out.println(map.get(“two”));

    }

}

a) 2
b) 3
c) 4
d) Compilation Error

Check Answer

57. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length, (v1, v2) -> v1));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

58. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length, (v1, v2) -> v2));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

59. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, String> map = list.stream().collect(Collectors.toMap(String::length, Function.identity(), (v1, v2) -> v1));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

60. What will be the output of the following code?

java

import java.util.*;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, String> map = list.stream().collect(Collectors.toMap(String::length, Function.identity(), (v1, v2) -> v2));

        System.out.println(map.size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

61. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Integer, List<String>> map = list.stream().collect(Collectors.groupingBy(String::length));

        System.out.println(map.get(3).size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

62. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        Map<Boolean, List<String>> map = list.stream().collect(Collectors.partitioningBy(s -> s.length() > 3));

        System.out.println(map.get(true).size());

    }

}

a) 1
b) 2
c) 3
d) 4

Check Answer

63. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<String> list = Arrays.asList(“one”, “two”, “three”);

        String result = list.stream().collect(Collectors.joining(“, “));

        System.out.println(result);

    }

}

a) one, two, three
b) one two three
c) one, two, three
d) Compilation Error

Check Answer

64. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        int result = list.stream().reduce(0, (a, b) -> a + b);

        System.out.println(result);

    }

}

a) 10
b) 15
c) 20
d) 0

Check Answer

65. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        int result = list.stream().reduce(1, (a, b) -> a * b);

        System.out.println(result);

    }

}

a) 10
b) 15
c) 20
d) 120

Check Answer

66. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> result = list.stream().reduce(Integer::max);

        System.out.println(result.get());

    }

}

a) 1
b) 5
c) 10
d) 15

Check Answer

67. What will be the output of the following code?

java

import java.util.*;

import java.util.stream.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> result = list.stream().reduce(Integer::min);

        System.out.println(result.get());

    }

}

a) 1
b) 5
c) 10
d) 15

Check Answer

68. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));

        set.removeIf(n -> n % 2 == 0);

        System.out.println(set);

    }

}

a) [1, 2, 3, 4, 5]
b) [1, 3, 5]
c) [2, 4]
d) Compilation Error

Check Answer

69. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));

        set.removeIf(n -> n % 2 != 0);

        System.out.println(set);

    }

}

a) [1, 2, 3, 4, 5]
b) [1, 3, 5]
c) [2, 4]
d) Compilation Error

Check Answer

70. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        list.replaceAll(n -> n * 2);

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [2, 4, 6, 8, 10]
c) [2, 4]
d) Compilation Error

Check Answer

71. What will be the output of the following code?

java

import java.util.*;

public class Test {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        list.sort(Comparator.reverseOrder());

        System.out.println(list);

    }

}

a) [1, 2, 3, 4, 5]
b) [5, 4, 3, 2, 1]
c) [2, 4]
d) Compilation Error

Check Answer