IS5103 Lesson-10

1.Which of the following throw an exception when an Optional is empty?
Each correct answer represents a complete solution. Choose all that apply.
A) opt.orElseThrow();
opt.orElseThrow(RuntimeException::new);
opt.get();

2.Assume that the directory /animals exists and is empty. What is the
result of executing the following code?
Path path = Path.of(“/animals”);
try (var z = Files.walk(path)) {
boolean b = z
.filter((p,a) -> a.isDirectory() && !path.equals(p)) // x
.findFirst().isPresent(); // y
System.out.print(b ? “No Sub”: “Has Sub”);
}
A) The code will not compile because of line x.

3.What could be the output of the following code snippet?
var stream = Stream.iterate(“”, (s) -> s + “1”);
System.out.println(stream.limit(2).map(x -> x + “2”));
A) java.util.stream.ReferencePipeline$3@4517d9a3

4.What could be the output of the following?
Predicate <String>predicate = s -> s.startsWith(“g”);
var stream1 = Stream.generate(() -> “growl!”);
var stream2 = Stream.generate(() -> “growl!”);
var b1 = stream1.anyMatch(predicate);
var b2 = stream2.allMatch(predicate);
System.out.println(b1 + ” ” + b2);
A) The code hangs.

5.What could be the output of the following?
Predicate predicate = s -> s.length()> 3;
var stream = Stream.iterate(“-“,
s -> ! s.isEmpty(), (s) -> s + s);
var b1 = stream.noneMatch(predicate);
var b2 = stream.anyMatch(predicate);
System.out.println(b1 + ” ” + b2);
A) An exception is thrown

6.Which are true statements about terminal operations in a stream that
runs successfully?
Each correct answer represents a complete solution. Choose all that
apply
A) At most one terminal operation can exist in a stream pipeline
Terminal operations are a required part of the stream pipeline in order to
get a result.

7.Which of the following can fill in the blank so that the code prints out
false?
var s = Stream.generate(() -> “meow”);
var match = s.__(String::isEmpty);
System.out.println(match);
A) allMatch

8.We have a method that returns a sorted list without changing the
original. Which of the following can replace the method implementation
to do the same with streams?
private static List<String>sort(List<String> list) {
var copy = new ArrayList<String>(list);
Collections.sort(copy, (a, b) -> b.compareTo(a));
return copy;
}
A) return list.stream().sorted((a, b) -> b.compareTo(a)).collect(Collectors.toLi
st());

9.Given the four statements (L, M, N, O), select and order the ones that
would complete the expression and cause the code to output 10 lines
Stream.generate(() -> “1”)
L: .filter(x -> x.length()> 1)
M: .forEach(System.out::println)
N: .limit(10)
O: .peek(System.out::println)
;
A) N, M

10.Which is true of the following code?
Set<String> birds = Set.of(“oriole”, “flamingo”);
Stream.concat(birds.stream(), birds.stream(), birds.stream())
.sorted() // line X
.distinct()
.findAny()
.ifPresent(System.out::println);
A) The code does not compile.

11.Which of the following can we add after line 3 for the code to run
without error and not produce any output?
var stream = LongStream.of(1, 2, 3);
var opt = stream.map(n -> n * 10)
.filter(n -> n < 5).findFirst();
Each correct answer represents a complete solution. Choose all that apply.
A) if (opt.isPresent()) System.out.println(opt.getAsLong());
opt.ifPresent(System.out::println);

12.What is the simplest way of rewriting this code?
List x = IntStream.range(1, 6)
.mapToObj(i -> i)
.collect(Collectors.toList());
x.forEach(System.out::println);
A) IntStream.range(1, 6).forEach(System.out::println);

13.Which of the following sets result to 8.0?
Each correct answer represents a complete solution. Choose all that apply.
A) I. double result = LongStream.of(6L, 8L, 10L).mapToInt(x -> (int) x).boxe
d().collect(Collectors.groupingBy(x -> x)).keySet().stream().collect(Coll
ectors.averagingInt(x -> x));

II. double result = LongStream.of(6L, 8L, 10L).mapToInt(x -> (int) x).boxed().coll
ect(Collectors.groupingBy(x -> x, Collectors.toSet())).keySet().stream().collec
t(Collectors.averagingInt(x -> x));

14.Which of the following are true for the given declaration?
var is = IntStream.empty();
Each correct answer represents a complete solution. Choose all that apply.
A) is.findAny() returns the type OptionalInt.
is.sum() returns the type int.

15.Which of the following is true?
List<Integer> x1 = List.of(1, 2, 3);
List<Integer> x2 = List.of(4, 5, 6);
List<Integer> x3 = List.of();
Stream.of(x1, x2, x3).map(x -> x + 1)
.flatMap(x -> x.stream())
.forEach(System.out::print);
A) The code does not compile.

16.Which of the following are true?
1. Stream<Integer> s = Stream.of(1);
2.IntStream is = s.boxed();
3.DoubleStream ds = s.mapToDouble(x -> x);
4.Stream<Integer> s2 = ds.mapToInt(x -> x);
5.s2.forEach(System.out::print);
Each correct answer represents a complete solution. Choose all that apply.
A) Line 2 causes a compiler error.
Line 4 causes a compiler error.

17.What is the result of the following code?
var s = DoubleStream.of(1.2, 2.4);
s.peek(System.out::println).filter(x -> x> 2).count();
A) 1.2 and 2.4

18.What is the output of the following?
public class Paging {
record Sesame(String name, boolean human) {
@Override public String toString() {
return name();
}
}
record Page(List list, long count) {}
public static void main(String[] args) {
var monsters = Stream.of(new Sesame(“Elmo”, false));
var people = Stream.of(new Sesame(“Abby”, true));
printPage(monsters, people);
}
private static void printPage(Stream monsters,
Stream<Sesame> people) {
Page page = Stream.concat(monsters, people)
.collect(Collectors.teeing(
Collectors.filtering(s -> s.name().startsWith(“E”),
Collectors.toList()),
Collectors.counting(),
(l, c) -> new Page(l, c)));
System.out.println(page);
} }
A) Page[list=[Elmo], count=2]

19.What changes need to be made together for this code to print the string 12345?
Stream.iterate(1, x -> x++)
.limit(5).map(x -> x)
.collect(Collectors.joining());
Each correct answer represents a part of the solution. Choose all that
apply
A) Change map(x -> x) to map(x -> “” + x)
Change x -> x++ to x -> ++x.
Wrap the entire line in a System.out.print statement.

20.Given the generic type String, the partitioningBy() collector creates a Map> when passed to collect() by default. When a downstream collector is passed to partitioningBy(), which return types can be created?
Each correct answer represents a complete solution. Choose all that apply.
A) Map<Boolean, List<String>>
Map<Boolean, Set<String>>

21.Which of the following statements are true about this code?
1. Predicate empty = String::isEmpty;
2. Predicate notEmpty = empty.negate();
3.
4.var result = Stream.generate(() -> “”)
5..limit(10)
6..filter(notEmpty)
7..collect(Collectors.groupingBy(k -> k))
8..entrySet()
9..stream()
10..map(Entry::getValue)
11..flatMap(Collection::stream)
12..collect(Collectors.partitioningBy(notEmpty));
13.System.out.println(result);
Each correct answer represents a complete solution. Choose all that apply.
A) It outputs {false=[], true=[]}.
If we changed line 12 from partitioningBy(notEmpty) to groupingBy(n ->
n), it would output {}.

22.What is the output of the following code?
var spliterator = Stream.generate(() -> “x”)
.spliterator();
spliterator.tryAdvance(System.out::print);
var split = spliterator.trySplit();
split.tryAdvance(System.out::print);
A) xx

Other Links:



Statistics Quiz




Networking Quiz




See other websites for quiz:



Check on QUIZLET




Check on CHEGG

Leave a Reply

Your email address will not be published. Required fields are marked *