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?
public class News<________> {}
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?
Collection<String> c = new ____________<>();
c.add("pen");
c.remove("pen");
System.out.println(c.isEmpty());
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?
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
public int compare(String s1, String s2) {
return s2.length() s1.length();
}
public int compare(Object obj1, object obj2) {
String s1 = (String) obj1;
String s2 = (String) obj2;
return s1.length() - s2.length();
}
public int compare(Object obj1, object obj2) {
String s1 = (String) obj1;
String s2 = (String) obj2;
return s2.length() s1.length();
}
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
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.
ArrayList<Integer> list = new ArrayList<>();
list.add(56);
list.add(56);
list.add(3);
TreeSet<Integer> 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
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
5: ArrayDeque<Integer> d = new ArrayDeque<>();
6: d.offer(18);
7: d.offer(5);
8: d.push(13);
9: System.out.println(d.poll() + " " + d.poll());
A. 13 18
B. 18 5
C. 18 13
D. None of the above
class Magazine {
private String name;
public Magazine(String name) {
this.name = name;
}
public int compareTo(Magazine m) {
return name.compareTo(m.name);
}
public String toString() {
return name;
}
}public class Newstand {
public static void main(String[] args) {
Set<Magazine> set = new TreeSet<>();
set.add(new Magazine("highlights"));
set.add(new Magazine("Newsweek"));
set.add(new Magazine("highlights"));
System.out.println(set.iterator().next());
}
}
A. highlights
B. Newsweek
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
6: List<String> list = new ArrayList<>();
7: list.add("Monday");
8: list.add(String::new);
9: list.add("Tuesday");
10: list.remove(0);
11: System.out.println(list.get(0));
A. An empty String
B. Monday
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
List<String> list = new LinkedList<>();
list.add("Archie");
list.add("X-Men");
list.stream().forEach(s -> System.out.println(s));
list.stream().forEach(s -> System.out.println(s));
A. Two
B. Four
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
1: interface Comic<C> {
2: void draw(C c);
3: }
4: class ComicClass<C> implements Comic<C> {
5: public void draw(C c) {
6: System.out.println(c);
7: }
8: }
9: class SnoopyClass implements Comic<Snoopy> {
10: public void draw(Snoopy c) {
11: System.out.println(c);
12: }
13: }
14: class SnoopyComic implements Comic<Snoopy> {
15: public void draw(C c) {
16: System.out.println(c);
17: }
18: }
19: public class Snoopy {
20: public static void main(String[] args) {
21: Comic<Snoopy> c1 = c > System.out.println(c);
22: Comic<Snoopy> c2 = new ComicClass<>();
23: Comic<Snoopy> c3 = new SnoopyClass();
24: Comic<Snoopy> c4 = new SnoopyComic();
25: }
26: }
A. Line 21
B. Line 22
C. Line 23
D. Line 24
Stream<String> s = Stream.of("Atlanta", "Chicago", "New York");
long count = s.filter(c -> c.startsWith("C")).count();
System.out.print(count);
A. 1
B. 2
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
public class Truck implements Comparable<Truck> {
private int id;
public Truck(int id) {
this.id = id;
}
@Override
____________________ {
return id - t.id;
}
}
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.
List<String> list = new LinkedList<>();
list.add("Archie");
list.add("X-Men");
Stream<String> s = list.stream();
s.forEach(System.out::println);
s.forEach(System.out::println);
A. Two
B. Four
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
class Wash<T> {
T item;
public void clean(T item) {
System.out.println("Clean " + item);
}
}p
ublic class LaundryTime {
public static void main(String[] args) {
________________________
wash.clean("socks");
}
}
A. Wash wash = new Wash();
B. Wash wash = new Wash
C. Wash
D. All three can fill in the blank.
LinkedList<String> list = new LinkedList<>();
list.add("Grapes of Wrath");
list.add("1984");
list.stream().forEach(System.out::println);
Iterator it = list.iterator();
while (it.hasNext())
System.out.println(it.next());
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.
List<String> list = new ArrayList<>();
list.add("Atlanta");
list.add("Chicago");
list.add("New York");
list.stream().filter(____________).forEach(System.out::println);
A. String::isEmpty
B. ! String::isEmpty
C. String::! isEmpty
D. None of the above
TreeMap<String, Integer> map = new TreeMap<>();
map.put("3", 3);
map.put("three", 3);
map.put("THREE", 3);
System.out.println(map.firstKey() + " " + map.lastKey());
A. 3 three
B. 3 THREE
C. three 3
D. THREE 3
import java.util.*;
public class ExtendingGenerics {
private static <_____________ , U> U add(T list, U element) {
list.add(element);
return element;
}
public static void main(String[] args) {
List<String> values = new ArrayList<>();
add(values, 'duck');
add(values, 'duck');
add(values, 'goose');
System.out.println(values);
}
}
A. ? extends Collection
B. ? implements Collection
C. T extends Collection
D. T implements Collection
List<String> list = new ArrayList<>();
list.add("Austin");
list.add("Boston");
list.add("San Francisco");
list.removeIf(a -> a.length() > 10);
System.out.println(list.size());
A. 1
B. 2
C. 3
D. None of the above
ArrayDeque<Integer> dice = new ArrayDeque<>();
dice.offer(3);
dice.offer(2);
dice.offer(4);
System.out.print(dice.stream().filter(n -> n != 4));
<br/>A. 2
<br/>B. 3
<br/>C. The code does not compile.
<br/>D. None of the above
<p><strong>32. Which of the following cannot fill in the blank to make the code compile?</strong>
private void output(____________<?> x) {
x.forEach(System.out::println);
}
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