Collections Framework In Java - Study Mode
[#156] What will be the output of the following Java program? class Output
{
public static void main(String args[])
{
ArrayList obj = new ArrayList()
obj.add("A")
obj.add("D")
obj.ensureCapacity(3)
obj.trimToSize()
System.out.println(obj.size())
}
}
Correct Answer
(B) 2
[#157] What will be the output of the following Java code snippet? import java.util.*
class Linkedlist
{
public static void main(String args[])
{
LinkedList obj = new LinkedList()
obj.add("A")
obj.add("B")
obj.add("C")
obj.addFirst("D")
System.out.println(obj)
}
}
Correct Answer
(D) [D, A, B, C]
[#158] What will be the output of the following Java program? import java.util.*
class Maps
{
public static void main(String args[])
{
HashMap obj = new HashMap()
obj.put("A", new Integer(1))
obj.put("B", new Integer(2))
obj.put("C", new Integer(3))
System.out.println(obj.get("B"))
}
}
Correct Answer
(B) 2
[#159] What will be the output of the following Java program? import java.util.*
class Collection_Algos
{
public static void main(String args[])
{
LinkedList list = new LinkedList()
list.add(new Integer(2))
list.add(new Integer(8))
list.add(new Integer(5))
list.add(new Integer(1))
Iterator i = list.iterator()
Collections.reverse(list)
Collections.sort(list)
while(i.hasNext())
System.out.print(i.next() + " ")
}
}
Correct Answer
(C) 1 2 5 8
[#160] What is the difference between Queue and Stack?
Correct Answer
(A) Stack is LIFO
Queue is FIFO