Home > Java Programming > Collections > Questions and Answers
01. |
What is the output of below code? public class Bean{ private String str; Bean(String str ){ this.str = str; } public String getStr() { return str; } public boolean equals(Object o){ if (!(o instanceof Bean)) { return false; } return ((Bean) o).getStr().equals(str); } public int hashCode() { return 12345; } public String toString() { return str; } } import java.util.HashSet; public class Test { public static void main(String ... sss) { HashSet myMap = new HashSet(); String s1 = new String("das"); String s2 = new String("das"); Bean s3 = new Bean("abcdef"); Bean s4 = new Bean("abcdef"); myMap.add(s1); myMap.add(s2); myMap.add(s3); myMap.add(s4); System.out.println(myMap); } } | |||||||||||
|
02. | Which collection class grows or shrinks its size and provides indexed access to its elements, but methods are not synchronized? | |||||||||||
|
03. |
If we do ArrayList lst = new ArrayList(); What is the initial capacity of the ArrayList lst ? | |||||||||||
|
04. |
What is the output for the below code? import java.util.Iterator; import java.util.TreeSet; public class Test { public static void main(String... args) { TreeSet s1 = new TreeSet(); s1.add("one"); s1.add("two"); s1.add("three"); s1.add("one"); Iterator it = s1.iterator(); while (it.hasNext() ) { System.out.print( it.next() + " " ); } } } | |||||||||||
|
05. |
What is the output for the below code? import java.util.Iterator; import java.util.Set; import java.util.TreeSet; public class Test { public static void main(String... args) { Set s = new TreeSet(); s.add("7"); s.add(9); Iterator itr = s.iterator(); while (itr.hasNext()) System.out.print(itr.next() + " "); } } | |||||||||||
|
06. |
What is the output for the below code? import java.util.LinkedList; import java.util.Queue; public class Test { public static void main(String... args) { Queue q = new LinkedList(); q.add("newyork"); q.add("ca"); q.add("texas"); show(q); } public static void show(Queue q) { q.add(new Integer(11)); while (!q.isEmpty ( ) ) System.out.print(q.poll() + " "); } } | |||||||||||
|
07. | HashMap can be synchronized by _______ ? | |||||||||||
|
08. |
What is the output for the below code ? public class Test {
public static void main(String... args) { int [] index = new int[5]; System.out.println(index instanceof Object); } } | |||||||||||
|
09. |
1. HashMap props = new HashMap(); 2. props.put("key45", "some value"); 3. props.put("key12", "some other value"); 4. props.put("key39", "yet another value"); 5. Set s = props.keySet(); 6. // insert code here What, inserted at line 6, will sort the keys in the props HashMap? | |||||||||||
|
10. |
1. Object [] myObjects = { 2. new Integer(12), 3. new String("foo"), 4. new Integer(5), 5. new Boolean(true) 6. }; 7. Arrays.sort(myObjects); 8. for(int i=0; i 10 System.out.print(" "); 11. } What is the result? | |||||||||||||||
|