Docsity

Programming paradigms and Data Structures, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Programming paradigms and
Data Structures
1. A 2-3-4 tree is so named because a node can have
a. three children and four data items.
b. zero, two, three, or four children.
c. two parents, zero or three children, and four items.
d. two parents, three items, and zero or four children. - ANSb
1. A balanced binary search tree is desirable because it avoids slow
performance when data is inserted ________. - ANS1. in order (or inverse
order) of the keys
1. Aside from the insert, delete, search, and traverse methods common to
all "database" data structures, array data structures should have
___________ method(s). - ANS1. They should have get() and set() methods to
access the value of a
particular array cell. The ability to get and set cells indexed by an
integer value is the basic functionality of arrays in all languages.
1. Computer sorting algorithms are more limited than humans in that
a. the amount of data that computers can sort is much less than what
humans can.
b. humans can invent new sorting algorithms, whereas computers
cannot.
c. humans know what to sort, whereas computers need to be told.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Programming paradigms and Data Structures and more Exams Data Structures and Algorithms in PDF only on Docsity!

Programming paradigms and

Data Structures

  1. A 2-3-4 tree is so named because a node can have a. three children and four data items. b. zero, two, three, or four children. c. two parents, zero or three children, and four items. d. two parents, three items, and zero or four children. - ANSb
  2. A balanced binary search tree is desirable because it avoids slow performance when data is inserted ________. - ANS1. in order (or inverse order) of the keys
  3. Aside from the insert, delete, search, and traverse methods common to all "database" data structures, array data structures should have ___________ method(s). - ANS1. They should have get() and set() methods to access the value of a particular array cell. The ability to get and set cells indexed by an integer value is the basic functionality of arrays in all languages.
  4. Computer sorting algorithms are more limited than humans in that a. the amount of data that computers can sort is much less than what humans can. b. humans can invent new sorting algorithms, whereas computers cannot. c. humans know what to sort, whereas computers need to be told.

d. computers can compare only two things at a time, whereas humans can compare small groups. - ANSd

  1. Data structures that allow programs to ______ a record, _______ for a record, _______ a record, and _______ all the records are considered to be databases in this book. - ANS1. insert, search, delete, traverse
  2. Insertion and deletion in a binary search tree require what Big O time? - ANS1. O(log N)
  3. Suppose you push the numbers 10, 20, 30, and 40 onto the stack in that order. Then you pop three items. Which one is left on the stack? - ANS
  4. The Shellsort works by a. partitioning the array into two parts. b. swapping adjacent elements. c. dealing with widely separated elements. d. forming increasingly larger shells of elements that are already sorted. - ANSc
  5. Using Big O notation, how long does it take (ideally) to find an item in a hash table? - ANS1. O(1)
  6. What does the term complete mean when applied to binary trees? a. All the necessary data items have been inserted. b. All the rows are filled with nodes, except possibly the bottom one. c. All existing nodes contain data.

the keys of its children c. every left child has a key less than its parent and every right child has a key greater than or equal to its parent. d. in the path from the root to every leaf node, the key of each node is greater than or equal to the key of its parent. - ANSc

  1. A(n) __________ transforms a range of key values into a (possibly large) number, which can be mapped to a range of index values. - ANS2. hash function
  2. Access to the links in a linked list is usually through the _________ link. - ANS2. first (called __first in LinkedList)
  3. Data structures are a. composed of names and values. b. built with fields that have methods attached to them. c. implemented with object classes. d. records that don't change in a database. - ANS2. c implemented with object classes.
  4. If a program calls triangular(100) using the definition in Listing 6-1, what is the maximum number of "copies" of the triangular() function in execution that exist at any one time? - ANS
  5. If an array has 100 elements, then Knuth's interval sequence algorithm would start with an interval of ________. - ANS
  1. In a balanced tree, there are roughly the same number of nodes in the left and right subtrees, and a. the tree may need to be restructured during searches. b. the paths from the root to all the leaf nodes are about the same length. c. all left subtrees are the same height as all right subtrees. d. the starting level of all subtrees is closely controlled. - ANSb
  2. The two basic operations in simple sorting are _________ items and _________ them (or sometimes _________ them). - ANS2. comparing and swapping (or copying/moving)
  3. When constructing a new instance of an Array (Listing 2-3): a. the initial value for at least one of the array cells must be set. b. the data type of all the array cells must be set. c. the key for each data item must be set. d. the maximum number of cells the array can hold must be set. e. none of the above. - ANS2. d. Some languages don't require specifying the maximum number of cells at construction time, but most do.
  4. Which of the following is true? a. The Pop operation on a stack is considerably simpler than the Remove operation on a queue. b. The contents of a queue can wrap around, while those of a stack cannot. c. The top of a stack corresponds to the front of a queue. d. In both the stack and the queue, items removed in sequence are taken

