0% found this document useful (0 votes)
33 views9 pages

CM20254 Exam 2016 Solution

Uploaded by

Yufan Ye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views9 pages

CM20254 Exam 2016 Solution

Uploaded by

Yufan Ye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

University of Bath

COMPUTER SCIENCE

CM20254

Data Structures and Algorithms

SOLUTION

Answer All Questions

PLEASE FILL IN THE DETAILS ON THE FRONT OF YOUR ANSWER


BOOK/COVER AND SIGN IN THE SECTION ON THE RIGHT OF YOUR
ANSWER BOOK/COVER, PEEL AWAY ADHESIVE STRIP AND SEAL.

TAKE CARE TO ENTER THE CORRECT CANDIDATE NUMBER AS DETAILED


ON YOUR DESK LABEL.

DO NOT TURN OVER YOUR QUESTION PAPER UNTIL INSTRUCTED TO BY


THE CHIEF INVIGILATOR
1. Which of the following statements about abstract data types is FALSE?
a. ADTs combine data structures and algorithms.
b. ADTs hide the complexity of their implementation.
c. ADTs hide the complexity of the code which uses them. CORRECT
d. Programs can use an ADT through its interface.
e. The implementation of an ADT can change without the programs using it
having to change.
[1 mark]

2. Given the binary search algorithm, as taught in the lecture, and an array with the
following numbers (from first to last index):
5, 9, 11, 99, 110, 111, 200, 300, 500, 1000, 1001.
How many iterations does the algorithm need to find the element 5?
a. 1 iteration
b. 2 iterations
c. 3 iterations CORRECT
d. 4 iterations
e. None of the others
[1 mark]

3. An algorithm has runtime T(n)=0.00019n3+1000n2+99n+108 for input of size n. What


is the asymptotic time complexity of this algorithm?
a. Ω(n4)
b. ϴ(n2)
c. O(n2)
d. ϴ(n4)
e. ϴ(n3) CORRECT
[1 mark]

4. Which of the following statements about runtime is FALSE?


a. For small input sizes a quadratic algorithm can be faster than a linear-time
algorithm.
b. It is always good to choose a linear algorithm instead of a quadratic one if
possible. CORRECT
c. From a certain input size upwards, a quadratic algorithm is always slower
than a linear algorithm.
d. One linear algorithm can be a lot slower than another linear algorithm.
e. Single runtime measurements are less precise than averages over several
runtime measurements.
[1 mark]

5. Consider the following statements about the following functions:


f(n) = 10 n10, g(n) = 100 n10 and h(n) = 2n. Which one is true?
a. g is O(f), h is Ω(g), and f is ϴ(g) CORRECT
b. g is O(f), g is O(h), and f is Ω(h)
c. g is Ω(f), f is O(g), and f is Ω(h)
d. f is Ω(g), g is ϴ(h), and f is O(h)
e. None of the others is true
[1 mark]

Page 2 of 9
6. The runtime for the following code fragment is ϴ(f(n)). What is f(n)?

for (int i=1; i<n; i=i*2)


System.out.println(“hello”);
for (int j=1; j<n; j++)
for (int k=1; k<10; k++)
System.out.println(“world”);

a. n log n
b. n CORRECT
c. n2
d. n3
e. None of the others
[1 mark]

7. The runtime for the following code fragment is ϴ(f(n)). What is f(n)?

for (int i=0; i<n; i++)


for (int j=0; j<i/10; j++)
for (int k=1; k<n; k=2*k)
System.out.println(i);

a. n log n
b. n2
c. n2 log n CORRECT
d. n3
e. None of the others
[1 mark]

8. A certain cubic time algorithm uses 30 elementary operations to process an input of


size 10. What is the most likely number of elementary operations it will use if given
an input of size 1000?
a. 3 000
b. 30 000
c. 300 000
d. 3 000 000
e. 30 000 000 CORRECT
[1 mark]

9. Consider an algorithm that takes 10s to process an input of n=100, 20s for n=140
and 40s for n=180. Which asymptotic complexity does it likely have?
a. ϴ(n)
b. ϴ(n2)
c. ϴ(2n) CORRECT
d. ϴ(1)
e. None of the others
[1 mark]

Page 3 of 9
10. Given the incomplete Java code for class ArrayList and a method that inserts an
element at index i, as presented in the lecture:

