wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

collection

Total questions: 64

Worksheet time: 43mins

Name
Class
Date
1.

-Contains no duplicate elements.

-Can contain at most one Null value.

-Elements are not key-value pairs.

-Accessing an element can be almost as fast as performing a similar operation on an array.


Which of these classes provides the specified features?

a)

LinkedList

b)

TreeMap

c)

HashSet

d)

LinkedHashMap

e)

HashMap

2.

Which of the following statement about Set is true?

a)

A HashSet cannot have a null value.

b)

Set allows duplicate values.

c)

Set allows null values but only single occurrence.

d)

Set extends the Java.util.collection interface.

3.

-Entries are organized as key/value pairs.

-Duplicate entries replace old entries.

-Entries are sorted using a Comparator or the Comparable interface.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

SortedSet

e)

SortedMap

4.

-Entries are organized as key/value pairs.

-Duplicate entries replace old entries.

-Entries are sorted using a Comparator or the Comparable interface.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

SortedSet

e)

SortedMap

5.
a)

{1=C, 2=JavaEE, 4=Python, 3=PHP}

b)

{1=C, 2=JavaSE, 3=Python, 4=PHP}

c)

{1=C, 2=JavaEE, 3=Python, 4=PHP}

d)

{1=C, 2=JavaSE,2=JavaEE, 3=Python, 4=PHP}

e)

NullPointerException

6.
a)

{null=null, a=null, b=JavaSE, c=PHP, d=Python}

b)

{a=null, b=JavaSE, c=PHP, d=Python}

c)

{b=JavaSE, c=PHP, d=Python}

d)

Compilation Fails

7.
a)

4

b)

5

c)

6

d)

12

8.

-Entries are not organized as key/value pairs.

-Duplicate entries are rejected.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

None of the above

9.
a)

[Computer, Network, Networks, Programming, Security]

b)

[Computer, Programming, Network, Networks, Programming, Security]

c)

[Computer, Programming, Computer, Networks, Network, Security]

d)

[Computer, Networks, Network, Programming, Security]

10.

Which of the following is/are correct statements?

a)

HashSet class implements the Set interface

b)

HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet.

c)

HashSet permits to have a single null value.

d)

HashMap does not allow duplicate keys however it allows to have duplicate values.

e)

HashMap and HashSet are threadsafe

11.

Which of the following statement(s) is/are TRUE?

a)

Both HashMap and HashSet are not synchronized.

b)

HashMap does not allow duplicate keys however it allows to have duplicate values.

c)

HashMap permits single null key and any number of null values.

d)

Both HashMap and HashSet are synchronized.

12.
a)

{"Computer", "Programming","Networks","null"}

b)

{"Computer", "Programming","Networks",null,"null"}

c)

Line 11 : Genetates NullPointer Exception

d)

Line12: Generates NullPointerException

13.

Which of the following statement(s) is/are FALSE?

a)

TreeSet does not preserve the insertion order of elements but elements are sorted by keys.

b)

Objects in a TreeSet are stored in a sorted and ascending order.

c)

TreeSet does not allow to insert Heterogeneous objects. It will throw classCastException at Runtime if trying to add hetrogeneous objects.

d)

Comparator is note provided for TreeSet.

14.

Which of the following is/are FALSE about HashMap in java?

a)

It provides the basic implementation of Map interface of Java.

b)

HashMap doesn’t allow duplicate keys but allows duplicate values.

c)

HashMap allows null key also but only once and multiple null values.

d)

Unsynchronized.

e)

Synchronized

15.

Which of the following is/ are True ?

a)

Set is a group of ordered objects without duplicates

b)

Map is essentially a set of (key,value) pairs with unique keys, supporting a lookup operation to find the value associated with a given key

c)

List is a sequence of objects, with a distinguished first object, and in which each object has a unique successor.

d)

Bag is a group of unordered objects allowing duplicates

16.

Which statement(s) are false about maps in java?

a)

A Map cannot contain duplicate keys and each key can map to at most one value.

b)

A Map is an object that maps keys to values, or is a collection of attribute-value pairs.

c)

A Map cannot contain duplicate keys and each key can map to at more than one value.

d)

A Map is an object that maps functions to values, or is a collection of attribute-value pairs.

17.

List listNames = new ArrayList();

listNames.add("Tom");

listNames.add("Mary");

listNames.add("Peter");


