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
Answer: a) one two three
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
Answer: b) three two one
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
Answer: b) false
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
Answer: d) none
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
Answer: a) one two three
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
Answer: b) 3 1 2
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
Answer: c) 3
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
Answer: a) true
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
Answer: b) 2
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
Answer: b) 2
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
Answer: a) 1
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
Answer: a) 1
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
Answer: a) 1
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
Answer: a) 1
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
Answer: a) one, two, three
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
Answer: a) one-two-three
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
Answer: b) 15
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
Answer: d) 120
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
Answer: b) 5
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
Answer: a) 1
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
Answer: b) [1, 3, 5]
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
Answer: c) [2, 4]
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
Answer: b) [2, 4, 6, 8, 10]
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
Answer: b) [5, 4, 3, 2, 1]
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
Answer: c) Random order of elements
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
Answer: b) TWO
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
Answer: b) TWO
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
Answer: c) twoTWO
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
Answer: b) four
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
Answer: b) TWO
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
Answer: a) null
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
Answer: a) two
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
Answer: b) 3
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
Answer: c) 3
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
Answer: c) 3
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
Answer: b) 2
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
Answer: b) 2
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
Answer: b) 2
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
Answer: a) 1
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
Answer: a) one, two, three
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
Answer: b) 15
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
Answer: d) 120
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
Answer: b) 5
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
Answer: a) 1
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
Answer: b) [1, 3, 5]
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
Answer: c) [2, 4]
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
Answer: b) [2, 4, 6, 8, 10]
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
Answer: b) [5, 4, 3, 2, 1]
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
Answer: c) Random order of elements
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
Answer: b) TWO
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
Answer: b) TWO
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
Answer: c) twoTWO
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
Answer: b) four
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
Answer: b) TWO
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
Answer: a) null
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
Answer: b) 3
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
Answer: c) 3
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
Answer: c) 3
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
Answer: b) 2
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
Answer: b) 2
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
Answer: b) 2
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
Answer: a) 1
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
Answer: a) one, two, three
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
Answer: b) 15
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
Answer: d) 120
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
Answer: b) 5
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
Answer: a) 1
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
Answer: b) [1, 3, 5]
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
Answer: c) [2, 4]
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
Answer: b) [2, 4, 6, 8, 10]
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
Answer: b) [5, 4, 3, 2, 1]