class ArrayList {
private Object[] a; // list elements
private int n = 0; // list size

public void insert(Object o, int i) {


if (i<0 || i>n) throw new RuntimeException();
if (n==a.length()) resize();
X
a[i] = o;
n++;
}
}

Which code must go at the place marked with X for the method to work correctly?
a. for (int j=n-1; j>=i; j--) a[j+1] = a[j]; CORRECT
b. for (int j=i; j<n; j++) a[j+1] = a[j];
c. for (int j=i; j<n-1; j++) a[j+1] = a[j];
d. for (int j=n; j>=i; j--) a[j+1] = a[j];
e. None of the others
[1 mark]

11. Consider the following statements about resizable ArrayLists. Which statement is
FALSE?
a. If the array does not need to be resized, inserting n elements takes ϴ(n).
b. If we double the size of the array when it is full, then an insertion takes
ϴ(n) in the worst case.
c. If we double the size of the array when it is full, then n insertions take on
average ϴ(1) each.
d. If we increase the size of the array by a large constant when it is full, then
inserting n elements takes ϴ(n). CORRECT
e. If we increase the size of the array by a small constant when it is full, then
inserting n elements takes ϴ(n2).
[1 mark]

12. Consider the following statements about lists. Which statement is FALSE?
a. If the list is based on an array, the ith element can be accessed in ϴ(1).
b. If a list is sorted, an element can be found in O(log(n)). CORRECT
c. If the list is based on an array, inserting an element has an average
runtime of ϴ(n).
d. Given a pointer to an element of a doubly-linked list, that element can be
removed in ϴ(1).
e. Given an appropriate implementation, inserting and removing an element
at the end of a linked list is ϴ(1).
[1 mark]

Page 4 of 9
13. Which of the following statements about hashtables is FALSE?
a. A hash function can map keys from an infinite set to indices of a finite
array.
b. Given the load factor does not grow too much, a hash table can be used to
look up values associated with keys quickly.
c. There will always be collisions in a hash table. CORRECT
d. Many collisions can make a hash table slow.
e. A good hash function spreads elements over the buckets of the hashtable.
[1 mark]

14. You wish to use a standard closed hashtable with linear probing (to the right, i.e. in
the direction of higher indices). The hash function has the form h(x) = x % 7 and the
hash table has slots 0, … , 6. If you insert the elements 10, 3, 11, 20, 21, 22, 8 in that
order, what does the hashtable look like at the end?
a. 3, 8, 10, 11, 20, 21, 22
b. 21, 22, 8, 10, 3, 11, 20 CORRECT
c. 20, 8, 22, 10, 3, 11, 21
d. 21, 22, 10, 8, 11, 3, 20
e. None of the others
[1 mark]

15. You wish to use open hashing (where each array slot contains a pointer to a linked
list). The hash function has the form h(x) = x % 5 and the hash table has slots 0, …,
4. If you insert the elements 11, 10, 22, 21, 20, 31, 30, 12, 40 in that order, what is
the maximum list length at the end?
a. 1
b. 4 CORRECT
c. 3
d. 2
e. None of the others
[1 mark]

16. Which of the following inputs makes mergesort in ascending order perform the
largest number of comparisons?
a. 1, 2, 3, 4
b. 4, 3, 2, 1
c. 1, 3, 2, 4 CORRECT
d. 2, 1, 4, 3
e. All the other choices are correct
[1 mark]

17. An implementation of insertion sort runs for 1 second to sort a list of 106 records.
How long will it likely run to sort 107 records?
a. 10 seconds
b. 60 seconds
c. 100 seconds CORRECT
d. 600 seconds
e. 1000 seconds
[1 mark]

Page 5 of 9
18. A researcher has a database of 100 million records of people. The researcher wants
to study the distribution of given names according to other criteria such as zodiac
sign, birth year, etc., so wants to sort by name with the option of further sorting by
other criteria later. The researcher should use the following:
a. Insertion sort
b. Selection sort
c. Shellsort
d. Quicksort
e. Mergesort CORRECT
[1 mark]

19. The Quicksort algorithm selects a pivot element, partitions its array around that
element, then recursively sorts the elements above and below the pivot. Let low be
the first index and high be the last index of the array. Which of the following is the
best way to choose a pivot?
a. Median of three: median { a[low], a[(low+high)/2], a[high] } CORRECT
b. First element, i.e. a[low]
c. Middle element, i.e. a[(low+high)/2]
d. Last element, i.e. a[high]
e. All given options perform equally well
[1 mark]

