Welcome Guest | Sign in | Register

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);
}
}
A. das abcdef B. das abcdef das abcdef
C. das das abcdef abcdef D. das

Answer and Explanation

Answer: das abcdef

Explanation:
Implement 'equals' and 'hashCode' methods to get unique result in Set.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. Which collection class grows or shrinks its size and provides indexed access to its elements, but methods are not synchronized?
A. java.util.ArrayList B. java.util.List
C. java.util.HashSet D. java.util.Vector

Answer and Explanation

Answer: java.util.ArrayList

Explanation:
ArrayList provides an index to its elements and methods are not synchronized

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. If we do
ArrayList lst = new ArrayList();

What is the initial capacity of the ArrayList lst ?
A. 10 B. 8
C. 15 D. 12

Answer and Explanation

Answer: 10

Explanation:
Array List constructs an empty list with an initial capacity of ten.

public ArrayList(){
this(10);
}

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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() + " " );
}
}
}
A. one three two B. Runtime Exception
C. one three two one D. one two three

Answer and Explanation

Answer: one three two

Explanation:
The TreeSet assures no duplicate entries. It will return elements in natural order, which for Strings mean alphabetical order.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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() + " ");
}
}
A. Compile error B. Runtime Exception
C. 79 D. None of the above

Answer and Explanation

Answer: Runtime Exception

Explanation:
Without generics, the compiler does not know what type is appropriate for this
TreeSet, so it allows everything to compile. But at runtime the TreeSet will try to sort the
elements as they are added, and when it tries to compare an Integer with a String it will
throw a ClassCastException.

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be
cast to java.lang.Integer.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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() + " ");
}
}
A. Compile error : Integer can't be added B. newyork ca texas 11
C. newyork ca texas D. None of the above

Answer and Explanation

Answer: newyork ca texas 11

Explanation:
q was originally declared as Queue, But in show( ) method it is passed as an
untyped Queue. Nothing in the compiler or JVM prevents us from adding an Integer after
that.
If the show method signature is public static void show(Queue q) than you
can't add Integer, Only String allowed. But public static void show(Queue q) is untyped
Queue so you can add Integer.
Y poll( ) Retrieves and removes the head of this queue, or returns null if this queue is
empty.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. HashMap can be synchronized by _______ ?
A. Map m = Collections.synchronizeMap(hashMap); B. Map m = hashMap.synchronizeMap();
C. Map m = Collection.synchronizeMap(hashMap); D. None of the above

Answer and Explanation

Answer: Map m = Collections.synchronizeMap(hashMap);

Explanation:
HashMap can be synchronized by Map m = Collections.synchronizeMap(hashMap);

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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);
}
}
A. true B. false
C. Compilation fails with an error at line 3 D. Compilation fails with an error at line 4

Answer and Explanation

Answer: true

Explanation:
An array is always an instance of Object

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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?
A. Arrays.sort(s); B. s = new TreeSet(s);
C. Collections.sort(s); D. s = new SortedSet(s);

Answer and Explanation

Answer: s = new TreeSet(s);

Explanation:
If you store HashMap values into TreeSet then values will get sorted automatically,so here we are storing HashMap values into TreeSet. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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; i9. System.out.print(myObjects[i].toString());
10  System.out.print(" ");
11. }
What is the result?
A. Compilation fails due to an error in line 1. B. Compilation fails due to an error in line 7.
C. A ClassCastException occurs in line 7. D. A ClassCastException occurs in line 9.
E. The value of all four objects prints in natural order.

Answer and Explanation

Answer: A ClassCastException occurs in line 7.

Explanation:
Whenever you call Arrays.sort() method on some object then that class should implement Comparable interface. If not it will throw exception.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



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