d. Change the direction of the indices in the inner loop. - ANSd

  1. True or False: If you traverse a tree and print the path to each node as a series of the letters L and R for whether the path followed the left or right child at each step, there could be some duplicate paths. - ANSFalse
  2. True or False: The bubble sort always ends up comparing every possible pair of items in the initial array. - ANSFalse
  3. What do LIFO and FIFO mean? - ANSlast in first out - first in first out
  4. When an item is removed from a heap, it is always removed from the __________. - ANS3. root
  5. Where are the copies of the argument passed to the triangular() function, mentioned in question 2, stored? a. in a variable in the triangular() function b. in a field of the Triangular class c. in a placeholder variable of the problem description record d. on a stack - ANSd
  6. Which of the following metrics guarantees a balanced binary tree if it is less than or equal to one? a. the sum of the differences between the node count of the left and right subtrees, at every node of the tree b. the difference in the number of nodes in the left and right subtrees of the root node

c. the absolute difference between the heights of the left and right subtrees, at every node of the tree d. the height of the left subtree minus the height of the right subtree of the root node - ANSc

  1. Why is it important to use private instance attributes like __nItems in the definition of the array data structure? - ANS3. to prevent access to internal values of the object instance from being manipulated by code outside the object.
  2. Assume a call to triangular(100) as in question 2. What is the value of nth right after the triangular() function first returns a nonbase case value? - ANS4. 2. The base case returns 0, and nth has a value of 1 in the caller to the base case. That caller returns the first non-base case value of 1. The value of nth right after 1 is returned is in the call where nth has the value 2.
  3. AVL trees a. track the number of nodes in the subtree below each node. b. use average values of the keys in a subtree to speed up the search time. c. insert each item with height 1 and then adjust that height after modifications to its subtrees in later operations. d. use the difference in height of a node's subtrees to determine which subtree will store the next item inserted. - ANSc
  1. When an item is inserted into a heap, a. a hole is introduced at the root node and sifted down until it reaches the position the item should occupy. b. a search starts from the root to find the item with the key equal to or just above the key to insert, and the item is inserted as that node's child. c. the maximum item in the heap is moved to the first empty cell, and the new item is inserted and sifted down until it reaches the position it should occupy. d. the item is inserted in the first empty cell and then sifted up until it reaches the position it should occupy. - ANSd
  2. When compared to storing data in an ordered array, the main benefit of storing it in a binary search tree is a. having the same search time as traversal time in Big O notation. b. not having to copy data when inserting or deleting items. c. being able to search for an item in O(log N) time. d. having a key that is separate from the value identified by the key. - ANSb
  3. When different keys map to the same index in a hash table, _________ occurs. - ANS4. a collision
  4. A heap can be represented by an array because a heap a. is a binary tree. b. is partially complete. c. is partially ordered. d. satisfies the heap condition. - ANSa
  1. As other items are inserted and removed, does a particular item in a queue move along the array from lower to higher indices, or higher to lower? - ANS5. Neither; it doesn't move.
  2. Fill in the Big O values: The speed of the Shellsort is more than _______ but less than ________. - ANS5. O(N×log N), O(N2)
  3. How many references must be set or changed to insert a link at the end of a singly linked list? - ANS5. 1 plus setting the __next field to None in the new link, which really is a nonreference.
  4. If an empty AVL tree has 100 items inserted into it, and every insertion causes either an even balance or a temporary left-heavy tree at the root, you can conclude that a. the number of right rotations will equal or exceed the number of left rotations. b. the tree will be partially unbalanced leading the average number of comparisons for a search to be around 50. c. the last 50 items inserted will end up in the right subtree of the root. d. only right rotations will be needed to correct imbalances. - ANSa
  5. In a complete, balanced binary tree with 20 nodes, and the root considered to be at level 0, how many nodes are there at level 4? - ANS
  6. Open addressing refers to
  1. A 2-3-4 tree increases its number of levels when ________. - ANS6. the root is split
  2. A subtree of a binary tree always has a. a root that is a child of the main tree's root. b. a root unconnected to the main tree's root. c. fewer nodes than the main tree. d. a sibling with an equal or larger number of nodes. - ANSc
  3. Deleting an item from an AVL tree a. is easiest when the node containing the item has two subtrees. b. uses either one or two rotations to rebalance subtrees based on the height balance of (1) the subtree and (2) the child with the larger height, c. uses fusion operations like 2-3-4 trees do in some situations. d. uses the mirror image operations of inserting an item. - ANSb
  4. In an unordered array, allowing duplicates a. increases times for all operations. b. increases search times in some situations. c. always increases insertion times. d. sometimes decreases insertion times. - ANSb
  5. In the anagrams() function, at a certain depth of recursion, assume a version of the function is working with the string "que". When this method calls a new version of itself, what letters will the new version