String name2 = (String) listNames.get(1);

System.out.println(name2);

What is the output of the above code

a)

Tom

b)

Mary

c)

Peter

d)

raises Exception

18.

Which of the following statement is not True about Generics in java?

a)

Generics are features of the Java programming language that allow programmers to write parameterized code.

b)

With generics, the compiler can not detect violations at compile time, thus eliminating potential bugs.

c)

Due to the demand of safer code, generics were added to the Java programming language.

d)

generics allow you to write generic code

e)

Stronger type checks at compile time

19.

When do we use sets in java program?

a)

To allow null and duplicate elements

b)

You want to store elements distinctly without duplication, or unique elements.

c)

to allow t contain duplicate keys and each key can map to at most one value

d)

To perform lookups by keys

20.

What will happen if an element already present in the Set is added again?

a)

Run time error will be generated

b)

No error, the element will be added once again to the set

c)

Compile time error

d)

A boolean value of false will be returned, and the value will not be added to the Set

21.

What is Collection in Java?

a)

A group of objects

b)

A group of classes

c)

A group of interfaces

d)

None of the mentioned

22.

Which of these interface is not part of Java’s collection framework?

a)

List

b)

Sorted List

c)

Set

d)

Sorted Map

23.

 What will be the output of the  Java program?

a)

{0, 1, 3, 4}

b)

{0, 1, 2, 4}

c)

{0, 1, 2, 3, 4}

d)

{0, 0, 0, 3, 4}

24.

Which of these return type of hasNext() method of an iterator?

a)

Integer

b)

Double

c)

Boolean

d)

Collections Object

25.

Which of these exceptions is thrown by remover() method?

a)

IOException

b)

SystemException

c)

ObjectNotFoundExeception

d)

IllegalStateException

26.

Which of these methods deletes all the elements from invoking collection?

a)

clear();

b)

reset();

c)

delete();

d)

remove();

27.

Which of these interface declares core method that all collections will have?

a)

Collection

b)

EventListner

c)

Comparator

d)

Set

e)

List

28.

Which of these is an incorrect form of using method max() to obtain maximum element?

a)

max(Collection c)

b)

max(Collection c, Comparator comp)

c)

max(Comparator comp)

d)

max(List c)

29.

Which of these methods can convert an object into a List?

a)

SetList()

b)

ConvertList()

c)

CopyList()

d)

singletonList()

30.

Which of these is true about unmodifiableCollection() method?

a)

unmodifiableCollection() returns a collection that cannot be modified.

b)

unmodifiableCollection() method is available only for List and Set.

c)

unmodifiableCollection() is defined in Collection class.

d)

None of the mentioned.

31.

Which of these is static variable defined in Collections?

a)

EMPTY_SET

b)

EMPTY_LIST

c)

EMPTY_MAP

d)

All of the mentioned

32.

What implementation of Iterator can traverse a collection in both directions?

a)

Iterator

b)

ListIterator

c)

SetIterator

d)

MapIterator

33.

Which is faster and uses less memory?

a)

ListEnumeration

b)

Iterator

c)

Enumeration

d)

ListIterator

34.

What will be output of given code

a)

a followed by concurrentModification Exception

b)

a b c

c)

a b

d)

a c

35.

Which interface does java.util.Hashtable implement?

a)

Java.util.Map

b)

Java.util.List

c)

Java.util.HashTable

d)

Java.util.Collection

36.

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order.

Which interface provides that capability?

a)

java.util.Map

b)

java.util.Set

c)

java.util.List

d)

java.util.Collection

37.

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order.

Which interface provides that capability?

a)

java.util.Map

b)

java.util.Set

c)

java.util.List

d)

java.util.Collection

38.

Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

a)

java.util.HashSet

b)

java.util.LinkedHashSet

c)

java.util.List

d)

java.util.ArrayList

39.

The default capacity of a Vector is:

a)

10

b)

12

c)

8

d)

0

40.

The Comparable interface contains which called?

a)

toCompare

b)

compare

c)

compareTo

d)

compareWith

41.

Find the output

public class TreeMapTest {

public static void main(String args[]) {

Map<Integer, String> m = new TreeMap<Integer, String>();

m.put(11, "audi");

m.put(null, null);

m.put(11, "bmw");

m.put(null, "ferrari");

System.out.println(m.size());

System.out.println(m);

}

a)

Nullpointer exception

b)

