- Home
- Programming
- OCP Java SE8
41.
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
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
When declaring a class that uses generics, you must specify a name for the formal type parameter. Java uses the standard rules for naming a variable or class. A question mark is not allowed in a variable name, making I incorrect. While it is common practice to use a single uppercase letter for the type parameter, this is not required. It certainly isn’t a good idea to use existing class names like the News class being declared here or the Object class built into java. However, this is allowed, and Option C is correct. |
42.
Which method is available on both List and Stream implementations?
- A.filter()
- B.forEach()
- C.replace()
- D.sort()
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Option A is incorrect because the filter() method is available on Stream , but not List . Option C is incorrect because the replace() method is available on List , but not Stream . Option D is tricky because there is a sort() method on List and a sorted() method on Stream . These are different method names though, so Option D is incorrect. Option B is the answer because both interfaces have a forEach() method. |
43.
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 book’s 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
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
Notice how there is unnecessary information in this description. The fact that patrons select books by name is irrelevant. The checkout line is a perfect example of a double-ended queue. We need easy access to one end of the queue for patrons to add themselves to the queue. We also need easy access to the other end of the queue for patrons to get off the queue when it is their turn. The book lookup by ISBN is a lookup by key. We need a map for this. A HashMap is probably better here, but it isn’t a choice. So the answer is Option A, which does include both a double-ended queue and a map |
44.
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());
Collection<String> c = new ____________<>();
c.add("pen");
c.remove("pen");
System.out.println(c.isEmpty());
- A.Option A
- B.Option B
- C.Option C
- D.Option D
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Java talks about the collections framework, but the Map interface does not actually implement the Collection interface. TreeMap has different methods than ArrayDeque and TreeSet . It cannot fill in the blank, so Option B is correct. |
45.
Suppose we want to implement a Comparator<String> so that it sorts the longeststrings 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();
}
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Options C and D are incorrect because the method signature is incorrect. Unlike the equals() method, the method in Comparator takes the type being compared as the parameters when using generics. Option A is a valid Comparator . However, it sorts in ascending order by length. Option B is correct. If s1 is three characters and s2 is one character, it returns -2 . The negative value says that s1 should sort first, which is correct, because we want the longest String first. |