Python MCQ 2
Python MCQ 2
17.8 Suppose you choose the first element as a pivot in the list [5, 2, 9, 3, 8, 4, 0, 1, 6, 7].
Using the partition algorithm in the book, what is the new list after the partition?
A. 5 2 9 3 8 4 0 1 6 7
B. 4 2 3 0 1 5 6 7 9 8
C. 4 2 1 3 0 5 8 9 6 7
D. 2 3 4 0 1 5 9 8 6 7
E. 2 3 4 0 1 5 6 7 8 9
17.12 To remove the root, you need to start a process by first placing _______ to the place of
the root and move it down to maintain the heap property.
A. one of the root's children
B. the larger child of the root
C. the smaller child of the root
D. the last node in the heap
17.13 To add a new node, you need to start a process by first placing it as _______ and move
it up to maintain the heap property.
A. the new root
B. the last node in the heap
C. the left child of the root
D. the right child of the root
17.15 A heap is represented using a list. Is the list [64, 42, 59, 32, 39, 44] a heap?
A. Yes
B. No
18.5 list is more efficient than LinkedList for the following operations:
A. Insert/delete an element in the middle of the list.
B. Insert/delete an element in the beginning of the list.
C. Insert/delete an element at the end of the list.
D. Retrieve an element given the index.
18.6 LinkedList is more efficient than list for the following operations:
A. Insert/delete an element in the middle of the list.
B. Insert/delete an element in the beginning of the list.
C. Insert/delete an element at the end of the list.
D. Retrieve an element given the index.
18.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()
A. Code fragment A runs faster than code fragment B.
B. Code fragment B runs faster than code fragment A.
C. Code fragment A runs as fast as code fragment B.
A:
while len(list1) > 0:
del list1[len(list1) - 1]
B:
while len(list1) > 0:
list1.remove(list1.get(len(list1) - 1))
A. 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).
B. Code fragment B runs faster than code fragment A.
C. Code fragment A runs as fast as code fragment B beacuse both code fragment A and B
have the same time complexity O(n).
D. Both code fragment A and B have the same time complexity O(n), but A runs faster
beacuse code fragment A has less overhaed.
18.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()
A. 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).
B. Code fragment B runs faster than code fragment A.
C. Code fragment A runs as fast as code fragment B beacuse both code fragment A and B
have the same time complexity O(n).
D. 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.
A:
while len(list2) > 0:
list2.remove(list2.get(len(list2) - 1))
B:
while list2.getSize() > 0:
list2.removeLast()
A. Code fragment A runs faster than code fragment B.
B. Code fragment B runs faster than code fragment A.
C. Code fragment A runs as fast as code fragment B.
18.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);
A. Code fragment A runs faster than code fragment B.
B. Code fragment B runs faster than code fragment A.
C. Code fragment A runs as fast as code fragment B.
18.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):
list2.add(i);
A. 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).
B. Code fragment B runs faster than code fragment A.
C. Code fragment A runs as fast as code fragment B beacuse both code fragment A and B
have the same time complexity O(n).
D. 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.
18.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);
A. Code fragment A is more efficient that code fragment B.
B. Code fragment B is more efficient that code fragment A.
C. Code fragment A is as efficient as code fragment B.
Sections 18.5-18.6
18.16 Which of the following are true?
A. 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.
B. 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.
C. 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.
D. Since deletions are made at the beginning of the list, it is more efficient to implement
a queue using a LinkedList than a list.
18.17 In the implementation of Stack and Queue, which of the following are true?
A. Stack contains all the methods defined in list.
B. Queue contains all the methods defined in LinkedList.
C. Stack contains a list for storing elements.
D. Queue contains a linked list for storing elements.
Section 18.7 Priority Queues
18.18 Which data structure is appropriate to store patients in an emergency room?
A. Stack
B. Queue
C. Priority Queue
D. Linked List
E. list
18.19 Which data structure is appropriate to store customers in a clinic for taking flu shots?
A. Stack
B. Queue
C. Priority Queue
D. list
E. Linked List
18.20 Suppose the rule of the party is that the participants who arrive later will leave earlier.
Which data structure is appropriate to store the participants?
A. Stack
B. Queue
C. list
D. Linked List
19.2 The ________ of a path is the number of the edges in the path.
A. length
B. depth
C. height
D. degree
19.3 The _______ of a node is the length of the path from the root to the node.
A. length
B. depth
C. height
D. degree
19.4 The _______ of a nonempty tree is the length of the path from the root node to its
furthest leaf + 1.
A. length
B. depth
C. height
D. degree
19.5 The ________ is to visit the left subtree of the current node first, then the current node
itself, and finally the right subtree of the current node.
A. inorder traversal
B. preorder traversal
C. postorder traversal
D. breadth-first traversal
19.6 The _________ is to visit the left subtree of the current node first, then the right subtree
of the current node, and finally the current node itself.
A. inorder traversal
B. preorder traversal
C. postorder traversal
D. breadth-first traversal
19.7 The _________ is to visit the current node first, then the left subtree of the current node,
and finally the right subtree of the current node.
A. inorder traversal
B. preorder traversal
C. postorder traversal
D. breadth-first traversal
19.8 The _________ is to visit the nodes level by level. First visit the root, then all children
of the root from left to right, then grandchildren of the root from left to right, and so on.
A. inorder traversal
B. preorder traversal
C. postorder traversal
D. breadth-first traversal
19.9 True or False? A new element is always inserted into a leaf node.
A. True
B. False
19.11 The time complexity for inserting an element into a binary search tree is _______.
A. O(n)
B. O(logn)
C. O(nlogn)
D. O(n^2)
19.12 The time complexity for deleing an element into a binary search tree is _______.
A. O(n)
B. O(logn)
C. O(nlogn)
D. O(n^2)
20.1 The _________ of a node is the height of its right subtree minus the height of its left
subtree.
A. balance factor
B. depth
C. length
D. degree
20.2 The balance factor of every node in an AVL tree may be _________.
A. 0
B. 1
C. -1
D. 2
20.3 A __________ (with no duplicate elements) has the property that for every node in the
tree the value of any node in its left subtree is less than the value of the node and the value of
any node in its right subtree is greater than the value of the node.
A. binary tree
B. binary search tree
C. AVL tree
D. binary heap
Sections 29.2-29.9
20.4 The time complexity for insertion, deletion, and search is O(logn) for a ___________.
A. binary tree
B. binary search tree
C. AVL tree
D. binary heap
20.7 The average time-complexity for insertion, deletion, and search in a ________ is
O(logn).
A. binary search tree
B. AVL tree
C. binary heap
Chapter 21 Hashing
Section 21.2 What Is Hashing?
21.1 A hashing function __________.
A. stores an element in the hash table
B. maps a key to an index in the hash table
21.2 If each key is mapped to a different index in the hash table, it is called _______.
A. normal hashing
B. perfect hashing
21.4 True or False? Two objects have the same hashCodes if they are equal.
A. True
B. False
21.5 True or False? If two strigns are equal, the two strings have the same hashCodes.
A. True
B. False
Sections 21.4-21.5
21.6 _____________ is to find an open location in the hash table in the event of collision.
A. Open addressing
B. Separate chaining
21.7 When a collision occurs during the insertion of an entry to a hash table, ______ finds
the next available location sequentially.
A. linear probing
B. quadratic probing
C. double hashing.
21.8 The __________ places all entries with the same hash index into the same location,
rather than finding new locations.
A. Open addressing scheme
B. separate chaining scheme
22.2 If two vertices are connected by two or more edges, these edges are called ______.
A. loop
B. parallel edge
C. weighted edge
D. directed edge
22.3 A _________ is the one in which every two pairs of vertices are connected.
A. complete graph
B. weighted graph
C. directed graph
Sections 22.3-22.10
22.6 The _______ search of a graph first visits a vertex, then it recursively visits all the
vertices adjacent to that vertex.
A. depth-first
B. breadth-first
22.7 The _______ the breadth-first search of a graph first visits a vertex, then all its adjacent
vertices, then all the vertices adjacent to those vertices, and so on.
A. depth-first
B. breadth-first
1. A WeightedEdge object contains the public data fields _______.
A. u
B. v
C. weight
D. length
23.2 The adjacent edge for each vertex in the graph is stored in _________.
A. an ArrayList
B. a LinkedList
C. a PriorityQueue
D. a Stack