CS2

Ace your homework & exams now with Quizwiz!

In the following function, how many recursive calls are there? void towers(char source, char dest, char help, int numDisks) { if (numDisks<1) { return; } else { towers(source,help,dest,numDisks-1) cout << "Move disk from" << source << " to " << dest << endl; towers(help,dest,source,numDisks-1) } } A. 2 B. 0 C. 1 D. 3

A. 2

What is the output of the following code fragment? int f1(int x,int y) { if (x<0 || y<0) { return x-y; } else { std::cout << x << ":" << y << std::endl; return f1(x-1,y) + f1(x,y-1); } } int main() { std::cout << f1(2,1); } A. 5 B. -5 C. 0 D. -1

A. 5

The following catch statement catch(...): A. Catches all exceptions B. Should be the first catch block of multiple catch statements are needed C. Catches only numeric exceptions D. Is illegal

A. Catches all exceptions

The root node points to two other nodes, referred to as: A. Child nodes, or children B. Parent nodes, or parents C. Binary nodes D. Subnodes E. None of these

A. Child nodes, or children

When a throw statement is executed: A. Execution of the try block stops B. The program always exits C. Execution of the catch block stops D. Execution of the throw block stops

A. Execution of the try block stops

The parameter in the catch statement: A. Identifies what type of exceptions are caught B. Must always be an e C. Identifies the different number of exceptions that can be caught D. Makes the catch block a function

A. Identifies what type of exceptions are caught

In the binary search program, each time through the list, we cut the list approximately: A. In half B. In quarters C. In thirds D. By one element

A. In half

If you have a function that might throw an exception and some programs that use that function might want to handle that exception differently, you should: A. Not catch the exception in the function B. Throw an integer exception C. Never throw an exception in this function D. None of these

A. Not catch the exception in the function

The time to find an element is the same for a set or a map. It is: A. O(log N) B. O(N^1/2) C. O(N) D. O(N^2) E. O(1)

A. O(log N)

You should use exception handling: A. Only when you cannot handle the exception with simpler control structures B. In all of your programs C. Only when you use classes D. In every function

A. Only when you cannot handle the exception with simpler control structures

Inorder, Preorder, and Postorder traversals can be accomplished using ________. A. Recursion B. No pointers C. No arguments D. No parameters E. None of these

A. Recursion

The ________ in a binary tree is similar to the head pointer in a linked list: A. Root pointer B. Leaf pointer C. Null pointer D. Binary pointer E. None of these

A. Root pointer

If the following function will throw a string exception, then void myFunction( ); A. The function definition and declaration should have a throw list B. The function definition, but not the declaration should have a throw list C. The function should have an empty throw list D. All of these

A. The function definition and declaration should have a throw list

In order for the binary search to work correctly: A. The list must be sorted B. The item must exist C. All of these D. None of these

A. The list must be sorted

What is wrong with the following recursive function? It should print out the array backwards. void print(int array[], int start, int size) { if (start < size) { return } else { print(array, start+1, size); cout << array[start] << endl; } } A. The stopping condition is wrong B. Infinite recursion C. The recursive call is wrong D. Nothing

A. The stopping condition is wrong

Which of the following is NOT a valid reason for using exception handling? A. Throw and catch can be used as a goto B. The procedure for handling an error depends on the situation C. Need to handle built in exceptions D. None of these

A. Throw and catch can be used as a goto

A strong reason to use the binary tree structure is: A. To expedite the process of searching large sets of information B. Aesthetics and program design C. Code readability D. It is more flexible than unary tree structures E. None of these

A. To expedite the process of searching large sets of information

Which type of exception is thrown if a call to the new operator fails? A. bad_alloc B. MemoryError C. ArithmeticError D. DivideByZero

A. bad_alloc

The recursive definition of a Fibonacci Number is F(n) = F(n-1) + F(n-2), where F(0)=1 and F(1)=1. What is the stopping case in a recursive function that implements this function? A. n=0 and n=1 B. n=1 C. n=0 and n=2 D. n=0 E. n=2 F. n=0 n=1 and n=2

A. n=0 and n=1

Given the following recursive function definition, what is the stopping case? void towers(char source, char dest, char help, int numDisks) { if (numDisks<1) { return; } else { towers(source,help,dest,numDisks-1) cout << "Move disk from" << source << " to " << dest << endl; towers(help,dest,source,numDisks-1) } } A. numDisks < 1 B. numDisks > 1 C. numDisks = 0 D. numDisks == 1

A. numDisks < 1

I have an algorithm that runs in O(N2), where Nis the size of the problem. For N = 100, the time for the algorithm to run is 1 minute. How long does the algorithm take for N=1000? A. The same time B. 100 minutes C. 1000 minutes D. 10 minutes

B. 100 minutes

The recursive definition of a Fibonacci Number is F(n) = F(n-1) + F(n-2), where F(0)=1 and F(1)=1. What is the value of Fib(3)? A. 5 B. 2 C. 8 D. 1

B. 2

What is the output of the following code fragment? int f1(int n, int m) { if (n>m) return -1; else { if (n==m) { return 1; } else { return n * f1(n+1,m); } } } int main() { cout << f1(2,4); } A. 2 B. 6 C. 3 D. -1

B. 6

int f1(int n, int m) { if (n<m) { return 0; } else if (n==m) { return m+f1(n-1,m); } else { return n+f1(n-2,m-1) } } int main() { cout << f1(5,4) } A. 4 B. 8 C. 2 D. Infinite recursion E. 0

B. 8

Which of the following would be a good reason for using inherited exception classes? A. A base class exception can be passed to an exception parameter of the derived class B. A derived class exception can be passed to an exception parameter of the base class C. A base class exception parameter can be passed any type of exception D. All of these

B. A derived class exception can be passed to an exception parameter of the base class

I have an algorithm that runs in O(N1/2), where n is the size of the problem. For N = 100, the time the algorithm runs is 1 minute. How long does the algorithm take for N=1000? A. You haven't given enough information. I can't tell. B. About 3 minutes C. Same time D. About 10 minutes

B. About 3 minutes

A catch block that expects an integer argument will catch: A. All exceptions B. All integer exceptions C. Any exception value that can be coerced into an integeer D. None of these

B. All integer exceptions

When you dereference a pointer to a pointer, the result is: A. A value of the data type pointed to B. Another pointer C. Not possible to determine D. NULL E. None of these

B. Another pointer

If a function will possibly throw an unhandled exception, the try block should: A. Be in the function definition B. Encompass the function call C. Not be used D. Be in the catch block

B. Encompass the function call

The following class definition class MyError {}; A. Has no member function or member data B. Has only a default constructor C. Has no member function of member data and has only a default constructor D. Is illegal

B. Has only a default constructor

If class A is derived from class B, and a virtual function from class B throws an exception, then the overridden version of that function in class A must: A. Not throw any exceptions B. Have an exception specification that is a subset of the exception specification of the base class B C. Not throw any exceptions that the function in class B may throw D. All of these

B. Have an exception specification that is a subset of the exception specification of the base class B

In a binary tree, a node that has more than two children: A. Will be cut back by the compiler B. Is theoretically impossible in a correctly-developed binary tree structure C. Is known as a triplet node D. None of these

B. Is theoretically impossible in a correctly-developed binary tree structure

When an unusual situation or error occurs, then the ________ statement is executed. A. Exiting B. Throw C. Error D. Try

B. Throw

Stepping through the nodes of a tree is known as: A. Climbing B. Traversing C. Walking through D. Branching out E. None of these

B. Traversing

The head pointer, anchored at the top of a tree, is called the: A. Root node B. Tree pointer C. Binary pointer D. Either root node or binary pointer E. None of these

B. Tree pointer

Values are typically stored in a binary search tree so that a node's ________ child holds data is less than the ________ data A. right, node's B. left, node's C. right, left child's D. left, right child's E. None of these

B. left, node's

The recursive definition of a Fibonacci Number is F(n) = F(n-1) + F(n-2), where F(0)=1 and F(1)=1. What would be the recursive function call in a recursive implementation of this? A. return 1 B. return fib(n-1) + fib(n-2) C. return fib(n) + fib(n-1) D. return;

B. return fib(n-1)+fib(n-2)

Which of the following function declaration correctly specifies that two types of exceptions are thrown? A. void f1(exception a, exception b); B. void f1() throw(a,b); C. void f1() throw a, throw b; D. void f1() exception(a;b);

B. void f1() throw(a,b);

What is the output of the following code fragment? int f1(int n, int m) { if (n>m) return -1; else { if (n==m) { return 1; } else { return n + f1(n+1,m); } } } int main() { cout << f1(12,4); } A. 3 B. 6 C. -1 D. 2

C. -1

The recursive definition of a Fibonacci Number is F(n) = F(n-1) + F(n-2), where F(0)=1 and F(1)=1. What is the value of Fib(5)? A. 5 B. 1 C. 8 D. 2

C. 8

A definition that defines a concept or a formula in terms of the concept or formula is called: A. Reduction B. Iteration C. A recursive function D. Indecision

C. A recursive function

The throw statement is enclosed in: A. Quotes B. A throw block C. A try block D. A catch block

C. A try block

When a binary tree is used to facilitate a search, it is referred to as: A. Binary queue B. Binary ordered deque C. Binary search tree D. Sort algorithm E. None of these

C. Binary search tree

If a function throws an exception: A. It causes a syntax error B. It must be caught in that function C. It may be caught in that function D. It can only be a non-numeric exception

C. It may be caught in that function

A stack exhibits ________ behavior A. First in first out B. Last in last out C. Last out first in D. Undefined

C. Last out first in

A node that has no children is a: A. Root node B. Head node C. Leaf node D. Pure binary node E. None of these

C. Leaf node

In a non-linear linked list, a node can point to: A. Only the next node in sequence B. Only the previous node in sequence C. More than one other node, plus the previous node in sequence D. All of the other nodes in the list E. None of these

C. More than one other node, plus the previous node in sequence

Binary trees can be divided into: A. Branches B. Leaves C. Subtrees D. Sawdust E. None of these

C. Subtrees

Given the following function definition, what happens if the function throws the exception? void f1() throw(double) { if(//some code) { throw 12 } } A. This code has a syntax error B. The 12 will be converted to 12.0 C. The function will cause the program to exit D. The function will throw an integer exception which is passed to the calling code

C. The function will cause the program to exit

When an application begins searching a binary tree, it starts at: A. The outermost leaf node B. The middle node, halfway between the root and the longest branch C. The root node D. The rightmost child of the root node E. None of these

C. The root node

In a binary tree class, you usually have a pointer as a member that is set to: A. The leftmost child node B. The first leaf node C. The root of the tree D. The deepest leaf node E. None of these

C. The root of the tree

The block of code that checks if an unusual situation or error occurs is called: A. An error block B. The catch block C. The try block D. A function

C. The try block

A tree with a height of three has: A. Six nodes B. One root and three nodes with two children each C. Three levels D. Three subtrees E. None of these

C. Three levels

A binary tree can be created using a struct or class containing a data value and: A. A pointer to the first child node B. A pointer to the last child node C. Two pointers, one for the left child and one for the right child D. Two data nodes E. None of these

C. Two pointers, one for the left child and one for the right child

The square of n can be calculated by noting that square(n) = square(n-1) + diff(n-1). diff(n) = diff(n-1)+2. The square(0)=0, diff(0)=1. What is the stopping condition for this recursive definition? A. Unknown B. n=-1 C. n=0 D. n=1

C. n=0

The factorial of an integer is the product of that integer multiplied by all the positive non-zero integers less than that integer. So, 5! (! is the mathematical symbol for factorial) is 5 * 4 * 3*2*1. 4! is 4*3*2*1, so 5! could be written as 5*4!. So a recursive definition of factorial is n! is n*(n-1)!, as long as n >1. 1! is 1. What is the stopping case for this function? A. n<1 B. n==0 C. n==1 D. None of these

C. n==1

What is the output of the following code fragment? int f1(int x,int y) { if (x<0 || y<0) { return x-y; } else { std::cout << x << ":" << y << std::endl; return f1(x-1,y) + f1(x,y-1); } } int main() { std::cout << f1(1,2); } A. 0 B. 5 C. -1 D. -5

D. -5

Every time a recursive function call is executed, a new ________ is put on the top of the stack. A. Activity record B. Program C. Activity frame D. Activation frame

D. Activation frame

An operation that can be performed on a binary search tree is: A. Insertion B. Finding C. Deleting D. All of these E. None of these

D. All of these

Binary trees may be implemented as templates, but any data types used with them must support the ________ operator. A. < B. > C. == D. All of these E. None of these

D. All of these

Deleting a node that has two children offers an opportunity to use: A. A function that returns a pointer to a pointer B. A function parameter that is a pointer to a pointer C. Double indirection D. All of these E. None of these

D. All of these

In a binary tree, each node may point to ________ other nodes. A. No B. One C. Two D. All of these E. None of these

D. All of these

Methods of traversing a tree are: A. Inorder traversal B. Preorder traversal C. Postorder traversal D. All of these E. None of these

D. All of these

To ensure that your function recurses correctly and the proper result is reached, you must ensure that: A. All stopping cases are correct B. All recursive calls lead to one of the stopping cases C. For each case that involves recursion, that case returns the correct value, provided all recursive calls in that case return the correct value D. All of these E. None of these

D. All of these

The shape of a binary tree is: A. Always triangular B. Always balanced C. Determined by the programmer D. Determined by the order in which values are inserted E. None of these

D. Determined by the order in which values are inserted

Which of the following does NOT have STL containers types? A. Container adapters B. Sequence containers C. Associative containers D. Generic functions

D. Generic functions

What is wrong with the following recursive function? It should print out the array backwards: void print(int array[], int start, int size) { if (start==size) { return; } else { print(array,start-1,size); cout << array[start] << endl; } A. The recursive call is wrong B. The stopping condition is wrong C. Infinite recursion D. Infinite recursion and the recursive call is wrong E. Nothing

D. Infinite recursion and the recursive call is wrong

All node pointers that do not point to other nodes are set to: A. The root of the tree B. A parent node C. Their left-most child node D. NULL E. None of these

D. NULL

Which of the following fragments are illegal? A. try { try { //other code } catch (int e) { //code here } } catch (float e) { //code here } B. try { //code here } catch (int e) { //code here try { //code here } catch (string e) { } } C. All of these D. None of these

D. None of these

If your program makes too many recursive function calls, your program will cause a ________. A. Syntax error B. Activation overflow C. Stack underflow D. Stack overflow

D. Stack overflow

What is the output of the following code fragment? int f1(int n, int m) { if (n<m) return 0; else if (n==m) return m + f1(n-1,m); else return n + f1(n-2,m-1); } int main() { cout << f1(5,4) } A. Infinite recursion B. 2 C. 0 D. 4 E. 8

E. 8

A throw statement can throw: A. An integer exception B. A float exception C. A boolean exception D. An exception of any data type E. All of these F. None of these

E. All of these

A class that is used for exceptions is declared: A. Differently from other classes B. Specialized only for exceptions C. May not have objects declared of that class D. All of these E. None of these

E. None of these


Related study sets

Chapter 19: Cholinergic Agonists and Anticholinergics

View Set

Chapter 6: Corporate Forms of Business Ownership

View Set

SkillsUSA - Criminal Justice Trivia Bowl Terms

View Set