Home
46.
Suppose we want to store JellyBean objects. Which of the following pairs require JellyBean to implement the Comparable interface or create a Comparator in order to add them to the Collection ?
  • A.
    ArrayList and ArrayDeque
  • B.
    HashMap and HashSet
  • C.
    HashMap and TreeMap
  • D.
    TreeMap and TreeSet
  • Answer & Explanation
  • Report
Answer : [C]
Explanation :
Option A is incorrect because a pipeline still runs if the source doesn’t generate any items and the rest of the pipeline is correct. Granted some of the operations have nothing to do, but control still passes to the terminal operation. Option B is incorrect because intermediate operations are optional. Option C is the answer. The terminal operation triggers the pipeline to run.
Report
Name Email  
47.
What is a common reason for a stream pipeline not to run?
  • A.
    The source doesn’t generate any items.
  • B.
    There are no intermediate operations.
  • C.
    The terminal operation is missing.
  • D.
    None of the above
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
The Iterator interface uses the hasNext() and next() methods to iterate. Sincethere is not a hasMore() method, it should be changed to hasNext() , making Option B the answer. With respect to Option A, the missing generic type gives a warning, but the code still runs. For Option C, iterators can run as many times as you want, as can the forEach() method on list .
Report
Name Email  
48.
We want this code to print the titles of each book twice. Why doesn’t it?
LinkedList<String> list = new LinkedList<>();
list.add("Grapes of Wrath");
list.add("1984");
list.forEach(System.out::println);
Iterator it = list.iterator();
while (it.hasMore())
System.out.println(it.next());
  • A.
    The generic type of Iterator is missing.
  • B.
    The hasMore() method should be changed to hasNext() .
  • C.
    The iteration code needs to be moved before the forEach() since the stream is used up.
  • D.
    None of the above. The code does print each book title twice.
  • Answer & Explanation
  • Report
Answer : [A]
Explanation :
First the code creates an ArrayList of three elements. Then the list is transformed into a TreeSet . Since sets are not allowed to have duplicates, the set only has two elements. Remember that a TreeSet is sorted, which means that the first element in the TreeSet is 3 . Therefore, Option A is correct.
Report
Name Email  
49.
What is the result of the following?
ArrayList list = new ArrayList<>();
list.add(56);
list.add(56);
list.add(3);
TreeSet set = new TreeSet<>(list);
System.out.print(set.size());
System.out.print(" " );
System.out.print(set.iterator().next());
  • A.
    2 3
  • B.
    2 56
  • C.
    3 3
  • D.
    3 56
  • Answer & Explanation
  • Report
Answer : [A]
Explanation :
First the code creates an ArrayList of three elements. Then the list is transformed into a TreeSet . Since sets are not allowed to have duplicates, the set only has two elements. Remember that a TreeSet is sorted, which means that the first element in the TreeSet is 3 . Therefore, Option A is correct.
Report
Name Email  
50.
What best describes a reduction?
  • A.
    An intermediate operation where it filters the stream it receives
  • B.
    An intermediate operation where it mathematically divides each element in the stream
  • C.
    A terminal operation where a single value is generated by reading each element in the prior step in a stream pipeline
  • D.
    terminal operation where one element is returned from the prior step in a stream pipeline without reading all the elements
  • Answer & Explanation
  • Report
Answer : [C]
Explanation :
The word reduction is used with streams for a terminal operation, so Options A and B are incorrect. Option D describes a valid terminal operation like anyMatch() , but is not a reduction. Option C is correct because a reduction has to look at each element in the stream in order to determine the result.
Report
Name Email