File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Methods question

 
matthew meltzer
Greenhorn
Posts: 24
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic