Sunteți pe pagina 1din 8

CSE214 COMPUTER SCIENCE II

FINAL EXAM PRACTICE QUESTIONS


USE THE FOLLOWING INFORMATION TO ANSWER PROBLEMS 1.1-1.4:
Consider the following four operations on a data structure containing n data values.
A. Finding the maximum value in a singly linked list of n IntNode nodes.
B. Finding the maximum value in an array of n int values by sorting it first using insertion sort.
C. Finding the maximum value in a full binary search tree of n BTNode nodes.
D. Finding the maximum value in a standard heap of n data values.
1.1 The worst-case order of complexity is O(1) for which of these operations?
(a) A
(b) B
(c) C
(d) D
(e) none of these answers
1.2 The worst-case order of complexity is O(log n) for which of these operations?
(a) A
(b) B
(c) C
(d) D
(e) none of these answers
a
1.3 The worst-case order of complexity is O(n) for which of these operations?
(a) A
(b) B
(c) C
(d) D
(e) none of these answers
1.4 The worst-case order of complexity is O(n log n) for which of these operations?
(a) A
(b) B
(c) C
(d) D
(e) none of these answers
1.5 Which of the following postfix expressions evaluates to 35 (assuming integer division)?
(a) 5 4 3 2 1 + * - /
(c) 5 4 3 2 1 - / + *
(e) none of these answers

(b) 5 4 3 2 1 * - / +
(d) 5 4 3 2 1 / + * -

1.6 Consider a binary tree as a directed graph, where every non-leaf node has an edge to each of its
children. Assuming that left child is visited before right child, the depth-first traversal of this graph started
at root, is equivalent to which of the following binary tree traversal?
(a) preorder

(b) postorder

(c) inorder

(d) none of these answers

1.7 What is the maximum number of edges in an undirected graph with n vertices without any multiple
edges? Remember self-cycles (self-loops) must be counted.
(a) n

(b) n2

(c) n2 n

(d) (n2 + n)/2

(e) (n2 n)/2

1.8 What is the maximum number of edges in a simple directed graph with n vertices?
(a) n

(b) n2

(c) n2 n

(d) (n2 + n)/2

(e) (n2 n)/2

1.9 What is the order of complexity for the most efficient algorithm to make a heap, i.e. to convert an array
into a heap?
(a) O(1)

(b) O(log n)

(c) O(n)

(d) O(n * log n)

(e) O(n2)

1.10 How many different heaps (with the maximum at the root) can be formed out of the integers 22, 33,
44, 55, and 66?
(a) 5
(b) 6
(c) 7
(d) 8
(e) none of these answers
1.11 Which of following methods are tail-recursive?
public void B(int n) {
public void A(int n) {
if (n == 0) return;
if (n == 0) return;
else {
else {
System.out.println(n);
if (n % 2 == 0){
B(n-1);
System.out.println(n+*);
}
A(n-2);
}
}
public void C(int n) {
else {
if ( n == 0) return;
System.out.println(n+/);
int[] t = {3,2,7};
A(n-1);
for (int i=0;i<3;i++) {
}
System.out.println(t[i]);
}
C(n-1);
}
}
}
(a) C
(b) B and C
(c) A and B
(d) all of the above
(e) none of the above
1.12 Which of the following trees have all of their leaves at the same level?
I. Red-black Tree
II. B-Tree
III. Complete binary tree
IV. Full binary tree
(a) Only II

(b) II and IV

(c) II, III and IV

(d) all of the above

(e) none of the above

1.13 Consider the following double-hashing function for a hash table of size 100:
H1(k) = k mod 100
H2(k) = 2 + (k mod 52)
For k = 75, how many elements of the hash table are examined in the worst case before an empty slot is
found for k?
(a) 100
(b) 52
(c) 50
(d) 4
(e) 2
1.14 Which of the following is the best replacement for H2(k) given in problem 1.13 for k =75.
(a) H2(k) = 1 + (k mod 52)
(c)H2(k) = 4 + (k mod 52)
(e)none of these answers

(b)H2(k) = 5 + (k mod 52)


(d)H2(k) = 27 + (k mod 52)

1.15 If free()is not called in a C program, where applicable, it can lead to what type of error?
(a) an exception
(b) a memory leak
(c) a syntax error
(d) a segmentation fault
(e) none of these answers
1.16 The expression data[i]is equivalent to which of the following C expressions?
(a) &(data+i)
(b) data+i
(c) *(data+i)
(d) (*data)+i

