Home > Java Programming > Collections > Questions and Answers
01. |
1. import java.util.*; 2. public class Old { 3. public static Object get0(List list) { 4. return list.get(0); 5. } 6. } Which three will compile successfully? (Choose three.)
A. Object o = Old.get0(new LinkedList()); B. Object o = Old.get0(new LinkedList()); C. String s = Old.get0(new LinkedList D. Object o = Old.get0(new LinkedList | |||||||||||
|
02. |
Given: 1. import java.util.*; 2. public class Example { 3. public static void main(String[] args) { 4. // insert code here 5. set.add(new Integer(2)); 6. set.add(new Integer(1)); 7. System.out.println(set); 8. } 9. } Which code, inserted at line 4, guarantees that this program will output [1, 2]? | |||||||||||||||
|
03. |
Given: 1. public static Collection get() { 2. Collection sorted = new LinkedList(); 3. sorted.add("B"); sorted.add("C"); sorted.add("A"); 4. return sorted; 5. } 6. public static void main(String[] args) { 7. for (Object obj: get()) { 8. System.out.print(obj + ", "); 9. } 10. } What is the result? | |||||||||||||||
|
04. |
Given: 1. import java.util.*; 2. public class PQ { 3. public static void main(String[] args) { 4. PriorityQueue 5. pq.add("carrot"); 6. pq.add("apple"); 7. pq.add("banana"); 8. System.out.println(pq.poll() + ":" + pq.peek()); 9. } 10. } | |||||||||||||||
|
05. |
Given: 1. import java.util.*; 2. public class WrappedString { 3. private String s; 4. public WrappedString(String s) { this.s = s; } 5. public static void main(String[] args) { 6. HashSet | |||||||||||||||
|
06. |
Given: 1. import java.util.*; 2. 3. public class LetterASort{ 4. public static void main(String[] args) { 5. ArrayList 6. strings.add("aAaA"); 7. strings.add("AaA"); 8. strings.add("aAa"); 9. strings.add("AAaa"); 10. Collections.sort(strings); 11. for (String s : strings) { System.out.print(s + " "); } 12. } 13. } | |||||||||||||||
|
07. |
1. public class Score implements Comparable 2. private int wins, losses; 3. public Score(int w, int l) { wins = w; losses = l; } 4. public int getWins() { return wins; } 5. public int getLosses() { return losses; } 6. public String toString() { 7. return "<" + wins + "," + losses + ">"; 8. } 9. // insert code here 10.} Which method will complete this class? | |||||||||||
|