Serial : 1PT_CS_A+C_Programming & Data Structure_230918 Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: E-mail: info@madeeasy.in Ph: 011-45124612 CLASS TEST 2018-19 COMPUTER SCIENCE & IT Subject : Programming & Data Structure Date of test : 23/09/2018 Answer Key 1. (b) 7. (c) 13. (b) 19. (a) 25. (b) 2. (a) 8. (a) 14. (c) 20. (a) 26. (b) 3. (d) 9. (c) 15. (a) 21. (b) 27. (d) 4. (c) 10. (b) 16. (b) 22. (a) 28. (c) 5. (c) 11. (b) 17. (c) 23. (a) 29. (c) 6. (c) 12. (c) 18. (a) 24. (c) 30. (c)
8 Computer Science & IT Detailed Explanations 1. (b) In this ordering of {2, 6} and ordering of {1, 3, 5, 7} do not matter. We can see that resulting binary tree is perfectly balance that is a AVL tree. 2. (a) In this code while (current) is used to run the loop until null is encountered i.e. linked list is finished. 1 st for loop to reach the M th position node in linked list. If M th position present then run further otherwise not. 2 nd for loop delete N nodes from M th position from linked list and connect further nodes to M th position node to (N+1) th position node. So code will return linked list after deleting N nodes from M th position in the linked list. 3. (d) We have pre-oder traversal and the tree is binary search tree. So in-oder traversal of binary search tree is ascending oder. Using the in-oder and pre-oder two we can construct binary search tree with Θ(n log n) [we can apply binary search]. 4. (c) str 0 1 2 3 4 5 6 7 8 9 10 11 m a d e e a s y 2 0 1 6 \0 Inside run( ). Pointer str2 is initialized as str1 and str1 is moved till \0 is reached. So str1 will increment by 12 then 12 0 = 12 will return. 5. (c) S1: Inserting n elements in empty binary heap take O(n) time because build heap method take O(n). S2: BFS algorithm take O(m + n) time to find number of connected components in undirected graphs of n vertices and m edges. 6. (c) Static allocation is used for global variable because they are created at compile time. Stack allocation is used for local variables. Heap allocation is used for dynamic data structure. 7. (c) Binary search on array take O(log (n)) time because random access is possible on array but binary search on linked list take O(n) time because random access is not possible. Merge sort on array has more space complexity because it is outplace (i.e. take extra space) but merge sort on linked list take less space because it is inplace (i.e. not take extra space). 8. (a) Insertion of record in double linked list A maximum 4 pointer when record insert at between two node. a b c e 2 1 3 4 6 5 7 A 1 d = prev = e prev; 2 d next = c = c next; 3 d prev next = d next prev; 4 d next prev = d prev next; 1 2 d 3 4 Deletion of record in double linked list A maximum 2 pointer when record deleted from middle of linked list. a b c d e A
CT-2018 CS Programming & Data Structure 9 1. C prev next = C next prev; 2. C next prev = C prev next; 9. (c) Postfix expression evaluation take O(n) time because only one pass required for evaluation push element and pop 2 elements from stack, operate it and again push it onto stack whenever operator occurs. Prefix and infix evaluation take O(n 2 ) because two pass are require to evaluate. 10. (b) S1: AVL tree is a balance search the that has time complexity of searching O(log n) but in binary search tree, we can have a completely left / right skewed tree, in which search is O(n). S2: Insertion in AVL take O(log n) time because max height is log n, but for binary search tree take O(n) time because maximum height is n. 11. (b) Maximum number of node in AVL tree of height h is 2 h+1 1 i.e. 2 5+1 1 = 2 6 1 = 64 1 = 63 Minimum number of node in AVL tree Height 0 = 1 Node Height 1 = 2 Height 2 = MNH (H 1) + MNH (H 2) + 1 = 1 + 2 + 1 = 4 Height 3 = MNH (H 1) + MNH (H 2) + 1 = MNH (2) + MNH (1) + 1 = 4 + 2 + 1 = 7 Height 4 = MNH (H 1) + MNH (H 2) + 1 = MNH (3) + MNH (2) + 1 = 7 + 4 + 1 = 12 Height 5 = MNH (H 1) + MNH (H 2) + 1 = MNH (4) + MNH (3) + 1 = 12 + 7 + 1 = 20 Difference = 63 20 = 43 12. (c) Inserting an element in AVL tree with n nodes take O(log n) time. So inserting n more nodes it takes n O(log n) = O(n log n) time. So, O(n a log b n) = O(n log n) a = 1, b = 1 Value of a + 50b = 1 + (50 1) = 51 13. (b) 1 rec(5) 2 5 rec(3) pf(5) rec(2) 3 4 6 7 rec(1) pf(3) rec(0) rec(0) pf(2) rec( 1) return return return return 14. (c) Address of an element A[10][15] = Base address + [(j 1) r + (i 1)] 1 r = number of element in column = 1000 + [(15 1) 20 + (10 1)] = 1000 + [14 20 + 9] = 1000 + 289 = 1289 15. (a) Here a is free variable, since it is not declare value assignment to global variable.
10 Computer Science & IT 16. (b) b 1213 Global Function call N b = 12 + 1 = 13 Function call M(b) a = b (assigning address of b) a = a a = 13 13 = 169 a = 169 and b = 13 q m a d e e a s y 2 0 1 6 \0 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 Strcpy (p, best ); copy the string at location 1004. So printf function will print string upto null character i.e. madebest. q m a d e b e s t \0 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 p 17. (c) It merges the two sorted lists. In every recursion, z gets a node which is smallest node from x and y Finally z gets entire sorted list of given two sorted lists of x and y. So option (c) is correct. 18. (a) Building max heap 92 50 62 After adding 97 45 41 92 92 97 50 62 50 97 50 92 45 41 97 45 41 62 Deleting inorder predecessor (i.e. 92). Final max heap will be 45 41 62 97 50 63 45 41
CT-2018 CS Programming & Data Structure 11 19. (a) If S 2 stack is empty and S 1 stack is not empty then we have to pop the element from stack S 1 and push that element into stack S 2 and return the stack S 2 which contain newly inserted element. 20. (a) 1st we have to change the head pointer which contain front node address initially to initial second node (new front node) have to free the initial first node to completely delete the node. 21. (b) In program condition If(root left child == NULL && root right child == NULL) will true when node is leaf node and return 0. Lvalue and Rvalue find number of non-leaf node on left side of root tree and number of non-leaf node on right side of root respectively, and atlast 1 is added to include the root node. So it will return total number of internal node node. 22. (a) float ( p[n]) (int a); Return type float, ( p[n]) p is n elements array of pointers to function where each function having argument (int a) pointer to an integer a. So p is a n elements array of pointers to functions, each function takes an argument as pointer to an integer and return a pointer to a float. 23. (a) Given post-fix expression is 16 2 4 / 4 3 + 6 2 Expression OP1 OP2 Value Top op of stack 16 16 2 16, 2 4 16, 2, 4 (max height = 3) 2 4 16 16, 16 / 16 16 1 1 4 1, 4 3 1, 4, 3 4 3 12 1, 12 + 1 12 13 13 6 13, 6 2 13, 6, 2 6 2 12 13, 12 13 12 1 1 (output) 24. (c) a 54 b x y z 400 10 200 10 200 54 400 15 100 200 300 400 1. x = x (y + z); x = x (5 + 15) = 10 (20) = 200 2. y = 200 << 1 = (11001000 ) 2 << 1 = (110010000) 2 = 400 3. z = x + y = 200 + 400 = 600 25. (b) Trace of matricx take a matrix m = n = 3 a 0 0 0 b 0 0 0 c 500 600
12 Computer Science & IT then sum = 0 + a, sum = a + b, sum = a + b + c sum = a + b + c which is trace of matrix. 26. (b) To evaluate the infix and prefix expression 1 operator stack is used. To evaluate the postfix expression 1 operand stack is used. 27. (d) a 20 100 28. (c) 58 a = 20 + Rec(3) Rec(4) a = 20 + a + a a a + 1 + a + 1 a + 1 a = 20 + 3 + 3 3 3 + 1 + 3 + 1 3 + 1 = 20 + 3 + 27 + 1 + 3 + 3 + 1 = 58 a p 90 98 96 84 70 a+0 a+1 a+2 a+3 a+4 a+5 a+2 a+1 a a+3 a+4 a+5 p+0 p+1 p+2 p+3 p+4 p+5 S p+4 p+5 p+1 p p+2 p+3 S+0 S+1 S+2 S+3 S+4 S+5 Ptr S+3 (ptr + 2) (p + 2) = ( ( ( (S + 3 + 2)))) ( ( (p + 2))) = ( ( (p + 3))) ( (a)) = 96 90 = 6 29. (c) If x be the number of vertices with p connected components, then maximum number of edges in X is given by size of each component in graph = x / p. xx ( p ) Maximum number of edges in each component =. 2 Total number of edges = Number of components Number of edges in one component x x 1 p p xx ( p) = p = 2 2p 10(10 5) So maximum number of edges = = 5 2 5 30. (c) 1. x y return (func (75, 25)) 2. x y return (func (25, 0)) y == 0. So x = 25 x 100 1000 y 75 25 75 25 2000 0