Exam #2 Computer Science
best case
------ scenario of an algorithm happens when the algorithm is working on the easiest input or the minimum number of operations.
Find the order of growth for pushing a value onto a Stack.
1
Consider the following code; What is the value of mystery(2,14)? public static int mystery(int a, int b){if (b == 0) return 0;if (b%2 == 0) return mystery(a+a,b/2);return mystery(a+a,b/2)+a;
28//// multiplication
for(row=0;row<N;row++){for (col=o;col<N;com++){p=a[row][col]}}
3N2+4N+2
In the worst case, how many letters do you need to check to find a specific letter in an ordered list of English letters.
4
Constant time operations Which of the following statements is true with reference to an algorithm's runtime?
A single algorithm can execute more quickly on a faster processor
publicinc void show() { Node current = first; while (current != null) { System.out.print(current.item + " "); current = first.next; } }
The statement current = first.next; should be Node current = curent.next;
Which statement is true of linked list traversal.
for an empty linked list, we traverse 0 nodes
The order of growth of which code is constant? N is input size.
int[] arr = new int[30];for(int i = 0; i < arr.length; i++)arr[i] = 0;
Which of the following statements is true about the order of growth of linear and binary search algorithms?
linear search is lenear and binary search is logarithmic
Find the order of growth for finding the largest value in an array with n elements. Assume n is a positive integer.
n
What code adds newNode after the currentNode in the following linked list.
newNode.next = current.next; current.next = newNode;
An algorithm is developed to find a name starting with a given letter in a list. What is the worst case scenario to find a name starting with letter 'X' in a list of student names.
no names in the list begin with X
Which of the following is an example of a linked list traversal operation?
printing the list
Which code defines class Node :
private class Node{ private String item; private Node next;}
Which option is not correct about worst case of an algorithm.
provides a guarantee for all inputs
A method that calls itself is a ________ method.
recursive
Big oh notation delve lopes the -------- of an algorithm.
the upper bound
You use this statement to throw an exception manually.
throw
Any problem that can be solved recursively can also be solved iteratively, with a loop.
true
Recursion is never absolutely required to solve a problem.
true
Tilde approximate is the same as Big Oh notation.
true
We consider the CPU type when we analyze an algorithm,
false
You cannot have more than one catch clause per try statement.
false
The order of growth of which code is not constant?
finding the total of all elements in an array
This method can be used to retrieve the error message from an exception object.
getMessage
Consider the following LinkedList. Which XXX completes the following algorithm to remove 18 from the linked list (18, 20, 22, 24).
head = head.next
Which is the correct code to prepend an item to a list?
public void prepend(Item item) { Node newNode = new Node(); newNode.item = item; newNode.next = first; first = newNode; size++; }
Suppose x is a linked-list node and not the last node on the list. What is the effectof the following code fragment?x.next = x.next.next;
Deletes the first node in the linked list
n.next = h.next; h.next = n;
Inserts node n immediately after node h
Which of the following codes does add a new node to the beginning of the linked-List.
Node oldfirst = first; first = new Node(); first.next = oldfirst;
Give the order of growth (as a function of N) of the running times of the following code fragment. int sum = 0; for (int i = 1; i < N; i *= 2) for (int j = 0; j < N; j++) sum++;
O(N2)O(N2) quadratic or
The ________ is at least one case in which a problem can be solved without recursion.
base case
This method can be used to retrieve the error message from an exception object.
call stack
A recursive method can have only one base case.
false
An algorithm finds the minimum number in a 2-D array. This is a time constant algorithm.
false
When in the same try statement you are handling multiple exceptions and some of the exceptions are related to each other through inheritance, you should handle the more general exception classes before the more specialized exception classes.
false
Without a base case, a recursive method will call itself only once and stop.
false
Which of the following loops does traverse correctly through the Linked-List.
for (Node current = first; current != null; current = current.next) { System.out.println(current.item + ",");
The purpose of analyzing algorithms is to avoid algorithms with _____.
high memory usage and high runtime