Forums Register Login

Generic Methods question

+Pie Number of slices to send: Send
Q8: Which collection types, when inserted at (1), will result in a generic method that will compile without errors?

public static <T> T justDoIt(______/* (1) INSERT TYPE HERE */ ____ <T> collection){
return collection.poll();
}

There may be more than one correct answer.

(a) PriorityQueue
(b) LinkedList
(c) Queue
(d) TreeSet
(e) LinkedHashSet



Here is the question. It is part of my homework assignment for chapter 7 in the SCJP study guide. I keep looking over the book but cannot figure it out.... Right now I am leaning towards A and B for the answer....Can anyone help me understand the problem and figure out which answers are correct?
1
+Pie Number of slices to send: Send
Well, the most reliable way to find out which of those will compile is to ask the compiler. Try them out, see whether you get any errors, and what errors they are.

The error messages you get may well make it clear to you why the wrong answers are wrong. But the obvious question to ask for me is this: which of those types has a poll() method?
1
+Pie Number of slices to send: Send
 

matthew meltzer wrote:Q8: Which collection types, when inserted at (1), will result in a generic method that will compile without errors?

public static <T> T justDoIt(______/* (1) INSERT TYPE HERE */ ____ <T> collection){
return collection.poll();
}

There may be more than one correct answer.

(a) PriorityQueue
(b) LinkedList
(c) Queue
(d) TreeSet
(e) LinkedHashSet



Here is the question. It is part of my homework assignment for chapter 7 in the SCJP study guide. I keep looking over the book but cannot figure it out.... Right now I am leaning towards A and B for the answer....Can anyone help me understand the problem and figure out which answers are correct?



poll() method resides in Queue interface, and both LinkedList and PriorityQueue implements Queue, so answer is a,b,c.
+Pie Number of slices to send: Send
 

matthew meltzer wrote:Q8: Which collection types, when inserted at (1), will result in a generic method that will compile without errors?

public static <T> T justDoIt(______/* (1) INSERT TYPE HERE */ ____ <T> collection){
return collection.poll();
}

There may be more than one correct answer.

(a) PriorityQueue
(b) LinkedList
(c) Queue
(d) TreeSet
(e) LinkedHashSet



Here is the question. It is part of my homework assignment for chapter 7 in the SCJP study guide. I keep looking over the book but cannot figure it out.... Right now I am leaning towards A and B for the answer....Can anyone help me understand the problem and figure out which answers are correct?


matthew, this is your home work to find out the correct answers. These type of Question you will see on the certification Exam. The main motive of these questions is to confuse you, but if your concepts are clear only then you can answer, if you are unable to answer its better if you re-re-re-re revise your notes that is what the writers of this book wants.So, its better if you ask doubts instead of a question straight from book.
Hot dog! An advertiser loves us THIS much:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 809 times.
Similar Threads
Collections
Study Resource for my weak areas?
Collections and the order of returning elements
Priority Queue Ordering
order of elements returned by Iterator in Set
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:28:02.