CSCI41 MIDTERM 2
What is following code segment do? Node* p; p = front; while(p->next != NULL) // while p != NULL) { cout<<data; p=p->next; }
Traversing a Linked List and print data
Consider the following binary search tree: If we remove the root node, which of the node from the left subtree will be the new root?
16
Which of the following sorting algorithm does generally do not use recursion?
Bubble sort
What is true about Overriding member function
Member function in the superclass can be altered by override function in the subclass AND Base class and derived class can have the same name and parameters due to overriding AND A virtual function is a member function that may be overridden in a derived class
What is the space complexity of a heap?
O(n)
Expected output of the following is int y=1000; int *yPtr; yPtr = &y; cout<<" &y = "<< &y <<" \n "; cout <<" y = " << y << endl; cout << " *yPtr = "<<*yPtr<<" \n "; cout <<"yPtr = "<< yPtr <<" \n ";
&y = 0x7fff9942bde4 y = 1000 *yPtr = 1000 yPtr =0x7fff9942bde4
Given the following AVL Tree: Find balance factors of node key 2, 4, 7, 5 respectively
-1, 0, 1, 1
If pre order traversal of a binary tree output 15, 10, 12, 11, 20,17, 19 Find the order of postorder traversal of the tree
11, 12, 10, 16, 19, 18, 20, 15
Find the number of rotations required to insert of elements 9, 6, 5, 8, 7, 10 into an empty AVL Tree
3
After inserting keys 5, 6, 8 , 10, 20, 30, 40 to a BST in the same order. Find the height of the BST
6
What is True about "scope"
Block scopes can define using { } AND Scope is Portion of the program where an identifier can be used AND File scope will remain till end of the program
What is the following code segment of AVL tree do? int avl(binarysearchtree root): if(not root) return 0 left_tree_height = avl(left_of_root) if(left_tree_height== -1) return left_tree_height right_tree_height= avl(right_of_root) if(right_tree_height==-1) return right_tree_height
Check the validity of AVL tree
Relation between children and the parent of heap (if start index is 1)
Children of parent k is located at 2k and 2k+1
Which of the following method is used for sorting in merge sort?
Merging
Which of the following is a double rotation?
Not any of these
What is the auxiliary space complexity in merge sort?
O(n)
When you store the following 10 values in array of length 10, using open hashing technique (chaining). What is the possible maximum time complexity of search. (n = 10) 0 10 20 30 40 50 60 70 80 90
O(n)
Quick sort uses
Partitioning
Find true statements of Queue (There may be more than one answer)
Queue is a LILO (Last in Last Out) Data Structure. AND Queue is a FIFO (First in First Out) Data Structure.
Which of the following is not a stable sorting algorithm in its typical implementation.
Quick Sort
Binary Search works when the array is
Sorted in ascending order AND Sorted in descending order
Find the correct statement/s about queues and stacks
Stack remove the item most recently added
What is data structure would fit the idea of Last In First Out (LIFO)
Stacks
T(n) =T(n−1) +n find T(n) after 4th step
T(n -4)+(n−3) + (n−2) + (n−1) +n
Which of the following is a true about Binary Trees
The height of a binary tree is the maximum number of edges in any root to leaf path AND Height of complete tree with N nodes is ⎣log N⎦.
What is following code segment do? Node* p; p = front;while(p->next != NULL) // while ( p != NULL) { cout<<data;p=p->next; }
Traversing a Linked List and print data
What is the location of a parent node for any arbitary node i? (in heap)
floor(i/2) position
What is the location of a parent node for any arbitrary node i? (in a heap)
floor(i/2) position
In a max-heap, element with the greatest key is always in which node?
root node
Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 55. Which of the following sequences CANNOT be the sequence of nodes examined (in tree traversal)?
{9, 85, 47, 68, 43, 57, 55}
Given Class name myClass True about default constructor declaration of class myClass { ... } is: (May have more than one answer)
//The following calls the default constructor once: myClass object; AND //The following calls the default constructor twice: myClass object1; myClass object2; AND //The following calls the default constructor 3 times: vector<myClass> obj(3);
Given the following code, indicate j's value at the end of iteration x = 2. for (int x = 0; x < 5; ++x) { int j = 0; j = j *x; }
0
Find the intermediate steps of the following data set when it is being sorted with the Quick sort? (Using start pivot as index 2)5 10 1 50 3 0
0,1,5,50,3,10 -- 0,1,5,50,3,10 -- 0,1,5,10,3,50 -- 0,1,5,3,10,50
Given that T(n) =T(1) +∑i=3ni What is T(n) and it's complexity ?
1 +(n+3)(n−2) /2 and Θ(n2)
What is true about the following code segment class A { public: void Print(); protected: char * name; private: char *address; }; class B : public A {}
A is the superclass of B AND Variable 'name' only accessible by A and B not anyone else
What statements are true about Priority Queue
A priority queue is a collection of elements such that each element has been assigned a priority. AND An element of higher priority is processed before any element of lower priority.
Which of the following methods is the most effective for picking the pivot element?
median-of-three partitioning
Choose the proper code for merge sort.
void merge_sort(int arr[], int left, int right) { if (left < right) { int mid = left+(right-left)/2; merge_sort(arr, left, mid); merge_sort(arr, mid+1, right); merge(arr, left, mid, right); //function to merge sorted arrays } }
Find complexity of T(n) = 4T(n/4) +n
Θ(nlogn) where log 4
Best Case Time Complexity of Binary Search is
Ω(1)
Given keys 20, 30, 33, 35, 39, 40, 50, 60, 100 What order of key insertion would construct a Full Binary Tree
40, 30. 60. 20, 35, 50, 100, 33, 38
How many comparisons are required to order an array of 5 elements using the selection sort?
10
Which array represents the max heap -- : indicate empty array cell
10 8 6 4 5 1 2 -- --
What are the correct intermediate steps of the following data set when it is being sorted with the Insertion sort? 15,20,10,18
10,15,20,18 -- 10,15,18,20 -- 10,15,18,20
What are the correct intermediate steps of the following data set when it is being sorted with the bubble sort? 14,19,10,17
14,10,19,17 -- 14,10,17,19 -- 10,14,17,19
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?
15, 10, 23, 25, 20, 35, 42, 39, 30
What are the correct intermediate steps of the following data set when it is being sorted with the Insertion sort? 15,20,10,18
15,20,10,18 -- 10,15,20,18 -- 10,15,18,20 -- 10,15,18,20
Let h'(x) = [h(x) + f(i3)]% n where i =0,1,2,3,4...f(i) = i insert keys 7,2,17,65,40 to a hash table of size 5 key map to the array index given by
17 65 7 2 40
After inserting keys 5, 6, 8 , 10, 20, 30, 40 to a BST in the same order. Find the height of the node with key 20
2
The maximum comparisons needed in Binary Search on sorted array of size 64 is
6
What is following code segment would do node<T>* p1, *p2; p1= front1; p2= front2; while(p1->next != NULL) { p1 = p1->next; } p1->next = p2;
Combining two linked lists
How many times the following Inner loop body runs int row; int col; for(row = 0;row <= 3;row ++){ for(col = 0;col < 5;col++){ // Inner loop body } }
20
The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is:
2^(h+1) -1
QUIZ 5 STARTS ****** After inserting keys 5, 6, 8 , 10, 20 to a BST in the same order. Find the depth of the node with key 10
3
The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16 What is the height of the binary search tree ?
3
Consider the following lists of partially sorted numbers. The double bars represent the sort marker Insertion Sort. How many comparisons and swaps are needed to sort the next number. [1 3 4 8 9 || 5 2]
3 comparisons, 2 swaps
Consider a hash table with 9 slots. The hash function is ℎ(k) = k % 9. The collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum, minimum, and average chain lengths in the hash table, respectively, are
3, 0, and 1
Suppose the numbers 13, 11, 7, 14, 9, 12, 6, 15, 10, 8 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree?
6 7 8 9 10 11 12 13 14 15
Let h'(x) = [h(x) + f(I^2)]% n where i =0,1,2,3,4... f(i) = i insert keys 7,2,17,65,40 to a hash table of size 5 key map to the array index given by
65 17 7 2 40
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap in array is.
32 30 25 15 12 20 16
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap in the array is.
32 30 25 15 12 20 16
Assume we are sorting in increasing order. 32 30 29 33 31 35 34 Let's use Quicksort with pivot middle floor value with recursive call find one of the correct intermediate step of the above data set
32 30 29 33 31 -- 34 35 29 -- 30 31 33 32 -- 34 -- 35 29 -- 30 31 -- 33 32 -- 34 -- 35 29 -- 30 -- 31 -- 33 --32 -- 34 -- 35
Given keys 20, 30, 35, 39, 40, 60, 100 What order of key insertion would construct a Complete Binary Tree
39, 30, 60, 20, 35, 40, 100
Suppose the numbers 4, 2, 6, 1, 3, 5, 15, 7, 16 are inserted in that order into an initially empty AVL tree. Find the resulting AVL after inserting number 12 followed by proper rotations. Resulting values read from left to right in each level and null node indicated by --
4 2 7 1 3 6 15 -- -- -- -- 5 12 16
After inserting keys 5, 6, 8 , 10, 20, 30, 40, 100 to a BST in the same order. Find the size of the tree
8
What is following code segment would do node<T>* p1, *p2; p1= front1; p2= front2; while(p1->next != NULL) { p1 = p1->next; } p1->next = p2;
Combining two linked lists
What is true about Derived class
Derived class cannot access private data of its superclass AND Derived class can access protected data of its superclass AND Derived class is a subclass
What is true about using C++ templates (There may be more than one answer)
Developer can write one generic code that fits most of the data types AND Using templates are not always practical when it uses different data types such as string types vs integers
Assume you have letters APPLE and do the following step1) insert all characters to a stack respectively step 2) Remove all characters from stack and insert to a queue step 3) Remove each letter from Queue and print it out your output will be?
ELPPA
Find valid (true) statements (There can be more than one correct answers)
For any 2functions f(n) and g(n), we have f(n)=Θ(g(n)) if and only if of(n)=O(g(n)) and f(n)=Ω(g(n)) AND To give an lower bound on Ω= 'Big Omega' a function we use AND worst case running time of an algorithm given by O = 'Big O'
What is true about following pseudo code segment (may have more than one answer) #include <iostream> #include <string> using namespace std; class Restaurant { // Info about a restaurant public: void SetName(string name) { // Sets the restuarant's name name = name; } void SetRating(int userRating) { // Sets the rating (1-5, with 5 best) rating = userRating; } void Print(); // Prints name and rating on one line private: string name; int rating; }; // Prints name and rating on one line void Restaurant::Print() { cout << name << " -- " << rating << endl; }
Function SetName() was defined inline AND member function Print() not defined inline
Which of the following traversal outputs the data in sorted order in a BST?
Inorder Traversal
Find True statements regarding C++ class structures
Mutator and Accessor required only when private or protected data member occurs AND Mutator and Accessor also define as setter and getter respectively
Find TRUE statements of Perfect Binary Tree
Number of internal nodes in the tree with height 5 is 31 AND Number of Leaf nodes at height 5 is 32 AND Number of nodes at depth of 5 is 32
What is the time taken to perform a delete min operation of a min Heap?
O(log N)
What is the worst case time complexity for search, insert and delete operations in a general BST?
O(n) for all
What is the time complexity of Build Heap operation. Build Heap is used to build a max(or min) binary heap from a given array
O(nLogn)
MIDTERM 2 BEGINS:******* What is the worst case complexity of selection sort?
O(n^2)
What is the time complexity of Build Heap operation. Build Heap is used to build a max(or min) binary heap from a given array
O(nlogn)
Which of the following methods Quick sort uses
Partitioning
The function travel() receives root of a Binary Search Tree (BST) and a positive integer k as arguments. What is the code segment prints // A BST node struct node { int data; struct node *left, *right; }; int count = 0; void travel(struct node *root, int k) { if (root != NULL && count <= k) { travel(root->right, k); count++; if (count == k) printf("%d ", root->data); travel(root->left, k); } }
Prints the kth largest element in BST
What will the following code segment would do ? Node<T>* p; while (front!= NULL) { p = front; front = front->next; delete p; }
Remove nodes from the front repeatedly
What will the following code segment would do ? Node<T>* p; while (front!= NULL) { p = front; front = front->next; delete p; }
Remove nodes from the front repeatedly
QUIZ4 STARTS******** Which of the following searching technique generally does not get affected on increasing the size of search list.
Search by Hashing
What statements are true about sequential search(unordered list) and binary search(ordered array)
Sequential search is slow in large data sets and binary search is slow in inserting data AND Binary search cannot perform on unordered list
Find the recurrence relation of the following function void func(int n) { if(n>0 ){ cout<< n2<<endl; func(n-1); } }
T(n) = T(n-1) +2
Given that T(n) =T(n/m) + 1 find the qth step and q will be
T(n/m^q) +q where q = log^n m
Which of the following is a true about Binary Trees
The maximum number of binary trees that can be formed with three unlabeled nodes is 5 AND Constructing Binary Tree with sorted keys will give you a Tree with shape of Linked List
In a selection sort structure, there is/are?
Two for loops, one nested in the other
Which of the following is Not an application of priority queue?
Undo operation in text editors
In Merge Sort, what (Data Structure) do you use to combine subarrays into a sorted whole ?
Uses an Auxiliary array
In heap sort, after repeatedly deleting the last minimum element and construct, the array will contain elements in?
decreasing sorting order
Merge sort uses which of the following technique to implement sorting
divide and conquer
What are the minimum basic functions required to implement stack class after creating an empty Stack (Answer with standards)
push(), pop(), isEmpty()
In a max-heap, element with the largest key is always in the ....?
root node
Proper swap of two values in an array locate in index x,y
tmp = list[y]; list[y] = list[x]; list[x] = tmp;