(e) none of these answers


Use the following hash table to answer questions 2.1-2.3. The hash table stores integer keys using a hash
function h(k) = k mod 17. All keys were inserted without collisions.
INDEX

6
9
T

KEY
HAS_BEEN_USE
D

4
4

8
8
T

5
9
T

9
4
T

1
0
2
7
T

11

1
2
F

1
3
4
7
T

1
4
3
1
T

1
5

16
16

2.1 At what position will the key 60 be stored in the hash table using h(k) above if linear probing is used
to resolve collisions?
(a) 2

(b) 5

(c) 11

(d) 15

(e) none of these answers

2.2 At what position will the key 60 be stored in the original hash table if double hashing is used to
resolve collisions, assuming h1(k) = h(k) and h2(k) = 2 + (k mod 11) ?
(a) 0

(b) 6

(c) 11

(d) 12

(e) none of these answers

2.3 What is the load factor of the original hash table?


(a) 9/17

(b) 17/9

(c) 9

(d) 17

(e) none of these answers

Use the following method to answer questions 2.4-2.7. This method performs a sequential search for a
target recursively on an array of unique data values.
public int search(int[] data, int index, int target) {
if ( stopping condition ) return -1;
else if (data[index] == target) return index;
else return (
recursive call
);
}
2.4 Assuming that this method is initially called with index = 0, what is the correct stopping condition?
(a) index == 0
(c) index == data.length
(e) none of the answers above

(b) index < 0


(d) index > data.length

2.5 Assuming that this method is initially called with index = 0, what is the correct recursive call?
(a) search(data, index-1, target)
(c) search(data, 2*index+1, target)
(e) none of the answers above

(b) search(data, index+1, target)


(d) search(data, index/2, target)

2.6 If the array that we are searching has 64 values, what is the minimum number of recursive calls?
(a) 0
(b) 1
(c) 63
(d) 64
(e) none of these answers

2.7 If the array that we are searching has 64 values, what is the maximum number of recursive calls?
a) 6
(b) 63
(c) 64
(d) 65
(e) none of these answers
2.8 Consider the IntArrayBag class discussed in class. The following new IntArrayBag method
supposedly determines if an instance of this class has the same number of integers as another instance of
this class. What is wrong with this method?
public boolean sameSize(IntArrayBag otherBag)
{
return (manyItems == otherBag.manyItems);
}
(a) This method should be a static method since its parameter is of type IntArrayBag.
(b) This method can throw a NullPointerException which is not indicated.
(c) This method does not have direct access to the manyItems variable of the otherBag object.
(d) This method should use the equals method to test for equality rather than the == operator.
(e) none of the answers above
2.9 Assuming that a queue is implemented using a singly linked list of IntNode nodes where front
references the first node of the list only (there is no rear reference), what is the order of complexity of the
enqueue operation if there are n nodes in the list?
(a) O(1)

(b) O(n)

(c) O(log n)

(d) O(n2)

(e) none of these answers

USE THE FOLLOWING INFORMATION TO ANSWER PROBLEMS 2.10-2.11:


An IntStack is defined using a singly linked list of IntNode nodes such that the head of the list stores
the bottom of the stack. The list has two variables, bottom and top which are references to the nodes
with the bottom and top of the stack respectively.
public void push(int value) {
IntNode newNode = new IntNode(value);
if (top == null)
bottom = newNode;
else a ;
top = newNode;
}
2.10 What is the correct expression for a?
(a) top.setData(newNode);
(c) newNode.setLink(top);
(e) none of these answers

(b) top.setLink(newNode);
(d) newNode.setData(top);

2.11 If the operation pop() were implemented, what would be its worst case order of complexity if the
stack was a list with n nodes?

(a) O(1)

(b) O(n)

(c) O(n log n) (d) O(n2)

(e) none of these answers

USE THE FOLLOWING INFORMATION TO ANSWER PROBLEMS 2.12:


public static int mystery(int n) {
IntQueue q = new IntQueue();
int i;
int j = n;
for (i = 1; i <= n; i++)
q.enqueue(i);
while (!(q.isEmpty())) {
for (i = 1; i <= j; i++)
q.enqueue(q.dequeue());
j = q.dequeue();
}
return j;
}
2.12 What does this method return if n = 4?
(a) 4