compiler error

c)

2

d)

Null value

42.

public class MyClass {

public static void main(String args[]) {

Map<String, String> hashMap = new HashMap<String, String>();

hashMap.put(new String("a"), "audi");

hashMap.put(new String("a"), "ferrari");

System.out.println(hashMap);

}

}

a)

a=audi

b)

a=audi

a=ferrari

c)

a=ferrari

d)

Run time Error

43.

Deque and Queue are derived from

a)

Abstract class

b)

Collection

c)

Abstactcollection

d)

List

44.

What will be output of given code

a)

1 2 3

b)

1 followed by exception

c)

Compile time error

d)

run time error

45.

Which is best suited to a multi-threaded environment?

a)

WeakHashMap

b)

Hashtable

c)

HashMap

d)

ConcurrentHashMap

46.

TreeMap implements?

a)

Dictionary

b)

HashMap

c)

AbstractMap

d)

NavigableMap

47.

Which of these is synchronized?

a)

TreeMap

b)

HashMap

c)

Hashtable

d)

All of the above

48.

TreeMap

a)

doesn't allow null key

b)

allow many null values

c)

Both

d)

allow many null key

49.

How can you sort given HashMap on basis of values

a)

Implement Comparator interface and override its compare method

b)

It is not possible

c)

Implement Comparator interface and override its compareTo method

d)

Implement Comparator interface and override its comparable method

50.

What does Collections.sort internally uses when number of elements are less than 7?

a)

Insertion sort

b)

Merge sort

c)

Quick sort

d)

None

51.

What does Collections.sort internally uses when number of elements are greater than 7?

a)

Insertion sort

b)

Merge sort

c)

Quick sort

d)

None

52.

Contains no duplicate elements

Elements are not key-value pairs

Always unique elements

a)

LinkedList

b)

TreeMap

c)

HashSet

d)

HashMap

53.

Which of the following is/are NOT correct statements?

a)

HashSet class implements the Set interface

b)

HashSet permits to have a single null value.

c)

HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet.

d)

HashMap does not allow duplicate keys however it allows to have duplicate values.

e)

HashMap and HashSet are threadsafe

54.

What does it mean for a list to be "dynamic" in ArrayList?

a)

You can easily add and remove elements

b)

It may contain multiple types of values

c)

It has a set size once it has been created

d)

You can access it from anywhere in the program

55.

How do you get the number of elements in an ArrayList called "list" in Java?

a)

list.size();

b)

list.length();

c)

list.count();

d)

list.getSize();

56.

The _________ method will return the total elements in an ArrayList.

a)

total()

b)

size()

c)

length()

d)

length

57.

Which statement below is the correct way to retrieve the first element in the nums ArrayList?

a)

nums.get(0)

b)

nums(0)

c)

nums[0]

d)

nums[1]

58.

Given the nums ArrayList with the values [5, 4, 3, 2], what statement below removes the value 3 from the list?

a)

nums.remove(2)

b)

nums.remove(3)

c)

nums.get(2) = null

d)

nums.remove(1)

59.

What will print when the following code executes?

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(7);

list.add(8);

list.add(9);

System.out.println(list.remove(0));

a)

7

b)

9

c)

0

d)

8

60.

Which statement is the correct declaration and initialization of an ArrayList of String values?

a)

ArrayList<String> name;

name = new ArrayList<String>();

b)

ArrayList<String> name;

name = ArrayList<String>();

c)

ArrayList name;

name = new ArrayList<String>();

d)

String<ArrayList> name;

name = new String<ArrayList>();

61.

public static void main (String[] args) {

int arr1[] = {1, 2, 3};

int arr2[] = {1, 2, 3};

if (arr1 == arr2)

System.out.println("Same");

else

System.out.println("Not same");

}}

a)

same

b)

not same

c)

error

62.

public static void main (String[] args)

{

int arr1[] = {1, 2, 3};

int arr2[] = {1, 2, 3};

if (Arrays.equals(arr1, arr2))

System.out.println("Same");

else

System.out.println("Not same");

}

}

a)

Same

b)

Not Same

c)

Error

63.

What is the length of the following array: byte[] data = { 12, 34, 9, 0, -62, 88 };

a)

5

b)

6

c)

4

d)

7

64.

_________________ is a collection of elements used to store the same type of data.

a)

Array

b)

ArrayList

c)

Case

d)

Loop