Welcome Guest | Sign in | Register

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());
E. String s = (String)Old.get0(new LinkedList());
A. A,B,C B. A,D,E
C. A,B,D D. C,D,E

Answer and Explanation

Answer: A,D,E

Explanation:
Just normal flow of execution.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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]?
A. Set set = new TreeSet(); B. Set set = new HashSet();
C. Set set = new SortedSet(); D. List set = new SortedList();
E. Set set = new LinkedHashSet();

Answer and Explanation

Answer: Set set = new TreeSet();

Explanation:
Whenever you add elements into TreeSet it will sort automatically, so if you want data to be sorted then add into TreeSet. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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?
A. A, B, C B. B, C, A
C. Compilation fails. D. The code runs with no output.
E. An exception is thrown at runtime.

Answer and Explanation

Answer: B, C, A

Explanation:
In LinkedList however you add, it will display as it is. So option B is correct. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Given:
1. import java.util.*;
2. public class PQ {
3. public static void main(String[] args) {
4. PriorityQueue pq = new PriorityQueue();
5. pq.add("carrot");
6. pq.add("apple");
7. pq.add("banana");
8. System.out.println(pq.poll() + ":" + pq.peek());
9. }
10. }

What is the result?
A. apple:apple B. carrot:apple
C. apple:banana D. banana:apple
E. carrot:carrot

Answer and Explanation

Answer: apple:banana

Explanation:
peek( ) function will retrieve head element from the queue.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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 hs = new HashSet();
7. WrappedString ws1 = new WrappedString("aardvark");
8. WrappedString ws2 = new WrappedString("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
12. System.out.println(hs.size()); } }

What is the result?
A. 01 B. 1
C. 2 D. 3
E. 4

Answer and Explanation

Answer: 4

Explanation:
Just normal flow of execution.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Given:
1. import java.util.*;
2.
3. public class LetterASort{
4. public static void main(String[] args) {
5. ArrayList strings = new 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. }

 What is the result?
A. Compilation fails B. aAaA aAa AAaa AaA
C. AAaa AaA aAa aAaA D. AaA AAaa aAaA aAa
E. aAa AaA aAaA AAaa

Answer and Explanation

Answer: AAaa AaA aAa aAaA

Explanation:
 just normal flow of execution.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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?
A. public int compareTo(Object o){/*more code here*/} B. public int compareTo(Score other){/*more code here*/}
C. public int compare(Score s1,Score s2){/*more code here*/} D. public int compare(Object o1,Object o2){/*more code here*/}

Answer and Explanation

Answer: public int compareTo(Score other){/*more code here*/}

Explanation:
In a Comparable interface there is a method called compareTo ( ) using that we can sort the element depending on the attribute value.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.