Uploaded by jlore331

Test 2 Study Guide

advertisement
CSCI 2380
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
Chapter 15
1. In a recursive function, the base case stops the recursion.
2. With recursion, the base case must eventually be reduced to a general case.
3. The following is an example of a recursive function, where nextNum is a
function such that nextNum(x) = x + 1.
int recFunc(int x)
{
return nextNum(nextNum(x));
}
4. Every call to a recursive function requires the system to allocate memory
for the local variables and formal parameters.
5. Infinite recursions execute forever on a computer.
6. A definition in which something is defined in terms of a smaller version of
itself is called a(n) ____ definition.
a. step-wise
b. recursive
c. member-wise
d. iterative
7. The ____ case is the case for which the solution to an equation is obtained
directly.
a. general
b. base
c. direct
d. tail
int foo(int n)
{
if (n == 0)
return 0;
else
return n + foo(n - 1);
}
//Line
//Line
//Line
//Line
//Line
//Line
//Line
1
2
3
4
5
6
7
8. Consider the accompanying definition of a recursive function. Which of the
statements represents the base case?
a. Statements in Lines 1-6.
b. Statements in Lines 3 and 4.
c. Statements in Lines 5 and 6.
d. Statements in Lines 3, 4, and 5.
9. Consider the accompanying definition of a recursive function. Which of the
statements represent the general case?
a. Statements in Lines 1-6
b. Statements in Lines 3 and 4
c. Statements in Lines 4, 5, and 6
d. Statements in Lines 5 and 6
int recFunc(int num)
{
if (num >= 10)
return 10;
else
CSCI 2380
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
return num * recFunc(num + 1);
}
10. Consider the accompanying definition of a recursive function. What is the
output of the following statement?
cout << recFunc(8) << endl;
a. 4
b. 8
c. 72
d. 720
11. Consider the accompanying definition of a recursive function. What is the
output of the following statement?
cout << recFunc(10) << endl;
a. 10
b. 11
c. 100
d. 110
12. Consider the following definition of the recursive function print.
void print(int num)
{
if (num > 0)
{
cout << num << " ";
print(num - 1);
}
}
What is the output of the following statement?
print(4);
a. 0 1 2 3 4
c. 4 3 2 1
b. 1 2 3 4
d. 4 3 2 1 0
13. A recursive function in which the last statement executed is the recursive
call is called a(n) ____ recursive function.
a. direct
b. tail
c. indefinite
d. indirect
14. If every recursive call results in another recursive call, then the
recursive function (algorithm) is said to have ____ recursion.
a. unlimited
b. indefinite
c. infinite
d. tail
int mystery(int list[], int first, int last)
{
if (first == last)
return list[first];
else
return list[first] + mystery(list, first + 1, last);
}
15. Consider the accompanying definition of the recursive function mystery.
Given the declaration:
int alpha[5] = {1, 4, 5, 8, 9};
CSCI 2380
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
what is the output of the following statement?
cout << mystery(alpha, 0, 4) << endl;
a. 1
b. 18
c. 27
d. 35
16. Consider the accompanying definition of the recursive function mystery.
Given the declaration:
int beta[10] = {2, 5, 8, 9, 13, 15, 18, 20, 23, 25};
What is the output of the following statement?
cout << mystery(beta, 4, 7) << endl;
a. 27
b. 33
c. 55
d. 66
17. Consider the following definition of the recursive function mystery.
int mystery(int first, int last)
{
if (first > last)
return 0;
else if (first == last)
return first;
else
return first + mystery(first + 1, last - 1);
}
What is the output of the following statement?
cout << mystery(6, 10) << endl;
a. 13
b. 21
c. 40
d. 42
18. Consider the following definition of the recursive function mystery.
int mystery(int num)
{
if (num <= 0)
return 0;
else if (num % 2 == 0)
return num + mystery(num – 1);
else
return num * mystery(num – 1);
}
What is the output of the following statement?
cout << mystery(5) << endl;
a. 50
b. 65
c. 120
d. 180
int puzzle(int start, int end)
{
CSCI 2380
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
if (start > end)
return start - end;
else if (start == end)
return start + end;
else
return end * puzzle(start + 1, end - 1);
}
19. Consider the accompanying definition of a recursive function. What is the
output of the following statement?
cout << puzzle(5, 10) << endl;
a. 720
b. 5040
c. 5760
d. 10800
20. Consider the accompanying definition of a recursive function. What is the
output of the following statement?
cout << puzzle(3, 7) << endl;
a. 10
b. 21
c. 42
d. 420
Chapter 16
21. Linked lists allow you to overcome the size limitations of an array data
type.
22. Memory for the components of an array does not need to be contiguous.
23. You can use the pointer head of a linked list to traverse the list.
24. We deallocate the memory for a linked list by calling the operator clear.
25. The length of a linked list is the number of nodes in the list.
26. A linked list is a random access data structure.
27. A doubly linked list can be traversed in either direction.
28. A linked list is a collection of components, called ____.
a.
elements
b.
nodes
c.
members
d.
pointers
29. In a linked list, the address of the first node in the list is stored in a
separate location, called the ____ or first.
a.
head
b.
pointer
c.
front
d.
top
30. In a linked list, the order of the nodes is determined by the address,
called the ____, stored in each node.
a.
head
b.
tail
c.
link
d.
first
31. Every node (except of the last node) in a singly linked list contains ____.
a.
the next node
b.
no address information
CSCI 2380
c.
d.
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
the address of the next node
the address of the previous node
32. The link field of the last node of a linked list is ____.
a.
nullptr
b.
1
c.
n-1
d.
n
33. Because each node of a linked list has two components, we need to declare
each node as a(n) ____.
a.
reference and string
b.
int and object
c.
index and element
d.
class or struct
34. Suppose that the pointer head
link of the last node is nullptr.
the address of the ____ node.
a.
head
b.
c.
second
d.
points to the first node in the list, and the
The first node of the linked list contains
first
tail
35. What is the purpose of the following code?
current = head;
while (current != nullptr)
{
//Process current
current = current->link;
}
a.
c.
Insertion of a node
Traversal of a linked list
b.
d.
Selection of a node
Creation of a new list
struct nodeType
{
int info;
nodeType *link;
};
nodeType *head, *p, *q, *newNode;
newNode = new nodeType;
36. Consider the accompanying code. What is the effect of the following
statement?
newNode->info = 50;
a.
b.
c.
d.
Stores 50 in the info field of the newNode
Creates a new node
Places the node at location 50
Cannot be determined from this code
37. The ____ deallocates the memory occupied by the nodes of a list when the
class object goes out of scope.
a.
constructor
b.
destructor
c.
head pointer
d.
tail pointer
CSCI 2380
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
template <class Type>
____ doublyLinkedList<Type>::isEmptyList() const
{
return (first == nullptr);
}
38. Consider the accompanying statements. The operation returns true if the
list is empty; otherwise, it returns false. The missing code is ____.
a.
protected
b.
int
c.
void
d.
bool
39. Consider the following code:
template <class Type>
int doublyLinkedList<Type>::length() const
{
____
}
The statement that provides the length of the linked list is ____.
a.
cout <<< count;
b.
destroy();
c.
return count;
d.
return next;
40. Consider the following code which deletes all the nodes in a linked list.
void doublyLinkedList<Type>::destroy()
{
nodeType<Type> *temp; //pointer to delete the node
while (first != nullptr)
{
temp = first;
first = first->next;
____
}
last = nullptr;
count = 0;
}
Which of the following is the missing statement?
a.
delete first;
b.
delete temp;
c.
destroy temp;
d.
clear temp;
Chapter 18
41. The sequential search algorithm does not require that the list be sorted.
42. In a binary search, first, the search item is compared with the last
element of the list.
43. The binary search algorithm can be written iteratively or recursively.
44. During the sorting phase of insertion sort, the array containing the list
is divided into two sublists, sorted and unsorted.
45. The sequential search algorithm uses a(n) ____ variable to track whether
the item is found.
a.
int
b.
bool
CSCI 2380
c.
Test 2 study guide MC/TF Questions
char
d.
Spring 2023
Mr. G. Dietrich
double
46. A sequential search of an n-element list takes ____ key comparisons if the
item is not in the list.
a.
0
b.
n/2
c.
n
d.
n2
47. A sequential search of an n-element list takes ____ key comparisons on
average to determine whether the search item is in the list.
a.
0
b.
n/2
c.
n
d.
n2
48. In the average case, sequential search typically searches ____.
a.
one quarter of the list
b.
one third of the list
c.
one half of the list
d.
the entire list
49. Consider the following list:
int list[] = {4, 8, 19, 25, 34, 39, 45, 48, 66, 75, 89, 95}
When performing a binary search, the target is first compared with ____.
a.
4
b.
25
c.
39
d.
95
50. Consider the following list:
int list[] = {4, 8, 19, 25, 34, 39, 45,
When performing a binary search for 75,
is restricted to ____.
a.
list[0]...list[6]
b.
c.
list[5]...list[11]
d.
48, 66, 75, 89, 95}
after the first comparison, the search
list[0]...list[7]
list[6]...list[11]
51. The formula to find the index of the middle element of a list is ____.
a.
(mid + last)/2
b.
(first + last) - 2
c.
(first + last) / 2
d.
(first + mid ) * 2
52. With the binary search algorithm, ____ key comparison(s) is/are made in the
successful case—the last time through the loop.
a.
one
b.
two
c.
n-2
d.
n
53. For the Insertion sort algorithm, select the theoretical, worst-case
running time.
a.
Θ(log(n))
b.
Θ(n)
c.
Θ(n log(n))
d.
Θ(n2)
54. For the Quicksort algorithm, select the theoretical, worst-case running
time.
a.
Θ(log(n))
b.
Θ(n)
c.
Θ(n log(n))
d.
Θ(n2)
55. For a list of length n, insertion sort makes ____ key comparisons, in the
worst case.
a.
n(n - 1)/4
b.
n(n - 1)/2
c.
n2
d.
n3
56. For the Mergesort algorithm, select the theoretical, worst-case running
time.
a.
Θ(log(n))
b.
Θ(n)
CSCI 2380
c.
Test 2 study guide MC/TF Questions
Θ(n log(n))
d.
Spring 2023
Mr. G. Dietrich
Θ(n2)
57. Which of the following correctly states the quick sort algorithm?
a.
if (the list size is greater than 1)
{
a. Partition the list into four sublists.
b. Quick sort sublist1.
c. Quick sort sublist2.
d. Quick sort sublist3.
e. Quick sort sublist4.
d. Combine the sorted lists.
}
b.
a. Find the location of the smallest element.
b. Move the smallest element to the beginning of the unsorted list.
c.
if (the list size is greater than 1)
{
a. Partition the list into two sublists, say lowerSublist and upperSublist.
b. Quick sort lowerSublist.
c. Quick sort upperSublist.
d. Combine the sorted lowerSublist and sorted upperSublist.
}
d.
if the list is of a size greater than 1
{
a. Divide the list into two sublists.
b. Merge sort the first sublist.
c. Merge sort the second sublist.
d. Merge the first sublist and the second sublist.
}
58. For the Linear search algorithm, select the theoretical, worst-case running
time.
a.
Θ(log(n))
b.
Θ(n)
c.
Θ(n log(n))
d.
Θ(n2)
59. ____ sort requires knowing where the middle element of the list is.
a.
Merge
b.
Bubble
c.
Insertion
d.
Selection
60. For the Binary search algorithm, select the theoretical, worst-case running
time.
a.
Θ(log(n))
b.
Θ(n)
c.
Θ(n log(n))
d.
Θ(n2)
Chapter 19
61. The level of the root node of a binary tree is 1.
62. All binary tree traversals start at the left-most child node.
63. The item search, insertion, and deletion operations all require the binary
tree to be traversed.
CSCI 2380
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
64. Duplicates are allowed in a binary search tree.
65. After deleting the desired item from a binary search tree, the resulting
tree must be a binary search tree.
66. A binary tree has a special node called the ____ node.
a.
super
b.
root
c.
superparent
d.
rootleaf
67. Consider that A is a binary tree, C and D are the subtrees of A. Which of
the following statements is always true?
a.
C and D are binary trees.
b.
C and D are search binary trees.
c.
C and D are empty trees.
d.
A is empty.
68. Each link in a binary tree node points to a(n) ____ of that node.
a.
parent
b.
child
c.
value
d.
sibling
69. Every node in a binary tree has at most ____ children.
a.
one
b.
two
c.
three
d.
four
70. Every node in a binary tree has ____ pointers.
a.
one
b.
two
c.
three
d.
four
71. A pointer to the root node of the binary tree is stored outside the binary
tree in a pointer variable, usually called the ____.
a.
node
b.
parent
c.
root
d.
nodeType
72. A node in a binary tree is called a(n) ____ if it has no left and right
children.
a.
edge
b.
branch
c.
leaf
d.
path
73. In a binary tree, the level of the children of the root node is ____.
a.
0
b.
1
c.
2
d.
3
74. The ____ of a node in a binary tree is the number of branches on the path
from the root to the node.
a.
height
b.
level
c.
width
d.
size
75. The three traversal algorithms discussed for binary trees are ____, ____,
and ____.
a.
order, preorder, postorder
b.
in, preorder, order
c.
order, preorder, post
d.
inorder, preorder, postorder
76. The sequence of operations in a postorder traversal is ____.
a.
traverse left; traverse right
b.
traverse left; traverse right; visit
c.
visit; traverse left; traverse right
d.
traverse left; visit; traverse right
CSCI 2380
Test 2 study guide MC/TF Questions
Spring 2023
Mr. G. Dietrich
77. A binary tree is empty if root is ____.
a.
'0'
b.
1
c.
"zero"
d.
nullptr
78. Assume the key of the left child below the root node of a binary search
tree is 30. The value in the root node could be ____.
a.
0
b.
10
c.
30
d.
40
79. The key of the right child below the root node of a search binary tree is
40. The value in the root node could be ____.
a.
30
b.
40
c.
50
d.
60
80. In a binary search tree, the data in each node is ____ the data in the left
child.
a.
larger than
b.
smaller than
c.
equal to
d.
larger or equal to
CSCI 2380
Test 2 study guide code analysis problems
What is the output of the following C++ code?
int mystery(int x, int* A, int n)
{
if (n <= 1)
if (A[n/2] != x)
return -1;
else
return A[0];
if (A[n/2] == x)
return A[n/2];
if (A[n/2] > x)
return mystery(x, A, n/2);
else
return mystery(x, &(A[n/2]), n-n/2);
}
int main()
{
int A[11] = {2,4,7,9,11,15,16,17,20,21,23};
cout << mystery(7, A, 11) << ',' << mystery(18, A, 11);
}
Spring 2023
Mr. G. Dietrich
CSCI 2380
Test 2 study guide code analysis problems
Spring 2023
Mr. G. Dietrich
Suppose the content of a doubly-linked-list-based deque of strings is (from front to tail)
"alpha","beta","gamma"; what would the content of the list be after the enigma() function is
executed. List the VALUES separated by a comma please. Do NOT include the double quotes
and do NOT include blank spaces in your answer (just the strings separated by commas).
class Deque
{
public:
void enigma();
private:
class Node {
public:
string s;
Node* next;
Node* prev;
};
Node* head;
Node* tail;
int count;
};
void Deque::enigma()
{
if (tail == head || tail == nullptr)
return;
Node* cur = tail;
while (true)
{
if (cur == nullptr)
break;
Node* tmp = cur->next;
cur->next = cur->prev;
cur->prev = tmp;
cur = cur->next;
}
Node* tmp = tail;
tail = head;
head = tmp;
}
CSCI 2380
Test 2 study guide code analysis problems
What is the output of the following C++ code?
void print(int x)
{
if (x == 0)
{
cout << x;
return ;
}
else
{
cout << x;
print(x-1);
cout << x;
}
}
int main()
{
Print(4);
}
Spring 2023
Mr. G. Dietrich
CSCI 2380
Test 2 study guide code analysis problems
Spring 2023
Mr. G. Dietrich
Given the following unsorted list of numbers (numbers in first row are index numbers while
numbers in the second row are the values in the list):
[0]
5
[1]
-1
[2]
9
[3]
3
[4]
4
[5]
6
[6]
0
[7]
-5
[8]
-9
[9]
7
[10]
1
List the VALUES from left to right that you would end up with after the first partition of the list
if you used the QUICKSORT algorithm (use the second method shown in the Powerpoint
slides) to sort the list. Assume that the pivot is picked from the first element in the list. Use 1
(one) comma to separate the values but put the PIVOT between 2 (two) commas. For example,
suppose that P represents the value of the pivot and x represent the values that still need to
be sorted, you answer should look like: x,x,,P,,x,x. Do NOT include blank spaces in your
answer.
CSCI 2380
Test 2 study guide code analysis problems
What is the output of the following C++ code?
void yas(int a)
{
// Base case
if (a == 3)
{
cout << 'a' << ',';
return;
}
// Recursive case
for (int i = 1; i <= a; ++i)
yas(a+1);
}
int main()
{
yas(1);
}
Spring 2023
Mr. G. Dietrich
CSCI 2380
Test 2 study guide code analysis problems
Spring 2023
Mr. G. Dietrich
Given an empty binary search tree, if you inserted in the order provided the following values:
"dog", "bat", "noon", "farm", "zoo", "apple", "say", "hip"
What would the list of values look like if they were printed out using POSTORDER traversal?
List the VALUES separated by a comma please. You answer should look like: x,x,x,x. Do NOT
include the double quotes and do NOT include blank spaces in your answer (just the words
separated by commas).
The depth of the node containing "dog" is:
The depth of the node containing "say" is:
The number of internal nodes in the BST is:
The height of the BST is:
CSCI 2380
Test 2 study guide code completion problems
Spring 2023
Mr. G. Dietrich
Complete the following implementation of the push_front(string s) method that inserts a node
at the front of the doubly-linked-list-based deque shown below, where the front of the deque
is at the head of the linked list. The data in each Node is a string stored in a variable named s.
class Deque
{
public:
void push_front(string s);
private:
class Node {
public:
string s;
Node* next;
Node* prev;
};
Node* head;
Node* tail;
int count;
};
void Deque :: push_front(string s) {
Node* p = new Node;
p->s = s;
p->next = p->prev = _____;
if (count == 0) {
head = tail = ______;
++count;
return;
}
p->next = _____;
p->next->prev = _____;
head = ______;
++count;
}
CSCI 2380
Test 2 study guide code completion problems
Spring 2023
Mr. G. Dietrich
Complete the sCCount() function shown below that returns the number of nodes with a single
child in a binary tree:
class Node
{
public:
Node(int n) {
this->n = n;
left = right = nullptr;
}
int n;
Node* left;
Node* right;
};
int sCCount(Node *p)
{
if (p == _____)
return 0;
else if ((_____ != nullptr && _____ == nullptr) ||
(_____ == nullptr && _____ != nullptr))
return ____ + sCCount(_____) + sCCount(_____);
else
return sCCount(_____) + sCCount(_____);
}
CSCI 2380
Test 2 study guide code completion problems
Spring 2023
Mr. G. Dietrich
Complete the recursive function that computes the number of leaves in a binary tree.
int leaf_count(Node* root)
{
if (root _____ nullptr)
return 0 ;
int sc = 0;
sc += leaf_count( __________ );
sc += leaf_count( __________ );
if (root->left == nullptr && root->right == nullptr)
return _____ + sc;
return _____ + sc;
}
CSCI 2380
Test 2 study guide code completion problems
Spring 2023
Mr. G. Dietrich
Complete the following function that returns how many elements of a sorted array A of length
n are greater than or equal to x.
int count (int x, int *A, int n)
{
int ____ = 0;
int ____ = n - 1;
while (l + 10 < r)
{
int m = (____+____) / 2;
if (x > A[____])
_____ = m + 1;
else
_____ = m;
}
int i = l;
while (A[i] < x _____ i < n)
++i;
return _____ - _____;
}
CSCI 2380
Test 2 study guide code completion problems
Spring 2023
Mr. G. Dietrich
Complete the following function that receives a vector of strings and returns it sorted.
#include <vector>
using namespace std;
void sort(vector<_____> _____V)
{
for (int r = V.size(); r > 0; --r)
{
int i = V.size() - _____;
while (i > 0)
{
If (V[i-1] > V[i])
{
_____ tmp = V[i];
V[i] = V[i-1];
_____ = tmp;
}
--i;
}
}
}
Download