java utilities Mcqs

Our collections of Multiple choice questions and answers focuses on study of java utilities. These questions are chosen from a collection of most authoritative and best reference books on Java. Our aim is to prepare an individual for competitive exams like NTS, GAT, ECAT, University and College entrance exams and various tests and job interviews. One should practice our Mcqs to assimilate java utilities topic comprehensively.

2.
What happens if we put a key object in a HashMap which exists?

The new object replaces the older object

The new object is discarded

The old object is removed from the map

It throws an exception as the key already exists in the map

4.
If two threads access the same hashmap at the same time, what would happen?

ConcurrentModificationException

NullPointerException

ClassNotFoundException

RuntimeException

5.
How to externally synchronize hashmap?

HashMap.synchronize(HashMap a);

HashMap a = new HashMap(); a.synchronize();

Collections.synchronizedMap(new HashMap<String, String>());

Collections.synchronize(new HashMap<String, String>());

6.
What is the output of below snippet?

public class Demo
{
  public static void main(String[] args)
  {
  Map sampleMap = new TreeMap();
  sampleMap.put(1, null); 
  sampleMap.put(5, null); 
  sampleMap.put(3, null); 
  sampleMap.put(2, null); 
  sampleMap.put(4, null); 
 
       System.out.println(sampleMap);
   }
}

{1=null, 2=null, 3=null, 4=null, 5=null}

{5=null}

Exception is thrown

{1=null, 5=null, 3=null, 2=null, 4=null}

7.
If large number of items are stored in hash bucket, what happens to the internal structure?

The bucket will switch from LinkedList to BalancedTree

The bucket will increase its size by a factor of load size defined

The LinkedList will be replaced by another hashmap

Any further addition throws Overflow exception

9.
How to remove duplicates from List?

HashSet<String> listToSet = new HashSet<String>(duplicateList);

HashSet<String> listToSet = duplicateList.toSet();

HashSet<String> listToSet = Collections.convertToSet(duplicateList);

HashSet<String> listToSet = duplicateList.getSet();

11.
When two threads access the same ArrayList object what is the outcome of the program?

Both are able to access the object

ConcurrentModificationException is thrown

One thread is able to access the object and second thread gets Null Pointer exception

One thread is able to access the object and second thread will wait till control is passed to the second one

12.
How is Arrays.asList() different than the standard way of initialising List?

Both are same

Arrays.asList() throws compilation error

We cannot access the list returned using Arrays.asList()

13.
What is the difference between length() and size() of ArrayList?

Length() and size() return the same value

Length() is not defined in ArrayList

Size() is not defined in ArrayList

Length() returns the capacity of ArrayList and size() returns the actual number of elements stored in the list

0Shares
0
Scroll to Top