MCQ

Java MCQ – Collections – Part 3

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Collections”.
 

1. Which class stores items as a key-value pair?

A Arraylist

B LinkedHashSet

C TreeMap

D TreeSet

C
Only Maps store elements as a key-value pair.

 

 

2. Which is faster and uses less memory?

A ListEnumeration

B Iterator

C ListIterator

D Enumeration

D
Enumeration is very basic and meets basic needs.

 

 

3. What is the output of the following code?
import java.util.*;

public class Main 
{
	public static void main(String args[]) 
	{
		int tab[] = new int [6];
		
		for (int i = 6; i > 0; i--)
			tab[6-i] = i;
		
		Arrays.fill(tab, 1, 5, 0);
		
		for (int i = 0; i < 6 ; i++)
			System.out.print(tab[i]);
	}
}

A 611110

B 543210

C 654321

D 600001

D
In line 10 the array contain 6,5,4,3,2,1 but when the Arrays.fill(tab, 1, 5, 0); method is called, it fills the index slot starting from 1 to 5 with the value 0, so the array becomes 6,0,0,0,0,1.

 

 

4. Which of these is an incorrect form of using the max() method to get the maximum element?

A max(Collection c, Comparator comp)

B max(Collection c)

C max(Comparator comp)

D max(List c)

C
It is not allowed to call the max() method only with a comparator, we must also specify the collection in the parameters. The correct form is max(Collection c, Comparator comp)

 

 

5. Which of these methods can mix all the elements of a list?

A rand()

B srand()

C randomize()

D shuffle()

D
Collections.shuffle(list) method; mixes all the elements of a list. Example:

import java.util.*; 
  
public class Main { 
    public static void main(String[] args) 
    { 
        ArrayList<String> list = new ArrayList<String>(); 
		
        list.add("A"); 
        list.add("B"); 
        list.add("C"); 
 
        System.out.println("Before mixing the elements: "+list);
		
        Collections.shuffle(list); 
		
        System.out.println("After mixing the elements: "+list);
    } 
}

Output:

Before mixing the elements: [A, B, C]
After mixing the elements: [C, A, B]

 

 

6. Which of the following methods can convert an object to a list?

A SetList()

B ConvertList()

C singletonList()

D CopyList()

D
singletonList() returns the object as an immutable list. This is a simple way to convert a single object to a list. This was added in Java version 2.0.

 

 

7. Default capacity of a vector is ___

A 10

B 12

C 8

D 16

A
Default capacity of a vector is 10

 

 

8. Which one is best suited for a multi-threaded environment?

A WeakHashMap

B Hashtable

C HashMap

D ConcurrentHashMap

D
The ConcurrentHashMap class of the Collections framework provides a thread-safe Map. In other words, several threads can access the map at the same time without affecting the consistency of the entries in a map.

 

 

9. Which of the following interface does NOT implement the Collection interface?

A List

B Map

C Set

D None of the above

B
Map interface does NOT implement the Collection interface.

 

 

10. Default capacity of an ArrayList is ___

A 12

B 10

C 8

D 16

B
Default capacity of an ArrayList is 10

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *