18. Linked Lists, Stacks, Queues, and Priority Queues - 20 Questions

# 18. Linked Lists, Stacks, Queues, and Priority Queues

20 Questions
Created by Y. Daniel Liang
Free
##### This Python quiz is aimed at students, enthusiasts and job seekers with little or no programming experience. It aims to provide them with an understanding of the role of computers and python programming language.
1. ________ is a data structure to store data in a sequential order.

A list
A set
A dictionary
A heap

2. If a pointer p does not point to anything, assign ________ to p.

null
none
None
0

3. If a list is empty, which of following statements are true?

1. head is None.
2. tail is None.
3. head == tail.
4. head < tail.

1, 2, 3
2, 3, 4
1, 2, 4
1, 3, 4

4. Which of the following operations are supported by a list?

1. Retrieve an element from this list.
2. Insert a new element to this list.
3. Delete an element from this list.
4. Find how many elements are in this list.
5. Find whether an element is in this list.

1, 2, 3, 4
2, 3, 4, 5
1, 2, 3, 4, 5
3, 4, 5

5. list is more efficient than LinkedList for the following operations:

Insert/delete an element in the middle of the list.
Insert/delete an element in the beginning of the list.
Insert/delete an element at the end of the list.
Retrieve an element given the index.

6. LinkedList is more efficient than list for the following operations:

1. Insert/delete an element in the middle of the list.
2. Insert/delete an element in the beginning of the list.
3. Insert/delete an element at the end of the list.
4. Retrieve an element given the index.

1, 2
2, 3
3, 4
1, 4

7. Suppose list1 is a list and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code:

A:
while len(list1) > 0:
del list1[0]

B:
while list2.getSize() > 0:
list2.removeFirst()

Code fragment A runs faster than code fragment B.
Code fragment B runs faster than code fragment A.
Code fragment A runs as fast as code fragment B.
Can't say

8. Suppose list1 is a list. Analyze the following code:

A:
while len(list1) > 0:
del list1[len(list1) - 1]

B:
while len(list1) > 0:
list1.remove(list1.get(len(list1) - 1))

Code fragment A runs faster than code fragment B beacuse the time complexity for code fragment A is O(n) and for B is O(n^2).
Code fragment B runs faster than code fragment A.
Code fragment A runs as fast as code fragment B beacuse both code fragment A and B have the same time complexity O(n).
Both code fragment A and B have the same time complexity O(n), but A runs faster beacuse code fragment A has less overhead.

9. Suppose list1 is a list and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code:

A:
while len(list1) > 0:
del list1[len(list1) - 1]

B:
while list2.getSize() > 0:
list2.removeLast()

Code fragment A runs faster than code fragment B beacuse the time complexity for code fragment A is O(n) and for B is O(n^2).
Code fragment B runs faster than code fragment A.
Code fragment A runs as fast as code fragment B beacuse both code fragment A and B have the same time complexity O(n).
Both code fragment A and B have the same time complexity O(n), but A runs faster beacuse LinkedList has more overhaed on creating object for each node in the linked list.

10. Suppose list2 is a LinkedList. Analyze the following code:

A:
while len(list2) > 0:
list2.remove(list2.get(len(list2) - 1))

B:
while list2.getSize() > 0:
list2.removeLast()

Code fragment A runs faster than code fragment B.
Code fragment B runs faster than code fragment A.
Code fragment A runs as fast as code fragment B.
Can't say

11. Suppose list1 is a list and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code:

A:
for i in range(100000):
list1.insert(0, i)

B:
for i in range(100000):
list2.insert(0, i);

Code fragment A runs faster than code fragment B.
Code fragment B runs faster than code fragment A.
Code fragment A runs as fast as code fragment B.
Can't say

12. Suppose list1 is a list and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code:

A:
for i in range(100000):
list1.append(i);

B:
for i in range(100000):

Code fragment A runs faster than code fragment B beacuse the time complexity for code fragment A is O(n) and for B is O(n^2).
Code fragment B runs faster than code fragment A.
Code fragment A runs as fast as code fragment B beacuse both code fragment A and B have the same time complexity O(n).
Both code fragment A and B have the same time complexity O(n), but A runs faster beacuse LinkedList has more overhaed on creating object for each node in the linked list.

13. Suppose list1 is a list and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code:

A:
for i in range(100000):
sum += list1[i];

B:
for i in range(100000):
sum += list2.get(i);

Code fragment A is more efficient that code fragment B.
Code fragment B is more efficient that code fragment A.
Code fragment A is as efficient as code fragment B.
Can't say

14. Which of the following are true?

1. An iterator is an object that provides a uniformed way for traversing the elements in a container object.
2. To enable the traversal using a for loop in a container object, the container class must implement the __iter__(self) method that returns an iterator.
3. An iterator class must contains the __next__(self) method that returns the next element in the container object.
4. When there are no items left to iterate, the __next__() method must raise a StopIteration exception.

1, 2, 3, 4
1, 2, 3
2, 3, 4
1, 3, 4

15. Which of the following are true?

1. Generators are special Python functions for generating iterators.
2. When you define an iterator class, the __next__ and __iter__ methods must be defined explicitly. Using a generator, these two methods are automatically defined when you create an iterator from a generator.
3. A generator uses the yield keyword to return data rather than using the return keyword.
4. When the generator terminates, it automatically raises a StopIteration exception.

1, 2, 3
2, 3, 4
1, 3, 4
1, 2, 3, 4

16. Which of the following are true?

1. A stack can be viewed as a special type of list, where the elements are accessed, inserted, and deleted only from the end, called the top, of the stack.
2. A queue represents a waiting list. A queue can be viewed as a special type of list, where the elements are inserted into the end (tail) of the queue, and are accessed and deleted from the beginning (head) of the queue.
3. Since the insertion and deletion operations on a stack are made only at the end of the stack, using an array list to implement a stack is more efficient than a linked list.
4. Since deletions are made at the beginning of the list, it is more efficient to implement a queue using a LinkedList than a list.

1, 2, 3
2, 3, 4
1, 3, 4
1,2, 3, 4

17. In the implementation of Stack and Queue, which of the following are true?

1. Stack contains all the methods defined in list.
2. Queue contains all the methods defined in LinkedList
3. Stack contains a list for storing elements
4. Queue contains a linked list for storing elements

1, 2
2, 3
3, 4
1, 4

18. Which data structure is appropriate to store patients in an emergency room?

Stack
Queue
Priority Queue

19. Which data structure is appropriate to store customers in a clinic for taking flu shots?

Stack
Queue
Priority Queue
list