Question

In: Computer Science

JAVA- List, Stacks, Queues, Sets, And Maps Question 1 Given that you want to be able...

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

Solutions

Expert Solution

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.


Related Solutions

Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
The purpose of this problem is to gain familiarity with stacks and queues. You have three...
The purpose of this problem is to gain familiarity with stacks and queues. You have three jugs that can hold c1, c2, and c3 liters of water, respectively. Initially, jug 1 is full and the other two jugs are empty. You can repeat the following procedure any number of times: Choose two of the jugs and pour the contents of one into the other until either the first is empty or the second is full. Your goal is to end...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to be implemented by you. Write a menu driven program that implements the following Binary Search Tree Operations FIND (item) INSERT (item) DELETE (item) DELETE_TREE (delete all nodes - be careful with the traversal!)
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design...
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251 1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. 2. Add the following operations to your program: a. Return the first person in the queue...
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
In Java programing : Q1-A Deque supports operations, addFront(), addRear(), removeFront() and removeRear(). Given stacks and...
In Java programing : Q1-A Deque supports operations, addFront(), addRear(), removeFront() and removeRear(). Given stacks and their push() and pop() operations only, what are the minimum number of stacks required for this implementation? A-1, B-2, C-3, D-4, Q2- True or false and why The height of a random binary tree with 100 nodes can range from 7 to 100.? Q3-One can convert a binary tree into its mirror image by swapping each node's left and right childern while traversing it...
java question: How would you be able to store a matrix from a text file into...
java question: How would you be able to store a matrix from a text file into a linked or doubly linked list, if you cannot use 2D arrays? input example: 1 2 3 4 1 3 2 4 4 2 3 1
C++ Given two finite sets, list all elements in the Cartesian product of these two sets.
C++ Given two finite sets, list all elements in the Cartesian product of these two sets.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT