Final Exam Practice Problems — CS 112, Boston University

.pdf

School

Boston University *

*We aren’t endorsed by this school

Course

112

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

14

Uploaded by andrewleenyk

12/17/23, 8:15 PM Final Exam Practice Problems — CS 112, Boston University https://www.cs.bu.edu/courses/cs112/final_practice.html 1/14 Final Exam Practice Problems As we get closer to the exam, solutions will be posted under Other Content on Blackboard. These problems are not comprehensive, so make sure to review all of the relevant materials. 1. Which of the following data structures would be most appropriate for a program that simulates the operation of an airport in order to determine the maximum delays encountered by arriving and departing planes? a. queue b. binary tree c. heap d. stack e. hash table 2. A stack s of integers initially contains the following data from top to bottom: The following code is then executed: After this code has been executed, what are the contents of the stack? a. {8, 10} b. {10, 8} c. {15, 3} d. {10, 8, 6, 3} e. none of the above 3. Suppose that items A, B, C, D and E are pushed, in that order, onto an initially empty stack S. S is then popped four times; as each item is popped off, it is inserted into an initially empty queue. {6, 2, 7, 3} int x = s.pop(); int y = s.pop(); int z = s.pop(); s.push(x + y); int w = s.pop(); s.push(w + z);
12/17/23, 8:15 PM Final Exam Practice Problems — CS 112, Boston University https://www.cs.bu.edu/courses/cs112/final_practice.html 2/14 If two items are then removed from the queue, what is the next item that will be removed from the queue? a. item A b. item B c. item C d. item D e. item E 4. If the keys in the tree below are printed using a postorder traversal, what will the result be? a. G E F A B C D b. G E A B F C D c. A E B G C F D d. A E B C F D G e. A B E C D F G Questions 5 and 6 involve binary trees whose nodes have the following structure: 5. Consider the following method: G / \ E F / \ / \ A B C D public class Node { public char val; public Node left; public Node right; } public static int mystery(Node root) { if (root != null) { if (root.left == null && root.right == null) { return 0 ; } int sum = mystery(root.left) + mystery(root.right); if (sum == 0 ) { Node temp = root.left; root.left = root.right;
12/17/23, 8:15 PM Final Exam Practice Problems — CS 112, Boston University https://www.cs.bu.edu/courses/cs112/final_practice.html 3/14 What does this method do to the tree whose root is referred to by the parameter root ? a. swaps the left and right subtrees of the root node b. swaps any two leaf nodes that have the same parent c. swaps the leftmost and rightmost leaves d. replaces the tree with its mirror image e. none of the above 6. Consider the following methods: If root initially points to the root of the following tree: what tree results from the execution of the following statements? root.right = temp; } } return - 1 ; } public static void preorder(Node root, Stack<Character> s) { if (root != null) { s.push(root.val); preorder(root.left, s); preorder(root.right, s); } } public static void postorder(Node root, Stack<Character> s) { if (root != null) { postorder(root.left, s); postorder(root.right, s); root.val = s.pop(); } } A / \ B C / \ / \ D E F G
12/17/23, 8:15 PM Final Exam Practice Problems — CS 112, Boston University https://www.cs.bu.edu/courses/cs112/final_practice.html 4/14 a. b. c. d. e. 7. If a binary search tree is not allowed to have duplicates, there is more than one way to delete a node in the tree when that node has two children. One way involves choosing a replacement node from the left subtree. If this is done, which node are we looking for? a. the largest node in the subtree b. the smallest node in the subtree c. the root of the left subtree Stack<Character> s = new LLStack<Character>(); preorder(root, s); postorder(root, s); A / \ B C / \ / \ D E F G A / \ C B / \ / \ G F E D G / \ D F / \ / \ A B E C A / \ C B / \ / \ F G D E G / \ F D / \ / \ E C B A
12/17/23, 8:15 PM Final Exam Practice Problems — CS 112, Boston University https://www.cs.bu.edu/courses/cs112/final_practice.html 5/14 d. the next-to-smallest node in the subtree e. it doesn’t matter – any node in the left subtree will do 8. The binary search tree shown below was constructed by inserting a sequence of items into an empty tree. Which of the following input sequences will not produce this binary search tree? a. 5, 3, 4, 9, 12, 7, 8, 6, 20 b. 5, 9, 3, 7, 6, 8, 4, 12, 20 c. 5, 9, 7, 8, 6, 12, 20, 3, 4 d. 5, 9, 7, 3, 8, 12, 6, 4, 20 e. 5, 9, 3, 6, 7, 8, 4, 12, 20 9. If the keys in the tree below are printed using a preorder traversal, what will the result be? a. 9 4 17 16 12 11 6 b. 9 17 6 4 16 22 12 c. 6 9 17 4 16 22 12 d. 6 17 22 9 4 16 12 e. 6 17 9 4 22 16 12 6 / \ 17 22 / \ / \ 9 4 16 12
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help