Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Unit 4 - io, collections_java

Total questions: 30

Worksheet time: 18mins

Name
Class
Date
1.

What method is used for reading bytes from a file in Java?

a)

A) readBytes()

b)

B) fileRead()

c)

C) read()

d)

D) getBytes()

2.

Which of the following is not a method of the Set interface?

a)

A) add()

b)

B) get()

c)

C) remove()

d)

D) iterator()

3.

Which class would you use to write character streams in Java?

a)

A) FileOutputStream

b)

B) BufferedWriter

c)

C) InputStreamReader

d)

D) ByteBuffer

4.

If you want to ensure that a collection retains the order of its elements as they were added, which implementation would be most appropriate?

a)

A) HashSet

b)

B) LinkedHashSet

c)

C) TreeSet

d)

D) PriorityQueue

5.

To remove duplicates from a list while maintaining the element order, which collection type should you convert the list into?

a)

A) HashSet

b)

B) LinkedHashSet

c)

C) TreeSet

d)

D) PriorityQueue

6.

You need to implement a FIFO (First-In-First-Out) structure. Which collection interface provides this capability?

a)

A) Set

b)

B) List

c)

C) Queue

d)

D) Map

7.

You have a SortedSet of timestamps. You need to retrieve and remove the earliest timestamp. Which methods should you use?

a)

A) getFirst() and removeFirst()

b)

B) first() and remove()

c)

C) lowest() and dequeue()

d)

D) earliest() and delete()

8.

If you are using a BufferedReader to read text from a stream, which method would you use to read a line of text?

a)

A) readLine()

b)

B) readText()

c)

C) getText()

d)

D) nextLine()

9.

When would you prefer ArrayList over LinkedList for implementing a list in Java?

a)

A) When frequent addition and removal of elements is required.

b)

B) When memory usage is more of a concern than performance.

c)

C) When there is a need to frequently update elements in the middle of the list.

d)

D) When fast random access to elements is required.

10.

In Java, which of these classes are used to read characters from a file?

a)

A) FileInputStream

b)

B) FileReader

c)

C) BufferedInputStream

d)

D) DataInputStream

11.

Which List implementation provides constant time performance for the get and set methods?

a)

A) LinkedList

b)

B) CopyOnWriteArrayList

c)

C) Stack

d)

D) ArrayList

12.

If you have a large List of objects that will be accessed frequently for read and less frequently for insertions and deletions, which implementation is preferred?

a)

A) LinkedList

b)

B) CopyOnWriteArrayList

c)

C) ArrayList

d)

D) Vector

13.

You have a requirement to ensure that no duplicate elements are stored and that all elements remain in ascending order. Which Set implementation would you use?

a)

A) HashSet

b)

B) LinkedHashSet

c)

C) TreeSet

d)

D) ConcurrentSkipListSet

14.

You are given a LinkedList to manage. You need to frequently add elements at the beginning and end of the list. Which methods do you utilize to optimize performance?

a)

A) addFirst() and addLast()

b)

B) push() and append()

c)

C) insertBeginning() and insertEnd()

d)

D) enqueue() and stack()

15.

To quickly search for an element in a List, which condition should be met regarding the list's organization?

a)

A) The list should be unordered.

b)

B) The list should be sorted.

c)

C) The list should be a LinkedList.

d)

D) The list should be synchronized.

16.

What advantage does a LinkedHashSet have over a HashSet?

a)

A) It uses less memory.

b)

B) It allows duplicate elements.

c)

C) It maintains insertion order.

d)

D) It has faster iteration.

17.

When would you use a Stack over a Queue in Java?

a)

A) When you need to process elements in a first-in, first-out (FIFO) manner.

b)

B) When you need to process elements in a last-in, first-out (LIFO) manner.

c)

C) When elements need to be accessed randomly.

d)

D) When elements are to be processed in parallel.

18.

Which interface should be implemented for object serialization in Java?

a)

A) Serializable

b)

B) Externalizable

c)

C) Cloneable

d)

D) Readable

19.

Which of these data structures does not allow duplicate elements?

a)

A) ArrayList

b)

B) LinkedList

c)

C) HashSet

d)

D) Queue

20.

Which method signature from the List interface is correct for obtaining an element at a specific index?

a)

A) public Object get(int index);

b)

B) public Element getElement(int index);

c)

C) public Object atIndex(int index);

d)

D) public Element get(Object index);

21.

Consider a scenario where you are reading user input using System.in. Which wrapper class would you use to efficiently read strings of text input?

a)

A) InputStreamReader

b)

B) BufferedReader

c)

C) FileReader

d)

D) Scanner

22.

When you need to preserve the order of insertion in a Set, which implementation will you use?

a)

A) HashSet

b)

B) TreeSet

c)

C) LinkedHashSet

d)

D) CopyOnWriteArraySet

23.

A developer needs to ensure that a collection of employees' names does not contain duplicates and can be accessed in the order they were added. Which collection type should be used?

a)

A) TreeSet

b)

B) LinkedHashSet

c)

C) HashSet

d)

D) List

24.

When handling I/O operations, which exception must be handled or declared to be thrown for most I/O operations in Java?

a)

A) IOException

b)

B) FileNotFoundException

c)

C) EOFException

d)

D) UnsupportedEncodingException

25.

Which method would you use to ensure that a List is sorted in descending order?

a)

A) sort()

b)

B) reverse()

c)

C) Collections.sort()

d)

D) Collections.reverseOrder()

26.

What will the following Java code snippet output?

a)

It will create a file with AB written in it.

b)

It will throw an IOException.

c)

It will write 6566 to the file.

d)

It will not compile.

27.

Analyze the following code snippet and choose the correct output:

a)

London

b)

New York

c)

Rome

d)

The code will throw a RuntimeException.

28.

Given the following Java code snippet, what will be the output?

a)

[10, 20, 30]

b)

[10, 30]

c)

[20, 30]

d)

The code will throw an IndexOutOfBoundsException.

29.

What does the following Java code snippet demonstrate?

a)

Writing to a file

b)

Reading the first line from a file

c)

Compiling a Java program

d)

Serializing an object

30.

What will be the result of compiling and running the following code?

a)

[A, B, C]

b)

[A, C]

c)

[B, C]

d)

The code will throw a ConcurrentModificationException.