WorksheetsUnit 4 - io, collections_java
Total questions: 30
Worksheet time: 18mins
What method is used for reading bytes from a file in Java?
A) readBytes()
B) fileRead()
C) read()
D) getBytes()
Which of the following is not a method of the Set interface?
A) add()
B) get()
C) remove()
D) iterator()
Which class would you use to write character streams in Java?
A) FileOutputStream
B) BufferedWriter
C) InputStreamReader
D) ByteBuffer
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) HashSet
B) LinkedHashSet
C) TreeSet
D) PriorityQueue
To remove duplicates from a list while maintaining the element order, which collection type should you convert the list into?
A) HashSet
B) LinkedHashSet
C) TreeSet
D) PriorityQueue
You need to implement a FIFO (First-In-First-Out) structure. Which collection interface provides this capability?
A) Set
B) List
C) Queue
D) Map
You have a SortedSet of timestamps. You need to retrieve and remove the earliest timestamp. Which methods should you use?
A) getFirst() and removeFirst()
B) first() and remove()
C) lowest() and dequeue()
D) earliest() and delete()
If you are using a BufferedReader to read text from a stream, which method would you use to read a line of text?
A) readLine()
B) readText()
C) getText()
D) nextLine()
When would you prefer ArrayList over LinkedList for implementing a list in Java?
A) When frequent addition and removal of elements is required.
B) When memory usage is more of a concern than performance.
C) When there is a need to frequently update elements in the middle of the list.
D) When fast random access to elements is required.
In Java, which of these classes are used to read characters from a file?
A) FileInputStream
B) FileReader
C) BufferedInputStream
D) DataInputStream
Which List implementation provides constant time performance for the get and set methods?
A) LinkedList
B) CopyOnWriteArrayList
C) Stack
D) ArrayList
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) LinkedList
B) CopyOnWriteArrayList
C) ArrayList
D) Vector
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) HashSet
B) LinkedHashSet
C) TreeSet
D) ConcurrentSkipListSet
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) addFirst() and addLast()
B) push() and append()
C) insertBeginning() and insertEnd()
D) enqueue() and stack()
To quickly search for an element in a List, which condition should be met regarding the list's organization?
A) The list should be unordered.
B) The list should be sorted.
C) The list should be a LinkedList.
D) The list should be synchronized.
What advantage does a LinkedHashSet have over a HashSet?
A) It uses less memory.
B) It allows duplicate elements.
C) It maintains insertion order.
D) It has faster iteration.
When would you use a Stack over a Queue in Java?
A) When you need to process elements in a first-in, first-out (FIFO) manner.
B) When you need to process elements in a last-in, first-out (LIFO) manner.
C) When elements need to be accessed randomly.
D) When elements are to be processed in parallel.
Which interface should be implemented for object serialization in Java?
A) Serializable
B) Externalizable
C) Cloneable
D) Readable
Which of these data structures does not allow duplicate elements?
A) ArrayList
B) LinkedList
C) HashSet
D) Queue
Which method signature from the List interface is correct for obtaining an element at a specific index?
A) public Object get(int index);
B) public Element getElement(int index);
C) public Object atIndex(int index);
D) public Element get(Object index);
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) InputStreamReader
B) BufferedReader
C) FileReader
D) Scanner
When you need to preserve the order of insertion in a Set, which implementation will you use?
A) HashSet
B) TreeSet
C) LinkedHashSet
D) CopyOnWriteArraySet
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) TreeSet
B) LinkedHashSet
C) HashSet
D) List
When handling I/O operations, which exception must be handled or declared to be thrown for most I/O operations in Java?
A) IOException
B) FileNotFoundException
C) EOFException
D) UnsupportedEncodingException
Which method would you use to ensure that a List is sorted in descending order?
A) sort()
B) reverse()
C) Collections.sort()
D) Collections.reverseOrder()
What will the following Java code snippet output?
It will create a file with AB written in it.
It will throw an IOException.
It will write 6566 to the file.
It will not compile.
Analyze the following code snippet and choose the correct output:
London
New York
Rome
The code will throw a RuntimeException.
Given the following Java code snippet, what will be the output?
[10, 20, 30]
[10, 30]
[20, 30]
The code will throw an IndexOutOfBoundsException.
What does the following Java code snippet demonstrate?
Writing to a file
Reading the first line from a file
Compiling a Java program
Serializing an object
What will be the result of compiling and running the following code?
[A, B, C]
[A, C]
[B, C]
The code will throw a ConcurrentModificationException.
