Python MCQ 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Chapter 17 Sorting

Section 17.2 Bubble Sort


17.1  Suppose a list is [2, 9, 5, 4, 8, 1]. After the first pass of bubble sort, the list becomes
A. 2, 9, 5, 4, 8, 1
B. 2, 9, 5, 4, 1, 8
C. 2, 5, 9, 4, 8, 1
D. 2, 5, 4, 8, 1, 9
E. 2, 1, 5, 4, 8, 9

17.2  The best-time complexity for bubble sort is _____________.


A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

17.3  The worst-time complexity for bubble sort is _____________.


A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

Section 17.3 Merge Sort


17.4  The time to merge two sorted lists of size n is _________
A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

17.5  The worst-time complexity for merge sort is _________


A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

17.6  The average-time complexity for merge sort is _________


A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

Section 17.4 Quick Sort


17.7  What is correct about a pivot?
A. A pivot divides a list into two sublists of equal size.
B. A pivot can be chosen arbitrarily.
C. A pivot divides a list into two sublists, the elements in the first list are no larger than
the pivot and the elements in the second list are larger than the pivot.
D. You should always choose a pivot that divides the list evenly.

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.9  The worst-time complexity for quick sort is _________


A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

17.10  The average-time complexity for quick sort is _________


A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

Section 17.5 Heap Sort


17.11  Which of the following statements are true?
A. A heap is a complete binary tree.
B. Each node is greater than or equal to any of its children.
C. A binary tree is complete if every level of the tree is full except that the last level may
not be full and all the leaves on the last level are placed left-most.
D. A heap is a full binary tree.

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.14  A heap is represented using a list. Is the list [1, 2, 4, 5, 9, 3] a heap?


A. Yes
B. No

17.15  A heap is represented using a list. Is the list [64, 42, 59, 32, 39, 44] a heap?
A. Yes
B. No

17.16  The worst-time complexity for heap sort is _________


A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)
17.17  The average-time complexity for heap sort is _________
A. O(1)
B. O(logn)
C. O(n)
D. O(nlogn)
E. O(n*n)

Section 17.6 Bucket Sort and Radix Sort


17.18  The most efficient algorithm for sorting integer keys is __________.
A. quick sort
B. merge sort
C. heap sort
D. radix sort

17.19  The __________ algorithm does not compare keys.


A. quick sort
B. merge sort
C. heap sort
D. radix sort

Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues

Section 18.2 Linked Lists


18.1  ________ is a data structure to store data in a sequential order.
A. A list
B. A set
C. A dictionary
D. A heap

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


A. null
B. none
C. None
D. 0

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


A. head is None.
B. tail is None.
C. head == tail.
D. head < tail.

18.4  Which of the following operations are supported by a list?


A. Retrieve an element from this list.
B. Insert a new element to this list.
C. Delete an element from this list.
D. Find how many elements are in this list.
E. Find whether an element is in this list.

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.

18.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))
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.

18.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()
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.

Section 18.3 Iterators


18.14  Which of the following are true?
A. An iterator is an object that provides a uniformed way for traversing the elements in a
container object.
B. 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.
C. An iterator class must contains the __next__(self) method that returns the next
element in the container object.
D. When there are no items left to iterate, the __next__() method must raise a
StopIteration exception.

Section 18.4 Generators


18.15  Which of the following are true?
A. Generators are special Python functions for generating iterators.
B. 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.
C. A generator uses the yield keyword to return data rather than using the return
keyword.
D. When the generator terminates, it automatically raises a StopIteration exception.

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

Chapter 19 Binary Search Trees

Section 19.2 Binary Trees


19.1  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. 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

Section 19.3 Deleting Elements from a BST


19.10  The time complexity for searching an element in a binary search tree is _______.
A. O(n)
B. O(logn)
C. O(nlogn)
D. O(n^2)

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)

Chapter 20 AVL Trees

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.5  In a ________, the element j to be removed is always at the root.


A. binary tree
B. binary search tree
C. AVL tree
D. binary heap

20.6  In a ________, the element just inserted is always at the leaf.


A. binary search tree
B. AVL tree
C. 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.3  A collision occurs _____________.


A. when two or more keys are mapped to the same hash value.
B. when two elements have the same key value.
C. when two elements are mapped to the same key.

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

21.9  _______ measures how full the hash table is.


A. Load factor
B. Threshold

Chapter 22 Graphs and Applications

Section 22.2 Basic Graph Terminologies


22.1  A ____ is an edge that links a vertex to itself.
A. loop
B. parallel edge
C. weighted edge
D. directed edge

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

22.4  What is the number of edges in a complete graph of n vertices?


A. n
B. n - 1
C. n(n-1)/2
D. n*n

22.5  What is the number of edges in a tree of n vertices?


A. n
B. n - 1
C. n(n-1)/2
D. n*n

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

22.8  The time complexity of the DFS algorithm is O(|E| + |V|).


A. true
B. false

22.9  The time complexity of the BFS algorithm is O(|E| + |V|).


A. true
B. false

Chapter 23 Weighted Graph ApplicationsSection

23.1 2 Representing Weighted Graphs

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

Section 23.3 The WeightedGraph Class


23.3  The WeightedGraph is a subtype of _________.
A. UnweightedGraph
B. AbstraGraph
C. Graph
D. WeightedEdge

Section 23.4 Minimum Spanning Trees


23.4  True or False? A graph may have several minimum spanning tree.
A. True
B. False

23.5  The MST class is subtype of __________.


A. BST
B. AVLTree
C. AbstractGraph.Tree
D. Tree

23.6  The getMinimumSpanningTree() method returns __________.


A. an ArrayList
B. a LinkedList
C. a queue
D. a MST

Section 23.5 Finding Shortest Paths


23.7  A ___________ of a graph is a subgraph that is a tree and connects all vertices in the
graph.
A. spanning tree
B. shorted path

23.8  The ShortestPathTree class is subtype of __________.


A. BST
B. AVLTree
C. AbstractGraph.Tree
D. Tree

23.9  The getShortestPath() method returns __________.


A. an ArrayList
B. a LinkedList
C. a ShortestPathTree
D. a MST

You might also like