OCJP Practice Papers - Generics And Collections
OCJP Practice Papers Using Loop Constructs include following topics
- Create and use a generic class
- Create and use ArrayList, TreeSet, TreeMap, and ArrayDeque objects
- Use java.util.Comparator and java.lang.Comparable interfaces
- Collections Streams and Filters
- Iterate using forEach methods of Streams and List
- Describe Stream interface and Stream pipeline
- Filter a collection by using lambda expressions
- Use method references with Streams
See the complete syllabus for OCJP here
- Which of the following can fill in the blank to make the code compile?
I. ?
II. News
III. Object
A. None of them
B. I
C. II and III
D. I, II, and III
2. Which method is available on both List and Stream implementations?
A. filter()
B. forEach()
C. replace()
D. sort()
3. We are running a library. Patrons select books by name. They get at the back of the
checkout line. When they get to the front, they scan the books ISBN. The checkout
system finds the book based on this number and marks the book as checked out. Of
these choices, which data structures best represent the line to check out the book and
the book lookup to mark it as checked out, respectively?
A. ArrayDeque, TreeMap
B. ArrayDeque, TreeSet
C. ArrayList, TreeMap
D. ArrayList, TreeSet
4. Which cannot fill in the blank for this code to compile?
A. ArrayDeque
B. TreeMap
C. TreeSet
D. All of these can fill in the blank.
5. Suppose we want to implement a Comparator 7. What is a common reason for a stream pipeline not to run?
8. We want this code to print the titles of each book twice. Why doesnt it?
9. What is the result of the following?
10. What best describes a reduction?
stream pipeline without reading all the elements
11. What is the output of the following?
12. What is the output of the following?
13. What is the result of the following?
14. How many lines does this code output?
15. Which line in the main() method doesnt compile or points to a class that doesnt compile?
16. What is the output of the following?
17. Fill in the blank to make this code compile:
18. In a stream pipeline, which can return a value other than a Stream?
19. Rewrite this lambda using a constructor reference:
n -> new ArrayList<>(n)
20. What is the result of the following?
Comparator 21. How many lines does this code output?
22. Which option cannot fill in the blank to print Clean socks?
23. We want this code to print the titles of each book twice. Why doesnt it?
24. Rewrite this lambda using a method reference:
() -> Math.random()
25. Which operation can occur more than once in a stream pipeline?
26. Which type allows inserting a null value?
27. Fill in the blank so this code outputs three lines:
28. What is the output of the following?
29. Which fills in the blank in the method signature to allow this code to compile?
30. What is the result of the following?
31. What does the following output?
33. How many lines does this code output?
List 34. What is the output of the following?
class Magazine implements Comparable 35. How many lines does the following code output?
import java.util.*;
class Blankie {
String color;
String getColor() {
return color;
}
}p
ublic class PreSchool {
public static void main(String[] args) {
Blankie b1 = new Blankie();
Blankie b2 = new Blankie();
b1.color = "pink";
List 36. Which statement about a source in a Stream is true?
37. What does the following output?
List 38. Which options can fill in the blanks to print Cleaned 2 items?
import java.util.*;
class Wash 39. Which of the following declares a Comparator where all objects are treated as equal?
40. Why cant String::charAt be used as a method reference?
A. ArrayList and ArrayDeque
B. HashMap and HashSet
C. HashMap and TreeMap
D. TreeMap and TreeSet
A. The source doesnt generate any items.
B. There are no intermediate operations.
C. The terminal operation is missing.
D. None of the above
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.
A. 2 3
B. 2 56
C. 3 3
D. 3 56
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. A terminal operation where one element is returned from the prior step in a
A. 13 18
B. 18 5
C. 18 13
D. None of the above
A. highlights
B. Newsweek
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. An empty String
B. Monday
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. Two
B. Four
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. Line 21
B. Line 22
C. Line 23
D. Line 24
A. 1
B. 2
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. public int compare(Truck t)
B. public int compare(Truck t1, Truck t2)
C. public int compareTo(Truck t)
D. public int compareTo(Truck t1, Truck t2)
A. Source
B. Intermediate operation
C. Terminal operation
D. None of the above
A. ArrayList::new;
B. ArrayList::new();
C. ArrayList::new(n);
D. ArrayList::new[n];
A. 0
B. 1
C. The code does not compile.
D. The result is not defined.
A. Two
B. Four
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. Wash wash = new Wash();
B. Wash wash = new Wash
C. Wash
D. All three can fill in the blank.
A. The generic type of Iterator is missing.
B. The hasNext() method should be changed to isNext().
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.
A. Math.random
B. Math::random
C. Math::random()
D. None of the above
A. Source
B. Intermediate operation
C. Terminal operation
D. None of the above
A. ArrayDeque
B. ArrayList
C. TreeSet
D. All of these allow nulls.
A. String::isEmpty
B. ! String::isEmpty
C. String::! isEmpty
D. None of the above
A. 3 three
B. 3 THREE
C. three 3
D. THREE 3
A. ? extends Collection
B. ? implements Collection
C. T extends Collection
D. T implements Collection
A. 1
B. 2
C. 3
D. None of the above
A. ArrayDeque
B. Collection
C. TreeMap
D. None of the above
A. Two
B. Four
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. highlights
B. Newsweek
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. One
B. Two
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
A. The source is mandatory in a stream pipeline.
B. The source is only allowed to return primitives.
C. The source must be retrieved by calling the stream() method.
D. The source must return a finite number of elements.
A. 1 1
B. 1 3
C. 2 3
D. None of the above
wash = new ____________
wash.clean(Arrays.asList("sock", "tie")); }
}
A. extends, Wash
B. extends, Wash();
C. super, Wash
D. super, Wash();
A. Comparator
B. Comparator
C. Comparator
D. Comparator
A. Method references can only be used on static methods.
B. Method references can pass either the instance or the parameter from the lambda,
but not both.
C. The charAt() method takes an int rather than Integer parameter.
D. There is no charAt() method in the String class
OCJP Practice Test, Certification Path, Tips/Tricks