(b) 3

(c) 2

(d) 1

(e) none of these answers

USE THE FOLLOWING C PROGRAM TO ANSWER PROBLEMS 3.1-3.2:

#include <stdio.h>
void sample(int a, int *b, int c[], int d[]){
a++;
(*b)++;
c[0]=88;
d = c;
printf( IN SAMPLE: %d %d %d %d \n, a, *b , c[0], d[0]);
}
int main(){
int x=10, y=20, w[3]={22,33,44}, z[3]={55,66,77};
sample(x, &y, w, z);
printf( IN MAIN: %d %d %d %d \n, x, y, w[0], z[0]);
return 0;
}
3.1 What is the output printed in sample?
________________________________________
3.2 What is the output printed in main?

________________________________________

4. Show the B-tree after the integer 4 is inserted into the following B-tree, where MINIMUM=2.
6 60 80 90
1 2 3 5

61 62

81 82

91 92

5. An array is sorted in an increasing order and contains 64 data values.


(a) If sequential search is used, what is the maximum number of comparisons ______________
that are needed to search for a target in this array?
______________

(b) If binary search is used, what is the maximum number of comparisons


that are needed to search for a target in this array?
(c) If the target is in position 0 of the array, which search technique would
find the data faster? Why?

______________

6. Trace how selection sort run on the following array of integers in an increasing order, showing the
results after each run of the outer loop. Do not write a program.
10

21

8 18 14

70 1

7. A simple directed un-weighted graph can be stored as an array of singly-linked edge lists. Assuming
that IntNode is defined with the standard methods, setData, getLink, and setLink, complete the
method indicated below, based on their specifications. If you write dont grade or leave the answer
blank, we will not grade any of your work for this problem and you will receive 2 points.
public class Graph {
private IntNode[] edgeList;
private int numNodes;
public Graph(int maxNodes) throws GraphException
{
if (maxNodes <= 0)
throw new GraphException("Graph size error");
edgeList = new IntNode[maxNodes];
numNodes = maxNodes;
}
// Other methods not shown here
public boolean isEdge(int source, int target) throws GraphException
{
// Returns true if there is an edge from the source vertex to
// the target vertex; false otherwise. The neighbors are
// stored in increasing numerical order.
// YOU COMPLETE

} // isEdge
} // Graph
8. A list of key-value pairs is stored using two arrays namely keys and values, where keys[i] hold the
ith key and values[i] hold the ith value. Thus reordering of elements in keys requires reordering of
respective elements in values. Assuming that a key is either 0 or 1, write a non-recursive Java method to
sort the list in non-decreasing order by comparing the keys at most n times. And the total running time
should not exceed O(n). Your algorithm must sort the list in-place and you should not create additional
lists. If you write dont grade or leave the answer blank, we will not grade any of your work for this
problem and you will receive 2 points. Example of a result list sorted by keys is as follows:
keys:
0
0
0
0
1
1 1
1
values:
21 7
4
8
76 100 32 15
public class KeyValueList {
private int keys[];
private int values[];
private int manyItems;
public KeyValueList(int maxSize) {
keys = new int[maxSize];
values = new int[maxSize];
manyItems=0;
}
// Dont implement accessor and mutuator methods.
public void swap(int s, int d) {
int temp;
temp = keys[s];
keys[s] = keys[d];
keys[d] = temp;
temp = values[s]; values[s]= values[d]; values[d]= temp;
}
// manyItems is the number of elements to be sorted.
// Simply ignore the values in comparisons.
public void sort() {
}//sort
}//KeyValueList
9. Consider the following declaration:
struct node {
int zip_code;
char name[20];
struct node *left;
struct node *right;
};
typedef struct node node_type;
Provided that zip_code is used as the key for creating a Binary Search Tree (BST) of node_type
nodes, write a C function search that takes target along with the root of a BST and searches for
target in the tree. If target is found, your function should copy its associated name to answer and
return 1; otherwise return 0. Note that answer is the third parameter defined in function search. You
may use any C library functions you choose. If you write dont grade or leave the answer blank, we will
not grade any of your work for this problem and you will receive 2 points.

int search(int target, node_type *root, char *answer)

S-ar putea să vă placă și