PASS - TAKER : Anonymous

Java SE 8 OCP - Generics and Collections

Correct : 7

38% Complete (success)

38 %

False : 11

61% Complete (success)

61 %

Anonymous 2019-07-05T03:10:17


1. Given: {code} Which statements could be inserted at // INSERT DECLARATION HERE to allow this code to compile and run? (Choose all that apply.)

A is incorrect because List is an interface, so you can't say new List(), regardless of any generic types. D, E, and F are incorrect because List only takes one type parameter (a Map would take two, not a List). C is tempting, but incorrect. The type argument <List<Integer>> must be the same for both sides of the assignment, even though the constructor new ArrayList() on the right side is a subtype of the declared type List on the left.


2. Which statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden? (Choose all that apply.)

B is true because often two dissimilar objects can return the same hashcode value. D is true because if the hashCode() comparison returns ==, the two objects might or might not be equal. A, C, and E are incorrect. C is incorrect because the hashCode() method is very flexible in its return values, and often two dissimilar objects can return the same hashcode value. A and E are a negation of the hashCode() and equals() contract.


3. Given: {code} Which statements are true?

You can't put both Strings and ints into the same TreeSet. Without generics, the compiler has no way of knowing what type is appropriate for this TreeSet, so it allows everything to compile. At runtime, the TreeSet will try to sort the elements as they're added, and when it tries to compare an Integer with a String, it will throw a ClassCastException. Note that although the before() method does not use generics, it does use autoboxing. Watch out for code that uses some new features and some old features mixed together.

Not Correct

4. Given: {code} Which is correct? (Choose all that apply.)

If hashCode() is not overridden, then every entry will go into its own bucket, and the overridden equals() method will have no effect on determining equivalency. If hashCode() is overridden, then the overridden equals() method will view t1 and t2 as duplicates.

Not Correct

5. Given: {code} This class is to be updated to make use of appropriate generic types, with no changes in behavior (for better or worse). Which of these steps could be performed? (Choose three.)

A is incorrect because you can't use a primitive type as a type parameter. C is incorrect because a Map takes two type parameters separated by a comma. D is incorrect because an int can't autobox to a null, and F is incorrect because a null can't unbox to 0. H is incorrect because you can't autobox a primitive just by trying to invoke a method with it.


6. Given: {code} Which of the following changes (taken separately) would allow this code to compile? (Choose all that apply.)

The problem with the original code is that Sheep tries to implement Herbivore<Sheep> and Herbivore declares that its type parameter E can be any type that extends Plant. Since a Sheep is not a Plant, Herbivore<Sheep> makes no sense—the type Sheep is outside the allowed range of Herbivore's parameter E. Only solutions that either alter the definition of a Sheep or alter the definition of Herbivore will be able to fix this. So A, E, and F are eliminated. B works—changing the definition of an Herbivore to allow it to eat Sheep solves the problem. C doesn't work because an Herbivore<Plant> must have a munch(Plant) method, not munch(Grass). And D doesn't work, because in D we made Sheep extend Plant—now the Wolf class breaks because its munch(Sheep) method no longer fulfills the contract of Carnivore.


7. Which collection class(es) allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized? (Choose all that apply.)

All of the collection classes allow you to grow or shrink the size of your collection. ArrayList provides an index to its elements. The newer collection classes tend not to have synchronized methods. Vector is an older implementation of ArrayList functionality and has synchronized methods; it is slower than ArrayList. C, List, is an interface, and F, PriorityQueue, does not offer access by index.

Not Correct

8. Given a method declared as public static <E extends Number> List<E> process(List<E> nums) A programmer wants to use this method like this: // INSERT DECLARATIONS HERE output = process(input); Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply.)

The return type of process is definitely declared as a List, not an ArrayList, so A and D are incorrect. C is incorrect because the return type evaluates to List<Integer>, and that can't be assigned to a variable of type List<Number>. Of course, all these would probably cause a NullPointerException since the variables are still null—but the question only asked us to get the code to compile.

Not Correct

9. Given the proper import statement(s) and {code} What is the result?

For the sake of the exam, add() and offer() both add to (in this case) naturally sorted queues. The calls to poll() both return and then remove the first item from the queue, so the test fails.

Not Correct

10. Given the proper import statement(s) and {code} What is the result?

add() and offer() both add to the end of the deque. The calls to poll() and remove() both return and then remove the first item from the queue. The calls to peek() return the first item in the queue without removing it. The test fails because the first item is not "2"” (it’s been removed).

Not Correct

11. Given: {code} And these three fragments: I. Set s = new HashSet(); II. TreeSet s = new TreeSet(); III. LinkedHashSet s = new LinkedHashSet(); When fragments I, II, or III are inserted independently at line 7, which are true? (Choose all that apply.)

Elements of a TreeSet must in some way implement Comparable.

Not Correct

12. Given: {code} And these two fragments: I. public int hashCode() { return size/5; } II. // no hashCode method declared If fragment I or II is inserted independently at line 8, which are true? (Choose all that apply.)

While fragment II wouldn't fulfill the hashCode() contract (as you can see by the results), it is legal Java. For the purpose of the exam, if you don't override hashCode(), every object will have a unique hashcode.

Not Correct

13. (OCJPJ 6 only) Given the proper import statement(s) and: {code} Which are true? (Choose all that apply.)

After "g" is added, TreeSet s contains six elements and TreeSet subs contains three (b, c, d), because "g" is out of the range of subs. The first pollFirst() finds and removes only the "a". The second pollFirst() finds and removes the "b" from both TreeSets (remember they are backed). The final add() is in range of both TreeSets. The final contents are [c,c2,d,e,g] and [c,c2,d].


14. (OCJPJ 6 only) Given: {code} Which of the System.out.println statements will produce the output 1st after mango: p? (Choose all that apply.)

The ceilingKey() method's argument is inclusive. The floorKey() method would be used to find keys before the specified key. The firstKey() method's argument is also inclusive.


15. Given: {code} Which statement inserted independently at line 9 will compile? (Choose all that apply.)

A is incorrect because polymorphic assignments don't apply to generic type parameters. C and D are incorrect because they don't follow basic polymorphism rules.

Not Correct

16. Given: {code} What is the result?

Class Dog needs to implement Comparable in order for a TreeSet (which keeps its elements sorted) to be able to contain Dog objects.

Not Correct

17. Given: {code} Which are true? (Choose all that apply.)

First, the compareTo() method will reverse the normal sort. Second, the sort() is valid. Third, the binarySearch() gives –1 because it needs to be invoked using the same Comparator (o) as was used to sort the array. Note that when the binarySearch() returns an "undefined result," it doesn't officially have to be a –1, but it usually is.

Not Correct

18. Given: {code} Which of the following code fragments sorts the dogs in ascending order by name? (Choose all that apply.)

A uses the dogSorter comparator made from the DogSort Comparator class, and B uses the lambda expression equivalent of dogSorter. The argument to C is the class name for the Comparator rather than an instance. D uses the incorrect syntax for a lambda expression. E is almost right, but it sorts the dogs in the wrong order.