Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Practice Quiz

Total questions: 30

Worksheet time: 16mins

Name
Class
Date
1.

Which of these classes provides the specified features?


-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.

a)

LinkedList

b)

TreeMap

c)

HashSet

d)

LinkedHashMap

e)

HashMap

2.

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

a)

List

b)

Sorted List

c)

Set

d)

Sorted Map

3.

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

a)

Integer

b)

Double

c)

Boolean

d)

Collections Object

4.

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.

5.

-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

6.
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

7.

-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

8.

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

9.
a)

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

b)

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

c)

Line 11 : Genetates NullPointer Exception

d)

Line12: Generates NullPointerException

10.

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.

11.

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

12.

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

13.

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

14.

Which of the following interfaces are not a part of the Java's Collection Framework?

a)

SortedList

b)

Set

c)

SortedMap

d)

List

15.

Which of the following interfaces must contain a unique element?

a)

List

b)

Set

c)

Array

d)

Collection

16.

Which of the following statements are true with respect to a stack?

a)

A Stack is implemented in the "first-in, first-out" (FIFO) order.

b)

A Stack is implemented in the "first-in, last-out" (FILO) order.

c)

A Stack supports random access.

d)

None of these.

17.

Which of the following classes implement the Queue interface?

a)

HashSet

b)

TreeSet

c)

LinkedList

d)

None of these

18.

What is the list nums if it is initially [5, 3, 1] and the following code is executed?

nums.add(6);

nums.add(0, 4);

nums.remove(1);

a)

[4, 3, 1, 6]

b)

[5, 3, 1, 6]

c)

[4, 3, 6]

d)

[4, 5, 3, 6]

19.

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)

8

c)

9

d)

0

20.

What is the output of the following code:

Stack <String> r=new Stack<>();

r.push("GEC");

r.push("CSE");

System.out.println(r instanceof Collection)

a)

False

b)

0

c)

True

d)

1

21.

Which of the following statement is correct

a)

Collection is an interface and Collections is a Class

b)

Collection is a class and Collections is an interface

c)

Both Collection and Collections are classes

d)

Both Collection and Collections are Interfaces

22.

What is the output of the following code:

HashMap<Integer,String> r=new HashMap<Integer,String>();

r.put(1,"hello");

r.put(2,"hai");

System.out.println(r instanceof Collection);

a)

false

b)

0

c)

true

d)

1

23.

How to display HashMap keys

a)

using keys() method

b)

using entrySet() method

c)

using keySet() method

d)

using values() method

24.

Maps and Sets are Synchronized

a)

True

b)

False

c)

We cant predict

25.

which of the following is wrapper class in java

a)

Float

b)

Integer

c)

Double

d)

all of above

26.

Which one of the following statement is not true about the collection interface?

a)

all methods defined in set interface are also defined in collection interface

b)

list interface extends collection interface

c)

set interface extends collection interface

d)

all methods defined in list interface are also defined in collection interface

27.

which one of the following is a legal decelration of a two- dimensional array of integers

a)

int[] a[] = new int[5][];

b)

int[5][5] a = new int[][];

c)

int a = new int[5,5];

d)

int[][] a = new int[][5];

28.

which interface needs to be implemented in order to make an object seralized

a)

clonnable

b)

remote

c)

seralizable

d)

none of the above

29.

which one of the following have elements in insertion order

a)

sorted map

b)

hashmap

c)

linkedhashmap

d)

treemap

30.

Cat class extends Predator class.

Which of the following must be true?

a)

A Cat object has access to all Predator variables and methods.

b)

A Predator object has access to all Cat variables and methods.

c)

Cat c = new Predator();

causes an error.

d)

Predator p = new Cat();

p has access to Cat methods and uses the Predator implementations.