In: Computer Science
JAVA- List, Stacks, Queues, Sets, And Maps
Question 1
Given that you want to be able to return the items that come immediately before and after a given node. Which variation of a list would be best suited?
1. Circular single-linked list
2. All options are equally good
3. Double linked list
4. Single linked list with header and trailer nodes
5. Single-linked list
Question 2
One of the statements is correct for stacks and queues
1. Both data structures must be implemented using an array
2. Both data structures must be implemented using a linked list
3. Both data structures must have a counter that holds the number of items used
4. Basic operations push, pop, enqueue and dequeue take constant time
Question 3
Assume List list = new ArrayList (). Which mappings below are correct? ( Several options possible)
1. list.add("Red");
2. list.add(new java.util.Date());
3. list.add(new E); // E is a generic type
4. list.add(new ArrayList());
5. list.add(new Integer(100));
Question 4
What is the Comparator interface used for? (Several options possible)
1. To compare objects differently than the compareTo method for them to work.
2. To sort items that have not implemented Comparable interface.
3. To separate the logic of the objects from the rest of the code so that it is easier to operate the code testing.
4. To increase the speed of sorting elements that implement the Comparable interface.
Question 5
What is the difference between Lists, Sets, and Maps? (Several options possible)
1. List allows duplicates, while Sets only allows unique elements. Maps allow duplicate values, but keys for them must be unique.
2. Lists allow multiple elements in it to be null objects, Maps allow multiple null values, but only one null key, while Sets allow only one null value in it.
3. Usually, Lists are sorted, while Sets are unsorted.
4. Lists have no limit on the number of items
they can contain, while Sets and Maps can only hold up to 1 million
items.
Question 6
When to Use List
1. Want to store items and an identifier for the items
2. Want to access items using index
3. Want to keep track of the items
4. Want only unique values
Question 7
When to use Map
1. Want to store items and an identifier for the items
2. Want to access items using index
3. Want to keep track of the items
4. Want only unique values
Question 8
When to use Set
1. Want to store items and an identifier for the items
2. Want to access items using index
3. Want to keep track of the items
4. Want only unique values
Question 9
Suppose arrayList is an ArrayList and linkedList is a LinkedList. Both contain one million items. Analyze the following code:
A: for (int i = 0; i < arrayList.size(); i++) sum += arrayList.get(i); B: for (int i = 0; i < linkedList.size(); i++) sum += linkedList.get(i);
1. Code fragment A runs faster than code fragment B
2. The code snippets run just as fast
3. Code fragment B will fail since LinkedList does not have method get (int)
4. Code fragment B runs faster than code fragment A
Question 10
Which data type should you use if you allow duplicate storage and you should be able to insert items anywhere without using a lot of resources?
1. ArrayList
2. AVL three
3. LinkedList
4. Stack
5. Set
Question 11
What type of container would be best suited for creating a dictionary program?
1. LinkedList
2. Map
3. Stack
4. Set
5. ArrayList
Question 12
Link names of operations with function they have in an ArrayDeque
Poll
1. insert the item at the end of ArrayDeque
2. Returns the first element of ArrayDeque
3. Returns and removes first element of ArrayDeque
Question 13
Link names of operations with function they have in an ArrayDeque
Offer
1. insert the item at the end of ArrayDeque
2. Returns the first element of ArrayDeque
3. Returns and removes first element of ArrayDeque
Question 14
Link names of operations with function they have in an ArrayDeque
Peek
1. insert the item at the end of ArrayDeque
2. Returns the first element of ArrayDeque
3. Returns and removes first element of ArrayDeque
1.
If we want to return items that come immediately before and after a node then Doubly Linked List will be a good choice because we can move in both forward and backward directions.
So option 3 is correct.
2.
To implement a stack or a queue we need a linked list because we can perform insertion and deletion easily. In case of array we would have to resize the array.
So option 2 is correct.
3.
We have created an array in java named list but without specifying its type.
list.add("Red") will add a string Red to the list and its data type will be <string>
So option 1 is correct.
4.
Comparator and Comparable classes are both used for sorting but Comparator can sort using multiple sequences which cannot be done using Comparable.
So options 1 and 2 are correct.
5.
List allows duplicates, while Sets only allows unique elements. Maps allow duplicate values, but keys for them must be unique. Lists allow multiple elements in it to be null objects, Maps allow multiple null values, but only one null key, while Sets allow only one null value in it. Lists are sorted, while Sets are unsorted.
So options 1,2,3 are correct.
6.
We use lists when we want to access using index.
So option 2 is correct.
7.
We use Maps when we want to store items and an key for the items
So option 1 is correct.
8.
We use Sets when we want to store only unique values.
So option 4 is correct.
9.
The running time for both is O(n). So under ideal circumstances both should be equally fast. But the array will have an advantage of requiring less space.
So option 2 is correct.
10.
Linked Lists allow us to store duplicates and insertion is really is with linked lists and it consumes less space than trees.
So option 3 is correct.
11.
Maps are best suited for creating dictionaries because they allow us to use key-value pairs.
So option 2 is correct.
12.
Poll returns and removes the first element.
So option 3 is correct.
13.
Offer inserts the element at the end of the list and returns a boolean value.
So option 1 is correct.
14.
Peek returns the first element but does not remove it like Poll does.
So option 2 is correct.