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

  1. 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 so that it sorts the longest strings first. You may assume there are no nulls. Which method could implement such a comparator? A.

public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
B.
public int compare(String s1, String s2) {
return s2.length()  s1.length();
}
C.
public int compare(Object obj1, object obj2) {
String s1 = (String) obj1;
String s2 = (String) obj2;
return s1.length() - s2.length();
}
D.
public int compare(Object obj1, object obj2) {
String s1 = (String) obj1;
String s2 = (String) obj2;
return s2.length()  s1.length();
}
6. 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

7. What is a common reason for a stream pipeline not to run?
A. The source doesnt generate any items.
B. There are no intermediate operations.
C. The terminal operation is missing.
D. None of the above

8. We want this code to print the titles of each book twice. Why doesnt 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.

9. What is the result of the following?

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

10. 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. A terminal operation where one element is returned from the prior step in a

stream pipeline without reading all the elements 11. What is the output of the following?

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

12. What is the output of the following?

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.

13. What is the result of the following?

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.

14. How many lines does this code output?

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.

15. Which line in the main() method doesnt compile or points to a class that doesnt compile?

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

16. What is the output of the following?

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.

17. Fill in the blank to make this code compile:

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)

18. In a stream pipeline, which can return a value other than a Stream?
A. Source
B. Intermediate operation
C. Terminal operation
D. None of the above

19. Rewrite this lambda using a constructor reference: n -> new ArrayList<>(n)
A. ArrayList::new;
B. ArrayList::new();
C. ArrayList::new(n);
D. ArrayList::new[n];

20. What is the result of the following? Comparator c = (x, y) > y x; List ints = Arrays.asList(3, 1, 4); Collections.sort(ints, c); System.out.println(Collections.binarySearch(ints, 1));
A. 0
B. 1
C. The code does not compile.
D. The result is not defined.

21. How many lines does this code output?

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.

22. Which option cannot fill in the blank to print Clean socks?

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 wash = new Wash<>();
D. All three can fill in the blank.

23. We want this code to print the titles of each book twice. Why doesnt it?

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.

24. Rewrite this lambda using a method reference: () -> Math.random()
A. Math.random
B. Math::random
C. Math::random()
D. None of the above

25. Which operation can occur more than once in a stream pipeline?
A. Source
B. Intermediate operation
C. Terminal operation
D. None of the above

26. Which type allows inserting a null value?
A. ArrayDeque
B. ArrayList
C. TreeSet
D. All of these allow nulls.

27. Fill in the blank so this code outputs three lines:

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

28. What is the output of the following?

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

29. Which fills in the blank in the method signature to allow this code to compile?

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

30. What is the result of the following?

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

31. What does the following output?

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

33. How many lines does this code output? List list = new LinkedList<>(); list.add("Archie"); list.add("X-Men"); list.stream().forEach(System.out.println); list.stream().forEach(System.out.println);
A. Two
B. Four
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

34. What is the output of the following? class Magazine implements Comparable { private String name; public Magazine(String name) { this.name = name; } @Override public int compareTo(Magazine m) { return name.compareTo(m.name); } @Override public String toString() { return name; } }p ublic class Newstand { public static void main(String[] args) { Set 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.

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 list = Arrays.asList(b1, b2); list.stream().filter(Blankie::getColor).forEach(System.out::println); } }
A. One
B. Two
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

36. Which statement about a source in a Stream is true?
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.

37. What does the following output? List list = new ArrayList<>(); list.add("Austin"); list.add("Boston"); list.add("San Francisco"); long c = list.stream().filter(a -> a.length() > 10).count(); System.out.println(c + " " + list.size());
A. 1 1
B. 1 3
C. 2 3
D. None of the above

38. Which options can fill in the blanks to print Cleaned 2 items? import java.util.*; class Wash { T item; public void clean(T items) { System.out.println("Cleaned " + items.size() + " items"); } }p ublic class LaundryTime { public static void main(String[] args) { Wash wash = new ____________ wash.clean(Arrays.asList("sock", "tie")); } }
A. extends, Wash();
B. extends, Wash();
C. super, Wash();
D. super, Wash();

39. Which of the following declares a Comparator where all objects are treated as equal?
A. Comparator comp = (c1)-> 0;
B. Comparator comp = (c1)-> {0};
C. Comparator comp = (c1, c2)-> 0;
D. Comparator comp = (c1, c2)-> {0};

40. Why cant String::charAt be used as a method reference?
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

  1. Oracle (OCJP) Java Certification Exam
  2. Java Certification Tips And Tricks
  3. OCJP Practice Papers - Java Basics
  4. OCJP Practice Papers - Operators And Decision Constructs
  5. OCJP Practice Papers-A Advanced Java Class Design
  6. OCJP Practice Papers Creating And Using Arrays
  7. OCJP Practice Papers Using Loop Constructs
  8. OCJP Practice Papers - Generics And Collections
  9. OCJP Practice Papers - Lambda Built-In Functional Interfaces
  10. OCJP Practice Papers Java Class Design
  11. OCJP Practice Papers - Java Stream API
  12. OCJP Practice Papers - Exceptions And Assertions
  13. OCJP Practice Papers - Date/Time API
  14. OCJP Practice Papers - Java I/O Fundamentals
  15. OCJP Practice Papers - Working With Methods And Encapsulation
  16. OCJP Practice Papers - Working With Inheritance
  17. OCJP Practice Papers - Handling Exceptions
  18. OCJP Practice Papers - Selected Classes From Java API
  19. OCJP Practice Papers - Java File I/O Nio.2
  20. OCJP Practice Papers - Java Concurrency