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.

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

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





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>());

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); 

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


Exception is thrown

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

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

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();

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

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()

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

Scroll to Top