20. Which of the following input arrays makes insertion sort (as from the lecture, inserting
from right to left) in ascending order perform the largest number of comparisons?
a. (2, 1, 0, 4, 9, 7, 6)
b. (1, 2, 0, 4, 9, 6, 7)
c. (0, 1, 2, 4, 9, 7, 6)
d. (0, 1, 2, 4, 6, 7, 9)
e. (9, 7, 6, 4, 2, 1, 0) CORRECT
[1 mark]

21. Which of the following is NOT a property of mergesort?


a. ϴ(n log n) worst-case sorting time
b. ϴ(n log n) average-case sorting time
c. Good for sorting linked lists
d. In-place sorting CORRECT
e. Linear merge time
[1 mark]

22. Which of the following inputs will make the standard implementation of quicksort
(always choose the leftmost element as the pivot) in ascending order do the least
number of comparisons?
a. 2, 4, 6, 7, 5, 3, 1
b. 1, 3, 5, 7, 6, 4, 2
c. 7, 6, 5, 4, 3, 2, 1
d. 4, 3, 1, 2, 6, 5, 7 CORRECT
e. 1, 2, 3, 4, 5, 6, 7
[1 mark]

23. Which of the following statements about ordinary binary search trees is FALSE?
Page 6 of 9
a. In the worst case finding an element takes ϴ(n).
b. In the best case finding an element takes ϴ(1).
c. Binary search trees are generally faster for finding an element than using
binary search. CORRECT
d. In the average case finding an element takes ϴ(log n).
e. Red-black trees are designed to prevent the worst case of ordinary binary
trees.
[1 mark]

24. The worst case of finding an element in an ordinary binary search tree can happen
if… (select the true statement below).
a. All subtrees of the tree have the same height
b. All paths to the root have similar length
c. All tree nodes are on the same path CORRECT
d. Most tree nodes have two children
e. All leaf nodes are on the same level of the tree
[1 mark]

25. Given the following numbers: 1, 99, 4, 11, 2, 3, 9. When constructing a perfectly
balanced binary search tree from the numbers, which numbers will be in the leaf
nodes (in order from left to right)?
a. 1, 2, 3, 4, 9, 11, 99
b. 1, 4, 99
c. 1, 3, 9, 99 CORRECT
d. 1, 2, 11, 99
e. None of the others
[1 mark]

26. Consider the following graph. Which one of the following is a valid ordering of the
nodes if the graph is traversed using breadth first search (BFS)?

a. 5, 4, 6, 2, 3, 1 CORRECT
b. 3, 6, 4, 1, 5, 2
c. 1, 2, 6, 3, 5, 4
d. 2, 3, 1, 4, 6, 5
e. 4, 3, 2, 1, 5, 6
[1 mark]

27. Consider the following graph. Which one of the following is a valid ordering of the
Page 7 of 9
nodes if the graph is traversed using depth first search (DFS)?

a. 1, 3, 6, 4, 2, 5
b. 1, 6, 3, 2, 5, 4
c. 3, 1, 6, 5, 4, 2 CORRECT
d. 3, 1, 4, 6, 2, 5
e. None of the others
[1 mark]

28. Consider the following graph G. Assume that we use Dijkstra's algorithm to find
shortest paths from node s to any other node in G. Which of the following orderings
on the nodes of G is used by Dijkstra's algorithm to compute the shortest paths that
start at s?

a. s, 1, 2, 3, 4, 5
b. s, 1, 2, 4, 5, 3
c. s, 1, 4, 2, 3, 5
d. s, 1, 2, 4, 3, 5 CORRECT
e. None of the others
[1 mark]

Page 8 of 9
29. An application needs to store the contact details for a large number of people. The
application needs to be able to quickly determine if the contact details for a person
with a certain name are stored and retrieve these contact details. It also needs to be
able to quickly add new contact details. Which of the following data structures would
be most appropriate to store the contact details in memory?
a. Sorted array
b. Sorted ArrayList
c. Linked list
d. Open hashtable CORRECT
e. Closed hashtable
[1 mark]

30. An application needs to store the details of many products. Each product has a
number, and it should be possible to quickly output a list of the products sorted by
their number. It should also be possible to quickly retrieve product details given a
product number. Product details are inserted and removed frequently. Which of the
following data structures would be most appropriate to store the product details in
memory?
a. Sorted ArrayList
b. Doubly linked list
c. Binary search tree CORRECT
d. Open hashtable
e. Closed hashtable
[1 mark]

Page 9 of 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy