You are on page 1of 40

Multiple Choice 1. What is the worst-case time for serial search finding a single item in an array? o A.

Constant time o B. Logarithmic time o C. Linear time o D. Quadratic time 2. What is the worst-case time for binary search finding a single item in an array? o A. Constant time o B. Logarithmic time o C. Linear time o D. Quadratic time 3. What additional requirement is placed on an array, so that binary search may be used to locate an entry? o A. The array elements must form a heap. o B. The array must have at least 2 entries. o C. The array must be sorted. o D. The array's size must be a power of two. 4. What is the best definition of a collision in a hash table? o A. Two entries are identical except for their keys. o B. Two entries with different data have the exact same key. o C. Two entries with different keys have the same exact hash value. o D. Two entries with the exact same key have different hash values. 5. Which guideline is NOT suggested from from empirical or theoretical studies of hash tables: o A. Hash table size should be the product of two primes. o B. Hash table size should be the upper of a pair of twin primes. o C. Hash table size should have the form 4K+3 for some K. o D. Hash table size should not be too near a power of two. 6. In an open-address hash table there is a difference between those spots which have never been used and those spots which have previously been used but no longer contain an item. Which function has a better implementation because of this difference? o A. insert o B. is_present o C. remove o D. size o E. Two or more of the above functions 7. What kind of initialization needs to be done for an open-address hash table? o A. None. o B. The key at each array location must be initialized. o C. The head pointer of each chain must be set to NULL. o D. Both B and C must be carried out. 8. What kind of initialization needs to be done for an chained hash table? o A. None. o B. The key at each array location must be initialized.

C. The head pointer of each chain must be set to NULL. D. Both B and C must be carried out. 9. A chained hash table has an array size of 512. What is the maximum number of entries that can be placed in the table? o A. 256 o B. 511 o C. 512 o D. 1024 o E. There is no maximum. 10. Suppose you place m items in a hash table with an array size of s. What is the correct formula for the load factor? o A. s + m o B. s - m o C. m - s o D. m * s o E. m / s
o o

Multiple Choice 1. One difference between a queue and a stack is: o A. Queues require dynamic memory, but stacks do not. o B. Stacks require dynamic memory, but queues do not. o C. Queues use two ends of the structure; stacks use only one. o D. Stacks use two ends of the structure, queues use only one. 2. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? o A. ABCD o B. ABDC o C. DCAB o D. DCBA 3. Which of the following expressions evaluates to true with approximate probability equal to P? (P is double and 0 <= P <= 1). o A. rand() < P o B. rand() > P o C. rand() < P * RAND_MAX o D. rand() > P * RAND_MAX 4. Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data[2] through data[11]. The CAPACITY is 42. Where does the push member function place the new entry in the array? o A. data[1] o B. data[2] o C. data[11] o D. data[12] 5. Consider the implementation of the queue using a circular array. What goes wrong if we try to keep all the items at the front of a partially-filled array (so that data[0] is always the front).

A. The constructor would require linear time. B. The get_front function would require linear time. C. The insert function would require linear time. D. The is_empty function would require linear time. 6. In the linked list implementation of the queue class, where does the push member function place the new entry on the linked list? o A. At the head o B. At the tail o C. After all other entries that are greater than the new entry. o D. After all other entries that are smaller than the new entry. 7. In the circular array version of the queue class (with a fixed-sized array), which operations require linear time for their worst-case behavior? o A. front o B. push o C. empty o D. None of these operations require linear time. 8. In the linked-list version of the queue class, which operations require linear time for their worst-case behavior? o A. front o B. push o C. empty o D. None of these operations require linear time. 9. If data is a circular array of CAPACITY elements, and last is an index into that array, what is the formula for the index after last? o A. (last % 1) + CAPACITY o B. last % (1 + CAPACITY) o C. (last + 1) % CAPACITY o D. last + (1 % CAPACITY) 10. I have implemented the queue with a circular array, keeping track of first, last, and count (the number of items in the array). Suppose first is zero, and last is CAPACITY-1. What can you tell me about count? o A. count must be zero. o B. count must be CAPACITY. o C. count could be zero or CAPACITY, but no other values could occur. o D. None of the above. 11. I have implemented the queue with a linked list, keeping track of a front pointer and a rear pointer. Which of these pointers will change during an insertion into a NONEMPTY queue? o A. Neither changes o B. Only front_ptr changes. o C. Only rear_ptr changes. o D. Both change. 12. I have implemented the queue with a linked list, keeping track of a front pointer and a rear pointer. Which of these pointers will change during an insertion into an EMPTY queue? o A. Neither changes
o o o o

B. Only front_ptr changes. C. Only rear_ptr changes. D. Both change. 13. Suppose top is called on a priority queue that has exactly two entries with equal priority. How is the return value of top selected? o A. The implementation gets to choose either one. o B. The one which was inserted first. o C. The one which was inserted most recently. o D. This can never happen (violates the precondition)
o o o

Multiple Choice on Introduction 1. Why is writing easily modifiable code important? o A. Easily modifiable code generally has a quicker run time. o B. Most real world programs require change at some time. o C. Most text editors make it easy to modify code. o D. Several people may be writing the same function at the same time. 2. When a method is called, who is responsible for ensuring that the precondition is valid? o A. The person who is using the program. o B. The programmer who called the method. o C. The programmer who wrote the method. o D. The programmer who implemented the Java Runtime System. 3. What will happen if a method is executed and the precondition for the method is not met? o A. An exception will be thrown. o B. The program will loop indefinitely. o C. The system will crash. o D. Any of the above results could happen. 4. Answer true or false for this statement: When programming in teams, the specification of a method must be written by the person writing the code for the method. o TRUE. o FALSE. 5. Answer true or false for this statement: When programming individually (not in a team), there is no need to write a specification for a method. o TRUE. o FALSE. 6. If the precondition fails, it is a good idea to throw an exception that will generally halt the program. Why is the program halted? o A. The Java Runtime System forbid continuation. o B. The method is no longer guaranteed to make the postcondition true. o C. The method's memory requires have become exponential (or worse). o D. The method's running time has become exponential (or worse).

7. What information do you need to see in order to make effective use of the printNumber method in Section 1.1? o A. Just the method's implementation. o B. Just the method's specification. o C. Both the implementation and specification. 8. Which of these function calls will cause an excpetion to be thrown when x is 42. (x is an int variable). o A.
o o o if (0 < x) throw new IllegalArgumentExcpetion("Bad x");

o o o

B.
if (0 == x) throw new IllegalArgumentExcpetion("Bad x");

o o o

C.
if (0 > x) throw new IllegalArgumentExcpetion("Bad x");

D. None of the above will cause an exception when x is 42. 9. What does a run-time analysis usually count? o A. The number of arithmetic and other operations required for the program to run o B. The number of megabytes required for the program to run o C. The number of seconds required for the program to run o D. The number of seconds plus the number of megabytes o E. The number of seconds times the number of megabytes 10. Which of these is the correct big-O expression for 1+2+3+...+n? o A. O(log n) o B. O(n) o C. O(n log n) o D. O(n) 11. Which of the following formulas in big-O notation best represent the expression n+35n+6? o A. O(n) o B. O(n) o C. O(n) o D. O(42) 12. Answer true or false for this statement: For all possible inputs, a linear algorithm to solve a problem must perform faster than a quadratic algorithm to solve the same problem. o TRUE. o FALSE.
o

13. Answer true or false for this statement: True or false: An algorithm with worst case time behavior of 3n takes at least 30 operations for every input of size n=10. o TRUE. o FALSE. 14. What term is used to describe an O(n) algorithm. o A. Constant o B. Linear o C. Logarithmic o D. Quadratic 15. Here is some code for an integer variable n:
while (n > 0) { n = n/10; // Use integer division }

What is the worst-case time analysis for the above loop? A. O(1) B. O(log n) C. O(n) D. O(n) 16. Express the formula (n - 2)*(n - 4) using big-O notation: o A. O(1) o B. O(8) o C. O(log n) o D. O(n) o E. None of the above 17. Why is it important to test boundary values when testing programs? o A. Calculating by hand, it's easy to find the right answers for boundary values. o B. Debuggers are easier to use when testing boundary values. o C. In practice, a large proportion of errors arise from boundary values. o D. The correct execution of a function on all boundary values proves a function is correct. 18. How may boundary values for a method be found? o A. Pick values that are one step away from different behavior. o B. Pick values that make the precondition equal to the postcondition. o C. Pick values where the precondition is false. o D. Pick values where the postcondition is false. 19. Which software tool will best help you determine whether your test cases are fully exercising your code? o A. Compiler o B. Debugger o C. Make o D. Pine
o o o o

E. Profiler

1 - Queue is the LIFO structure True False

2 In binary search tree (BST) every node has two or zero node.

True False

3- In Stack we can access elements from both ends

True False

4 Each node of linked list contains data element and pointer.

True False

5 - Every AVL is binary search tree (BST).

True False

6 - Which of the following is primitive type?

Byte String Integer Float Choice (C) & (D)

7 Each entry in a linked list is a called a ---------

Link Node Data Structure None of above

8 Traversing a binary tree can only be done by a recursive algorithm.

True

False

9 Stacks and queues are

primitive data structures non-primitive data structures non linear data structures. Data types

10 The constructor of the Hashtable class initializes data members and creates the hashtable.

True False

11 Which one is not Divide and Conquer algorithm?

Merge sort Quick sort Heap sort None of the above

12 Hash tables are very good if there are many insertions and deletions.

True False

13 A table is always a two dimensional array of same data type

True False

14 When there is a collision, we try to find some other place in our array. This approach is called

open addressing closed hashing open addressing & closed hashing none of the above

15 - ----------------------- is / are called algorithm (s)

merge sort quick sort heap sort all of the above

1. A cyclic graph has: a. a shape like a unicycle b. a graph that cycles from vertices to edges c. a cycle of two or more nodes d. a cycle of three or more nodes e. None of the above I'd say d, because a cycle graph consists of a single cycle (some number of vertices connected in a closed chain) 2. A(n) _______________________ graph is a graph in which each vertex has a connection to every other vertex. a. directed b. cyclic c. acyclic d. fully connected e. None of the above I'd say e, because I belive it should be either a connected graph or a strongly connected graph, depending on where you read about it. Fully connected sounds close, but I can't find a single reference to that. 3. A(n) ____________________ is a graph in which each connection has two directions. a. undirected graph b. bigraph c. bidirectional graph d. TS graph e. None of the above

Question Details: Question: Short questions are given below, Please answer yes or no, some have four answers so choose one which is correct.Thanks.

1 - Queue is the LIFO structure True False

2 In binary search tree (BST) every node has two or zero node. True False

3- In Stack we can access elements from both ends True False

4 Each node of linked list contains data element and pointer. True False

5 - Every AVL is binary search tree (BST). True False

6 - Which of the following is primitive type? Byte String Integer Float Choice (C) & (D)

7 Each entry in a linked list is a called a -------- Link Node Data Structure None of above

8 Traversing a binary tree can only be done by a recursive algorithm. True

False

9 Stacks and queues are primitive data structures non-primitive data structures non linear data structures. Data types

10 The constructor of the Hashtable class initializes data members and creates the hashtable. True False

11 Which one is not Divide and Conquer algorithm? Merge sort Quick sort Heap sort None of the above

12 Hash tables are very good if there are many insertions and deletions. True False

13 A table is always a two dimensional array of same data type True False

14 When there is a collision, we try to find some other place in our array. This approach is called

open addressing closed hashing open addressing & closed hashing none of the above

15 - ----------------------- is / are called algorithm (s) merge sort quick sort heap sort all of the above

Multiple choice questions on stacks


Multiple Choice 1. Entries in a stack are "ordered". What is the meaning of this statement? o A. A collection of stacks can be sorted. o B. Stack entries may be compared with the '<' operation. o C. The entries must be stored in a linked list. o D. There is a first entry, a second entry, and so on. 2. The operation for adding an entry to a stack is traditionally called: o A. add o B. append o C. insert o D. push 3. The operation for removing an entry from a stack is traditionally called: o A. delete o B. peek o C. pop o D. remove 4. Which of the following stack operations could result in stack underflow? o A. is_empty o B. pop o C. push o D. Two or more of the above answers 5. Which of the following applications may use a stack? o A. A parentheses balancing program. o B. Keeping track of local variables at run time.

C. Syntax analyzer for a compiler. D. All of the above. 6. Consider the following pseudocode:
o o declare a stack of characters while ( there are more characters in the word to read ) { read a character push the character on the stack } while ( the stack is not empty ) { write the stack's top character to the screen pop a character off the stack }

What is written to the screen for the input "carpets"? A. serc B. carpets C. steprac D. ccaarrppeettss 7. Here is an INCORRECT pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced:
o o o o declare a character stack while ( more input is available) { read a character if ( the character is a '(' ) push it on the stack else if ( the character is a ')' and the stack is not empty ) pop a character off the stack else print "unbalanced" and exit } print "balanced"

Which of these unbalanced sequences does the above code think is balanced? A. ((()) B. ())(() C. (()())) D. (()))() 8. Consider the usual algorithm for determining whether a sequence of parentheses is balanced. What is the maximum number of parentheses that will appear on the stack AT ANY ONE TIME when the algorithm analyzes: (()(())(()))? o A. 1
o o o o

B. 2 C. 3 D. 4 E. 5 or more 9. Consider the usual algorithm for determining whether a sequence of parentheses is balanced. Suppose that you run the algorithm on a sequence that contains 2 left parentheses and 3 right parentheses (in some order). What is the maximum number of parentheses that will ever appear on the stack AT ONE TIME during the computation? o A. 1 o B. 2 o C. 3 o D. 4 o E. 5 or more 10. Suppose we have an array implementation of the stack class, with ten items in the stack stored at data[0] through data[9]. The CAPACITY is 42. Where does the push member function place the new entry in the array? o A. data[0] o B. data[1] o C. data[9] o D. data[10] 11. Consider the implementation of the stack using a partially-filled array. What goes wrong if we try to store the top of the stack at location [0] and the bottom of the stack at the last used position of the array? o A. Both peek and pop would require linear time. o B. Both push and pop would require linear time. o C. The stack could not be used to check balanced parentheses. o D. The stack could not be used to evaluate postfix expressions. 12. In the linked list implementation of the stack class, where does the push member function place the new entry on the linked list? o A. At the head o B. At the tail o C. After all other entries that are greater than the new entry. o D. After all other entries that are smaller than the new entry. 13. In the array version of the stack class (with a fixed-sized array), which operations require linear time for their worst-case behavior? o A. is_empty o B. peek o C. pop o D. push o E. None of these operations require linear time. 14. In the linked-list version of the stack class, which operations require linear time for their worst-case behavior? o A. is_empty o B. peek o C. pop
o o o o

D. push E. None of these operations require linear time. 15. What is the value of the postfix expression 6 3 2 4 + - *: o A. Something between -15 and -100 o B. Something between -5 and -15 o C. Something between 5 and -5 o D. Something between 5 and 15 o E. Something between 15 and 100 16. Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual stack algorithm to convert the expression from infix to postfix notation. What is the maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression? o A. 1 o B. 2 o C. 3 o D. 4 o E. 5
o o

36. Suppose that Table is an array of records used to implement a linked list, as shown below.

Each component of Table contains an information field and a pointer to the next element in a linked list. (The pointer values are indices of Table, and 0 signifies the end of a list.) What is the list off characters starting with Table[5]? A. da B. djaba C. daadjaba D. dajabadj E. dadjhlabce 37. Which of the following is true of stacks and queues? A. A stack is a last-in, first-out structure, and a queue is a first-in, first-out structure B. A stack is a first-in, first-out structure, and both structures are random access structures.

C. A stack is a last-in, first-out structure, and a queue is a random access structure. D. A queue is a last-in, first-out structure, and a stack is a first-in, first-out structure. E. A queue is a first-in, first-out structure, and a stack is a random access structure. Questions 38-39 are based on the following information. A newspaper route has recently been computerized. Information about each of the 100 customers is stored in individual records containing first name, last name, and payment due. In writing a computer program to process the customer records, the programmer is uncertain whether to add a procedure to sort the records. 38. If the records are not sorted, what is the maximum possible number of comparisons that must be made to obtain a particular customer's record using a sequential search? A. 100 B. 50 C. 7 D. 5 E. 3 39. If the records are first sorted, what will be the maximum number of comparisons needed with a binary search to find a particular customer's record? A. 100 B. 50 C. 7 D. 5 E. 3 40. What is the least order of execution time for the following code segment, if the segment terminates without error?
41. 42. 43. 44. 45. 46. 47. 48. 49. i := 1; Count := 0; while i < N do begin Current := A[i]; while (Current = A[i]) and (i < N) do i := i + 1; Count = Count + 1 end

A. O(log N) B. O(N) C. O(N) D. O(N log N) E. O(N2) 50. If the inorder traversal of the binary tree T is ADBGCFE

and each node of T has either 0 or 2 children, which of the following nodes is NOT a leaf of that tree? A. A B. B C. C D. D E. E 51. In a procedure, value parameters do not necessarily protect the contents of the caller's data structures from being affected by execution of the procedure under which of the following conditions? A. The procedure is recursive. B. The value parameters are integers. C. The value parameters are pointers. D. The value parameters are arrays. E. The procedure is used with a forward declaration. 52. The alpha-mem, a new data structure, supports two operations. The insert operation allows "words" to be stored in the alpha-mem. The remove operation causes the "word" in the alpha-mem which is first alphabetically to be printed and removed from the alpha-mem. Which of the following is true of an alpha-mem? A. If words are inserted in alphabetical order and all words are inserted before any are removed, then it works like a stack B. If words are inserted in alphabetical order, then it works like a stack whether or not all inserts precede any removes. C. If all words are inserted before any are removed, then it works like a queue. D. If words are inserted in reverse alphabetical order, then it works like a queue whether or not all inserts precede any removes. E. If words are inserted in alphabetical order, then it works like a queue whether or not all inserts precede any removes. 53. Consider the partially completed program below.
54. 55. 56. 57. 58. 59. 60. 61. Root := 0; Lim := n; while BBB do { Invariant: (Root)2 n (Lim + 1)2 } begin <code to increment Root or decrement Lim,> <leaving Invariant true > end

With which of the following should BBB be replaced in order for the loop above to compute an integer approximation of the square root of non-negative n>?

A. Lim <> Root B. Lim = Root C. Root * Root <> n D. Lim * Lim <> n E. Lim * Lim = Root * Root 62. Suppose that the only Pascal implementation available does not support pointers or dynamic storage allocation using new. A Pascal program is given that uses pointers and new for creating and manipulating instances of the following type.
63. 64. 65. 66. 67. 68. type NodePtr = ^Node; Node = record Info : integer; Next : NodePtr end;

To modify this program so that it can be run using the available resources, one should A. simulate pointers by using indices into a large array, and change the program accordingly B. simulate pointers by using trees and recursion C. simulate pointers by using a combination of value parameters and var parameters D. modify the Pascal implementation so that it does support pointers and new E. identify the queues and stacks in the program, and reimplement each by using an array 69. A property of binary trees is said to be an "inherited" property if each of the subtrees has the property whenever the binary tree itself does. One inherited property for binary tree is having A. an odd number of nodes B. an even number of nodes C. the same number of nodes in the left and right subtrees D. at least 10 nodes E. no node with exactly one child 70. Consider the following procedure.
71. 72. 73. 74. 75. 76. 77. 78. procedure Delete(p : Pointer); { Deletes the node pointed to by p from a doubly linked list } { (without a header node) whose pointer fields are called } { BackLink and NextLink. } begin p^.BackLink^.NextLink = p^.NextLink; p^.NextLink^.BackLink = p^.BackLink; end;

In which of the following cases will the procedure above fail to work properly? I. II. III. p points to the first node in the list. p points to the last node in the list. p = nil

A.

III only B. I and II only C. I and III only D. II and III only E. I, II, and III 37. Of the following, which has the most impact on the efficiency of searching for an item in a hash table? A. The number of nonkey fields B. The size of the table C. The density of the table D. Whether the size of the table is a prime number E. The difficulty of computing the inverse of the hash function 38. Of the following data structures, which would be best suited for an application where search time must be minimized and where data can be easily added? A. Singly linked list B. Sorted array C. Hash table D. Sequential file E. Circularly linked list with two header nodes

39. The program segment below uses a Monte Carlo technique to approximate the area of the shaded region shown. The function Random returns a real number in the range 0 Random < 1 each time it is executed. Assume that the numbers are randomly chosen.
40. 41. Points := 0; for i := 1 to n do

42. 43. 44. 45. 46. 47.

begin x := Random; y := Random; S end; Area := Points/n

Which of the following statements should be used in place of S to make the segment work as indicated?
A. if y >= 1 - x*x then Points := Points + 1 if y <= 1 - x*x then Points := Points + 1 if y < 1 - x*x then Points := Points + 1 else Points := Points - 1 if y = 1 - x*x then Points := Points + 1 if y > 1 - x*x then Points := Points + 1

B. D.

C. E. F. G. H.

I.
J.

K. M.

L.

Throughout this section, A is a class and B is Multiple Choice Multiple Choice a new class derived from A. 1. What C++ syntax is used to Section 14.1 Also, we have these variables: declare that a class B is derived Derived Classes A a; B b; from class A? B b1; o A. class A derives B { ... B b2; }; o B. class B from A { ... }; o C. class B : public A { ... }; o D. class B subclass of A { ... }; 2. If a class B is derived from A, then which of the following terms describes A? o A. ancestor class. o B. base class. o C. parent class. o D. superclass. o E. All of the above. 3. Using the variable declarations at the top of this section, which of the following assignment statements are legal? o A. a = b; o B. b = a; o C. b1 = b2; o D. Both (A) and (B) are legal, but not (C).

E. Both (A) and (C) are legal, but not (B). F. Both (B) and (C) are legal, but not (A). 4. Consider the assignment statement a=b; (with the variable declarations at the top of this section). Which answer is true? o A. The assignment statement is illegal. o B. The assignment statement activates the A assignment operator. o C. The assignment statement activates the B assignment operator. o D. The assignment statement activates both A and B assignment operators. 5. Consider the assignment statement b=a; (with the variable declarations at the top of this section). Which answer is true? o A. The assignment statement is illegal. o B. The assignment statement activates the A assignment operator. o C. The assignment statement activates the B assignment operator. o D. The assignment statement activates both A and B assignment operators. 6. What is the term used to describe the situation when a derived class provides a function already provided in the base class? o A. Inheriting. o B. Overriding. 7. Consider the declarations at the top of this section. Suppose there are two functions: f has an argument of type A and g has an argument of type B. Which statement is correct? o A. Both f(b) and g(b) are legal function calls. o B. f(b) is legal, but g(b) is not legal. o C. f(b) is not legal, but g(b) is legal. o D. Neither f(b) nor g(b) is a legal function function call. 8. Consider the declarations at the top of this section. Suppose there are two functions: f has an argument of type A and g has an argument of type B. Which statement is correct? o A. Both f(a) and g(a) are legal function calls. o B. f(a) is legal, but g(a) is not legal. o C. f(a) is not legal, but g(a) is legal. o D. Neither f(a) nor g(a) is a legal function function call. 9. Suppose you were going to quickly implement a Stack class. What class would be the best base class? o A. Bag. o B. List. o C. Queue. o D. Table. 10. Suppose you were going to quickly implement a Stack class as a derived class. Why would it be likely that the base class would be a private base class? o A. You would not want to have all of the base classes private member functions be private member functions of the stack. o B. You would not want to have all of the base classes private member functions be public member functions of the stack. o C. You would not want to have all of the base classes public member functions be private member functions of the stack.
o o

D. You would not want to have all of the base classes public member functions be public member functions of the stack.

Queues
Multiple Choice 1. One difference between a queue and a stack is: o A. Queues require dynamic memory, but stacks do not. o B. Stacks require dynamic memory, but queues do not. o C. Queues use two ends of the structure; stacks use only one. o D. Stacks use two ends of the structure, queues use only one. 2. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? o A. ABCD o B. ABDC o C. DCAB o D. DCBA 3. Which of the following expressions evaluates to true with approximate probability equal to P? (P is double and 0 <= P <= 1). o A. rand() < P o B. rand() > P o C. rand() < P * RAND_MAX o D. rand() > P * RAND_MAX 4. Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data[2] through data[11]. The CAPACITY is 42. Where does the push member function place the new entry in the array? o A. data[1] o B. data[2] o C. data[11] o D. data[12] 5. Consider the implementation of the queue using a circular array. What goes wrong if we try to keep all the items at the front of a partially-filled array (so that data[0] is always the front). o A. The constructor would require linear time. o B. The get_front function would require linear time. o C. The insert function would require linear time. o D. The is_empty function would require linear time. 6. In the linked list implementation of the queue class, where does the push member function place the new entry on the linked list? o A. At the head o B. At the tail o C. After all other entries that are greater than the new entry. o D. After all other entries that are smaller than the new entry.

7. In the circular array version of the queue class (with a fixed-sized array), which operations require linear time for their worst-case behavior? o A. front o B. push o C. empty o D. None of these operations require linear time. 8. In the linked-list version of the queue class, which operations require linear time for their worst-case behavior? o A. front o B. push o C. empty o D. None of these operations require linear time. 9. If data is a circular array of CAPACITY elements, and last is an index into that array, what is the formula for the index after last? o A. (last % 1) + CAPACITY o B. last % (1 + CAPACITY) o C. (last + 1) % CAPACITY o D. last + (1 % CAPACITY) 10. I have implemented the queue with a circular array, keeping track of first, last, and count (the number of items in the array). Suppose first is zero, and last is CAPACITY-1. What can you tell me about count? o A. count must be zero. o B. count must be CAPACITY. o C. count could be zero or CAPACITY, but no other values could occur. o D. None of the above. 11. I have implemented the queue with a linked list, keeping track of a front pointer and a rear pointer. Which of these pointers will change during an insertion into a NONEMPTY queue? o A. Neither changes o B. Only front_ptr changes. o C. Only rear_ptr changes. o D. Both change. 12. I have implemented the queue with a linked list, keeping track of a front pointer and a rear pointer. Which of these pointers will change during an insertion into an EMPTY queue? o A. Neither changes o B. Only front_ptr changes. o C. Only rear_ptr changes. o D. Both change. 13. Suppose top is called on a priority queue that has exactly two entries with equal priority. How is the return value of top selected? o A. The implementation gets to choose either one. o B. The one which was inserted first. o C. The one which was inserted most recently. o D. This can never happen (violates the precondition)

Linked lists
Multiple Choice 1. Suppose cursor refers to a node in a linked list (using the IntNode class with instance variables called data and link). What statement changes cursor so that it refers to the next node? A. cursor++; B. cursor = link; C. cursor += link; D. cursor = cursor.link; 2. Suppose cursor refers to a node in a linked list (using the Node class with instance variables called data and link). What boolean expression will be true when cursor refers to the tail node of the list? A. (cursor == null) B. (cursor.link == null) C. (cursor.data == null) D. (cursor.data == 0.0) E. None of the above. 3. Which boolean expression indicates whether the numbers in two nodes (p and q) are the same. Assume that neither p nor q is null. A. p == q B. p.data == q.data C. p.link == q.link D. None of the above. 4. Suppose that p is a reference variable that contains the null reference. What happens at runtime if the program tries to activate a method of p? A. IllegalArgumentException The IntNode definition:
public class IntNode { int data; IntNode link; ... }

B. IllegalStateException C. NullPointerException D. The results are unpredictable. 5. Suppose that a method has one node as a parameter and it returns two references to nodes. What's the best header for the method? A. IntNode foo(IntNode p) B. IntNode, IntNode foo(IntNode p) C. IntNode[ ] foo(IntNode p) D. void foo(IntNode p, IntNode answer1, IntNode answer2) 6. In the linked list version of the Bag class an instance variable manyNodes is used to keep track of how long the linked list is. Why not just make a call to the IntNode method listLength()? A. The listLength() method is O(n) and the alternative is O(1). B. The listLength() method is private. C. The listLength() method results in an infinite loop for circular lists. D. The listLength() method works only for lists of integers. 7. Suppose that the Bag is implemented with a linked list. Which of these operations are likely to have a constant worst-case time? A. add B. countOccurrences C. remove D. None of (A), (B), and (C) have a constant worst-case time E. TWO of (A), (B), and (C) have a constant worst-case time F. ALL of (A), (B), and (C) have a constant worst-case time 8. What is the expression for generating a pseudorandom number in the range 1...N?

A. (int) (Math.random() * N); B. (int) (Math.random() / N); C. (int) (Math.random() * (N + 1)); D. (int) (Math.random() / (N + 1)); E. (int) (Math.random() * N) + 1; 9. Which expression computes a pseudorandom integer between -10 and 10 using rand() from stdlib.h? A. (int) (Math.random( ) * 20) - 10 B. (int) (Math.random( ) * 21) - 10 C. (int) (Math.random( ) * 22) - 10 D. (int) (Math.random( ) * 20) - 11 E. (int) (Math.random( ) * 21) - 11 10. For the linked-list version of the Sequence ADT, which operations are constant time operations in the worst case? A. addAfter is constant time, but not addBefore B. addBefore is constant time, but not addAfter C. Both addAfter and addBefore are constant time D. Neither addAfter nor addBefore are constant time 11. Suppose that the Sequence is implemented with a linked list. Which of these operations are likely to have a constant worst-case time? A. addBefore B. countOccurrences C. remove D. None of (A), (B), and (C) have a constant worst-case time E. TWO of (A), (B), and (C) have a constant worst-case time

F. ALL of (A), (B), and (C) have a constant worst-case time 12. What is the output of these statements, using your Sequence ADT implemented as a linked list:
DoubleLinkedSeq x = new DoubleLinkedSeq( ); DoubleLinkedSeq y = new DoubleLinkedSeq( ); x.addBefore(41); // Inserts 41 into the list x x.addBefore(42); // Inserts 42, so that x is now 42, 41 with cursor at front y = (DoubleLinkedSeq) x.clone( ); x.addAfter(43); // Attaches 43 so that x is now 42, 43, 41 with cursor at 43 y.advance( ); System.out.println("y size is " + y.size( )); System.out.println(" and y current item is " + y.current( ));

A. y size is 2 and y current item is 41. B. y size is 2 and y current item is 43. C. y size is 3 and y current item is 41. D. y size is 3 and y current item is 43. E. None of the above. 13. Suppose that you forgot to write the extra work after super.clone in your implementation of the sequence clone method. What is the most likely output from the previous question? A. y size is 2 and y current item is 41. B. y size is 2 and y current item is 43. C. y size is 3 and y current item is 41. D. y size is 3 and y current item is 43. E. None of the above. 14. What kind of list is best to answer questions such as "What is the item at position n?" A. Lists implemented with an array. B. Doubly-linked lists.

C. Singly-linked lists. D. Doubly-linked or singly-linked lists are equally best

Others
Question Details: int *p; int num; Assume that memory location 1200 is allocated for p and memory location 1800 is allocated for num. 1. The statement p=&num; stores the address of num that is, ____ into p. a. 1800 b. 24 c. 1200 d. num 2. You can initialize a pointer variable by setting it to ____ a. 0 b. NULL c. 1 d. a or b Consider the following lines of code: struct studentType { char name[27]; double gpa; int sID;

char grade; }; int *p; double *q; char *chPtr; studentType *stdPtr; 3. The statement stdPtr++ increments the value of stdPtr by ____ bytes. a. 10 b. 20 c. 40 d. 80 4. An array created during the execution of a program is called a ____ array. a. static b. final c. just in time d. dynamic vector<int> v (1, 2); int value = v.pop_back (); cout << value << endl; 5. What is the output of the code fragment above? a. 0 b. 1 c. 2

d. There is no output int exampleRecursion (int n) { if (n==0) return 0; else return exampleRecursion(n-1) + n*n*n; } 6. What is the output of exampleRecursion(0)? a. 0 b. 3 c. 9 d. 27 7. Which of the following statements is NOT true about infinite recursion? a. In theory, infinite recursion executes forever. b. In reality, infinite recursion will lead to a computer running out of memory and abnormal termination of the program. c. Functions that are indirectly recursive always lead to infinite recursion. d. If recursive calls never lead to a base case infinite recursion occurs. int func3(int m, int n) { if (m < n) return 0;

else return 1 + func3(m-n, n); } 8. What is the value of func3(6, 3), based on the code above? a. 2 b. 3 c. 6 d. 9 9. Which of the following operations is needed for a linear implementation of a stack but not a linked implementation? a. isEmptyStack b. isFullStack c. initializeStack d. destroyStack 10. What is the equivalent infix expression for the postfix expression n m + p * ? a. p * n + m b. m + (p * n) c. (n + m) * p d. n + m * p 11. The function addQueue does which of the following? a. adds all the contents from one queue to another b. appends one queue to the back of another c. adds a new element to the front of the queue

d. adds a new element to the rear of the queue [0] [1] [2] [3] [4] [5] [6] [7] list 35 12 27 18 45 16 38 12. What is the minimum number of comparisons that have to be made to find 18 using sequential search on the list above? a. 1 b. 2 c. 3 d. 4 13. If we want to design a search algorithm that is of an order less than log2n, then it ____ be comparison based. a. must b. could c. cannot d. should 14. Selection sort can be applied to which of the following? a. arrays b. linked lists c. stacks d. a and b 15. Quick sort uses ____ for implementation. a. recursion b. traversal

c. heaps d. queues 16. The listing of the nodes produced by the preorder traversal of a binary tree is called the ____ sequence. a. inorder b. preorder c. postorder d. None of the above Multiple Choice on Sorting 1. In a selectionsort of n elements, how many times is the swap function called in the complete execution of the algorithm? o A. 1 o B. n - 1 o C. n log n o D. n 2. Selectionsort and quicksort both fall into the same category of sorting algorithms. What is this category? o A. O(n log n) sorts o B. Divide-and-conquer sorts o C. Interchange sorts o D. Average time is quadratic. 3. Suppose that a selectionsort of 100 items has completed 42 iterations of the main loop. How many items are now guaranteed to be in their final spot (never to be moved again)? o A. 21 o B. 41 o C. 42 o D. 43 4. Suppose we are sorting an array of ten integers using a some quadratic sorting algorithm. After four iterations of the algorithm's main loop, the array elements are ordered as shown here:
5. 1 2 3 4 5 0 6 7 8 9

Multiple Choice Section 12.1 Quadratic Sorting Algorithms

Which statement is correct? (Note: Our selectionsort picks largest items first.)
o o

A. The algorithm might be either selectionsort or insertionsort. B. The algorithm might be selectionsort, but could not be insertionsort.

C. The algorithm might be insertionsort, but could not be selectionsort. D. The algorithm is neither selectionsort nor insertionsort. 6. Suppose we are sorting an array of eight integers using a some quadratic sorting algorithm. After four iterations of the algorithm's main loop, the array elements are ordered as shown here:
o o 7. 2 4 5 7 8 1 3 6

Which statement is correct? (Note: Our selectionsort picks largest items first.) A. The algorithm might be either selectionsort or insertionsort. B. The algorithm might be selectionsort, but it is not insertionsort. C. The algorithm is not selectionsort, but it might be insertionsort. D. The algorithm is neither selectionsort nor insertionsort. 8. When is insertionsort a good choice for sorting an array? o A. Each component of the array requires a large amount of memory. o B. Each component of the array requires a small amount of memory. o C. The array has only a few items out of place. Multiple Choice o D. The processor speed is fast. Section 12.2 9. What is the worst-case time for mergesort to sort an array of Recursive Sorting n elements? Algorithms o A. O(log n) o B. O(n) o C. O(n log n) o D. O(n) 10. What is the worst-case time for quicksort to sort an array of n elements? o A. O(log n) o B. O(n) o C. O(n log n) o D. O(n) 11. Mergesort makes two recursive calls. Which statement is true after these recursive calls finish, but before the merge step? o A. The array elements form a heap. o B. Elements in each half of the array are sorted amongst themselves. o C. Elements in the first half of the array are less than or equal to elements in the second half of the array. o D. None of the above. 12. Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the array looking like this:
o o o o 13. 2 5 1 7 9 12 11 10

Which statement is correct?


o o o o

A. The pivot could be either the 7 or the 9. B. The pivot could be the 7, but it is not the 9. C. The pivot is not the 7, but it could be the 9. D. Neither the 7 nor the 9 is the pivot.

14. What is the worst-case time for heapsort to sort an array of n elements? o A. O(log n) o B. O(n) o C. O(n log n) o D. O(n) Multiple Choice 15. Suppose we are sorting an array of eight integers using Section 12.3 heapsort, and we have just finished one of the An O(n log n) Algorithm reheapifications downward. The array now looks like Using a Heap this:
16. 6 4 5 1 2 7 8

How many reheapifications downward have been performed so far?


o o o o

A. 1 B. 2 C. 3 or 4 D. 5 or 6 Multiple Choice Section 15.1 Graph Definitions

Multiple Choice on Graphs 1. Which of the following statements is true? o A. A graph can drawn on paper in only one way. o B. Graph vertices may be linked in any manner. o C. A graph must have at least one vertex. o D. A graph must have at least one edge. 2. Suppose you have a game with 5 coins in a row and each coin can be heads or tails. What number of vertices might you expect to find in the state graph? o A. 7 o B. 10 o C. 25 o D. 32 3. Why is the state graph for tic-tac-toe a directed graph rather than an undirected graph? o A. Once a move is made, it cannot be unmade. o B. There is an odd number of vertices. o C. There is an odd number of edges. o D. There is more than one player in the game. 4. A simple graph has no loops. What other property must a simple graph have? o A. It must be directed. o B. It must be undirected. o C. It must have at least one vertex. o D. It must have no multiple edges. 5. Suppose you have a directed graph representing all the flights that an airline flies. What algorithm might be used to find the best sequence of connections from one city to another? o A. Breadth first search.

B. Depth first search. C. A cycle-finding algorithm. D. A shortest-path algorithm. Multiple Choice 6. If G is an directed graph with 20 vertices, how many Section 15.2 boolean values will be needed to represent G using an Graph Implementations adjacency matrix? o A. 20 o B. 40 o C. 200 o D. 400 7. How many linked lists are used to represent a graph with n nodes and m edges, when using an edge list representation, o A. m o B. n o C. m + n o D. m*n 8. How are loops represented in an edge-list representation of a graph? o A. A vertex will be on its own edge-list. o B. The edge-list will be a circular linked list. o C. The edge-list will be empty for that particular vertex. o D. The edge-list will be full for that particular vertex.
o o o

Which graph representation allows the most efficient determination of the existence of a particular edge in a graph? A. An adjacency matrix. B. Edge lists. 9. What is the expected number of operations needed to loop through all the edges terminating at a particular vertex given an adjacency matrix representation of the graph? (Assume n vertices are in the graph and m edges terminate at the desired node.) o A. O(m) o B. O(n) o C. O(m) o D. O(n) Multiple Choice 10. What graph traversal algorithm uses a queue to keep track Section 15.3-15.4 of vertices which need to be processed? Graph Traversals o A. Breadth-first search. and Path Algorithms o B. Depth-first search.
o o

Others
1. In C++, which of the following best corresponds to the notion of an Abstract Data Type (ADT)? a. private members of a class b. all the methods of a class

c. an abstract class with only pure virtual methods d. a class with fully implemented methods 2. All programs are algorithms. a. True b. False 3. Which values for c and n0 will prove that 3n2 is O( n3 ) ? a. c = 1, n0 = 1 b. c = 2, n0 = 1 c. c = 2, n0 = 2 d. None of the above 4. All complete binary trees with an odd number of nodes are also full binary trees. a. True b. False 2 5. A tree with at least two nodes has at least as many leaves as it has internal nodes. a. True b. False 6. Suppose a binary tree has only three nodes A, B, and C, and you are given that the post-order traversal for the tree is B-A-C. The pre-order traversal for the tree is: a. C-A-B b. A-B-C c. C-B-A d. None of the above e. A definite pre-order traversal cannot be determined from the information given 7. Suppose you are given that a general tree has 4 nodes and has height 2. How many leaves does this tree contain? a. 1 b. 2 c. 3 d. 4 e. The number of leaves cannot be determined from the information given. 8. An internal node in a Huffman tree correspond to: a. a letter b. a sum of frequencies of letters c. the number of bits needed for a letter d. none of the above 9. Which of the following sorting algorithms perform in O( n ) time in the best case? a. Insertion Sort b. Bubble Sort c. Selection Sort

d. All of the above e. None of the above 3 10. Which of the following sorting algorithms perform in O( n2 ) time in the worst case? a. Insertion Sort b. Bubble Sort c. Selection Sort d. All of the above e. None of the above II. Running Time Complexity and Order Arithmetic (15 points: 10,5) 1. For the following C++ code fragment, find T(n), the exact number of multiplications performed (operations to count are shown in bold font) in terms of n (there should be no summations in your final answer). Assume that n is a power of 2. product = 1; for( s = 1; s < n; s *= 2 ) for( i = 1; i <= s; i++) product *= i; T(n) = _________________________ 2. Encircle ALL statements that are correct: T(n) is ( n2 ) T(n) is O( n ) T(n) is ( log n ) III. Comparing List Implementations. (30 points: 5,5,15,10

You might also like