be working with? - ANS6. "ue"

  1. In the insert() method in the LinkedList.py program, Listing 5-5, the statement link = Link(datum, self.getNext()) means that a. the datum will be assigned to the __data field in the first Link of the LinkedList. b. the __next field of the new link will refer to the LinkedList's first Link. c. link will be set to the first link whose data matches the given datum. d. a new, two-item linked list with the datum and the next link will be stored in link. - ANSb
  2. In the selection sort, a. the largest keys accumulate on the left (low indices). b. a minimum key is repeatedly discovered. c. a number of items must be shifted to insert each item in its correctly sorted position. d. the sorted items accumulate on the right. - ANSb
  3. Partitioning is a. putting all elements larger than a certain value on one end of the array. b. dividing an array into (almost) equal sized halves. c. partially sorting parts of an array. d. sorting each half of an array separately. - ANSa
  4. Searching for a key by testing adjacent cells in the hash table is called
  1. In the section "A Recursive Binary Search," the original, loop-based form was compared with a recursive form of the find() method. Which of the following is not true? a. The search range starts with the whole array, and only the recursive version can work on a subrange passed through arguments. b. Both forms of the program divide the search range repeatedly in half. c. If the key is not found, the loop version returns when the range bounds cross, but the recursive version finishes when the recursive depth is more than half the initial search range. d. If the key is found, the loop version returns from the entire method, whereas the recursive version returns from one level of recursion. - ANSc
  2. Searching a 2-3-4 tree does not involve a. splitting nodes on the way down if necessary. b. picking the appropriate child to go to, based on the keys of items in a node. c. ending up at a leaf node if the search key is not found. d. examining at least one key in any node visited. - ANSa
  3. True or False: If, on a particular computing platform, swaps take much longer than comparisons, the selection sort is about twice as fast as the bubble sort for all values of N. - ANSfalse
  4. True or False: In an unordered array, it's generally faster to find out an item is not in the array than to find out it is. - ANSFalse
  1. True or False: Pushing and popping items on a stack and inserting and removing items in a queue all take O(N) time. - ANSfalse
  2. True or False: The red-black rules are a sequence of steps that rearrange the nodes in a tree to balance it. - ANSFalse
  3. What are the first five offsets from the original address in quadratic probing? - ANS7. (0), 1, 4, 9, 16, 25
  4. What is a key field used for? - ANS7. A key field is used to find a larger record, typically by keeping a sorted structure of keys to accelerate the search.
  5. When implementing trees as objects, the ______ and the _______ are generally separate classes. - ANS7. node, (binary search) tree
  6. A null child is a. a child that doesn't exist but will be created next as a placeholder to balance the tree. b. a child with no children of its own. c. a child whose key was deleted but was left in the tree as a softdeletion. d. a nonexistent left child of a node with a right child (or vice versa). - ANSd
  7. A queue might be used appropriately to hold a. the items to be sorted in an insertion sort. b. reports of a variety of imminent attacks on the star ship Enterprise. c. keystrokes made by a computer user writing a letter.

b. it is passed over or not, depending on the other array element. c. it is placed next to the array element at the right end. d. it is swapped. - ANSd

  1. Ordered arrays, compared with unordered arrays, are a. much quicker at deletion. b. quicker at insertion. c. quicker to create. d. quicker at searching. - ANSd
  2. Secondary clustering occurs because a. many keys hash to the same location. b. the sequence of step lengths is always the same. c. too many items with the same key are inserted. d. the hash function maps keys into periodic groups. - ANSb
  3. What kind of subproblem is solved in the recursive calls of the TowerOfHanoi.solve() method (Listing 6-3) as compared to the overall problem? - ANS8. moving a smaller number of disks
  4. When one function calls another function using a variable, as in do_something(x), when can the value of x in the calling function be changed by the called function? - ANS8. When the variable, x, is passed as a reference instead of a copy of its value
  1. Which operation is more complex, sifting up or down? Why? - ANS8. sifting down because it must determine the maximum child key
  2. A double-ended list a. allows inserts to performed at either end in constant time. b. has its last link connected to its first link. c. is another name for a doubly linked list. d. has pointers running both forward and backward between links. - ANSa
  3. An unbalanced tree is one a. in which most of the keys have values greater than the average. b. where there are more nodes above the central node than below. c. where the leaf nodes appear much more frequently as the left child of their parents than as the right child, or vice versa. d. in which the root or some other node has many more left descendants than right descendants, or vice versa. - ANSd
  4. Double hashing a. should use a different hash function than that used for the hash address and compute a step size from the hashed value. b. applies the same hash function to the hash address, instead of the key, to get the next hash address. c. is more effective for separate chaining than for open addressing. d. decreases the search time by a factor of two. - ANSa
  5. For what purpose was object-oriented programming developed? - ANS9. To implement data structures and tightly organize algorithms with the