You are on page 1of 15
Important Questions eT ays! COMPUTER SCIENCE & IT India’s Best Institute for IES, GATE & PSUs Day 2 of 8 ieee ee PSU & Govt. Jobs ‘ONGC, IOC Cl, SAIL, GA, MLC OI, PEC HAL, RMD, NALCO, MDL, MPCIL BDA Elceey Boned and many more. Research Courses SUBJECT-WISE WEIGHTAGE ANALYSIS OF GATE SYLLABUS Subject Average % (last 5 yrs) Reasoning, Aptitude & English 15.00% Dirgiione eerie Programming & Data Structure 12.20% « » 7 Computer Networks 820% am treoanmings Theory of Computation 870% Operating Systems 8.40% Computer Organization & Architecture 8.40% Algorithms 230% Engineering Mathematics 7.83% Database Management System 730% coo 7 tot Digtal Lage sso “flor >» PE Ste Algorihme compas RAOR ‘Compiler Design 460% Discrete Mathematics 557% Total 100% Important Questions famane Easy for cs 2.26 26. Q27 27. Consider the following function, to which a pointer to the root node of the binary tree Tis passed. int MadeEasy(Struct Node * root) { inta=0,b=0,c=0; NULL) return 0; if(root + left == NULL &&e root + right a = MadeEasy(root + left); b = MadeEasy(root -» right); c= 1+ max(a, b); return ¢; } The above program computes, (a) The number of edges in the longest path from root to leaf in T (b) The number of nodes in the longest path from root to leaf in T (6) Number of leaves in T (d) Total number of nodes in T ) The above program computes the number of levels in T. Option (b) is actually the definition of the number of levels (or depth) of a tree. For example, if the tree T has only one node, it returns 1. So options (a) and (¢) are eliminated, as if (a) was supposed to be true, it should have returned 0. Now the only dilemma is between (b) and (4), which can also be handled easily, as the line c= 11 + max(a, #) should have been ¢ = 1 + a+b instead, but that’s not the case and therefore it only. computes the number of levels in T. Alll this explanation is given to help you solve the question in the exam quickly, but with a lot of practice, just by looking at the code you will be able to tell what the function can do and cannot do. NULL) return 1; Let PRE, IN, POST, LVL denote the preorder, postorder, inorder and level order traversal respectively. Then which of the following combinations of traversals is not sufficient to uniquely construct a binary tree? (@) (PRE, IN) (b) (POST, IN) (©) (LVL, IN) (a) (POST, PRE) (a) PRE and POST are not sufficient, as we need one of them to determine the ROOT, and the other is supposed to determine the left subtree and the right subtree respectively, but in case of PRE and POST, both give the information only about the root. So the correct choice is (4) rere near Day2 : Q.26toQ.50 © Copright:anceasy Page T Delhi Poo ea Eee ne 8 MADE EBSH for CIXTAELEA | CS Q.28 A program attempts to generate as many permutations as possible of the string, ‘xyzw’ by Q.29 Consider the following C code: pushing the characters x, y, z, w in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program? (a) xyzw (©) wzyx (b) zyxw (2) 2xyw a) For option (a): Push x, pop x, push y, pop y, push z, pop z, push w, pop w. For option (b): Push all of x, y, 2, w first; and then pop them at once. For option (c): Push x, y, 2; now pop all of these, and now push w and pop w at the very next moment. But option (d) is clearly not possible because first x, y, zis pushed; then once zis popped, y has to be at the top of stack, and so x cannot come in the output before y in a situation like this because pushing has to be done in the order already specified in the question. So option (4) is not possible, include int f(int *p, int n) { if (1 <= 1) return 0; int m = pf] - pl0]; int r= fp +1,-1); int x = max(m, 1); return x; I int main(void) { int al | = {7, 3, 8, 1, 2, 9}; printf("%d”, fla, 6)); return 0; 1 The output of the above program will be (a) 10 1 ©@9 (d)7 29. (a) ‘The program finds the maximum difference between two adjacent pairs in the array. So out of all adjacent pairs in the array, 8 - 1 = 7 will give the maximum difference. ae Day2 : Q.26toQ.50 © Copyright:maneeasy Page 2 Delhi Poo ea Eee ne Important Questions famane Easy for cs Q.30 Consider the following AVL tree: @ @ B ® ®@ ®@ @ Which of the following order of elements are inserted into an empty AVL tree, so that it is possible to get the above AVL tree? (a) 94, 71, 86, 25, 98, 83, 27, 90 (b) 98, 94, 90, 83, 86, 25, 71, 27 (©) 86, 25, 98, 83, 27, 90, 71, 94 (d) None of these 30. (@) The order: 86, 25, 98, 83, 27, 90, 71, 94 will result the given AVL Option (¢) is correct. [Note: Option (a) & option (b) will generate different AVL trees} rere near Day2 : Q.26toQ.50 © Copright-aveeasy Page ec Important Questions famane Easy for cs Q.31 Consider the following statements: S,: Connected components of a graph can be obtained by depth first traversal but not by breadth-first traversal. S, In DFS traversal every vertex of the graph is visited exactly once. Which of the following options is correct? (a) Only S, is correct (b) Only 5, is correct (©) Both S, and S, are correct (d) None of the statement is correct 31. (a) + Both BFT as well as DFT can be applied to find the connected components of a graph. + No, vertex can be visited as many times as the nodes adjacent to it are unvisited. Q.32 Consider the following graph: ‘The minimum size of queue required when performing BFS on above graph is (ize of queue is represented by maximum number of element at any time). 32. (a) ‘Assume BFS starts with vertex “A” sept: [A step 2: [K] BF adjacent of sep: [R[FIE© adjacent of sep«: (Fees adjacent of ses: [E[EIS sop: [Ze] 0 adjacent of'¢* steo7: [B]B see: [Fl Ste 8 ‘So, minimal size of queue is 4. rere near Day2 : Q.26toQ.50 © Copright-aveeasy Page ee aa Eee ne 8 MADE EBSH for CIXTAELEA | CS 2.33 Consider a small circular linked list. Which of the following(s) is/are correct way to detect the Q.34 Consider the following New-order strategy for traversing a binary tree: presence of cycles in this list effectively? (a) Keep one node as head and traverse another temp node till the end to check if its ‘next points to head. (b) Have fast and slow pointers with the fast pointer advancing two nodes at a time and slow pointer advancing by one node at a time. (©) We can apply depth first search in which back edges represents the presence of cycles. (d) Circular linked list itself represents a cycle. So no new cycles cannot be generated. (b, ¢) ‘Advance the pointers in such a way that the fast pointer advances two nodes at a time and slow pointer advances one node at a time and check to see if at any given instant of time if the fast pointer points to slow pointer or if the fast pointer’s ‘next’ points to the slow pointer. This is applicable for smaller lists. Similarly, DFS can be used to check the presence of cycles. + Visit the root; + Visit the right subtree using New-order; + Visit the left subtree using New-order; The New-order traversal of the expression tree corresponding to the reverse polish expression 3.4"5-2°67*1 +~is given by (a) +-167*245-34" () -+1°6792-5"34 (©) -#1°7642-5"43 (a)176742543*-4- Me ) Reverse polish expression is another name for postfix notation. Since postfix notation is similar to postorder (LEFT, RIGHT, ROOT) and new order strategy is (ROOT, RIGHT, LEFT), both are reversals of each other. Hence option (¢) is the correct answer. ae Day2 : Q.26toQ.50 © Copyright:maneeasy Pages Delhi Poo ea Eee ne famane Easy for cs Q35 2.36 Q37 37. The number of binary search tree’s with 4 nodes (1, 2, 3, 4) possible where in every binary search tree ‘I’ is leaf node are @4 5 os (a3 © (3), @) @ @ @ (2), ® © @ ‘There are 5 binary search tree's are possible only. Consider 3 dimensional Array A[90] [30] [40] stored in linear array in column major order. If the base address starts at 10, The location of A [20] [20] [30] is (Assume the first element is stored at A[1][1][1] and each element take 1 memory location) (23699) ij k Let Ale] le) Is] 9 63040 (planes) (rows) (colums) For column major order loc (A(t j, 8) Base Address + [(i- Iryrs + (= ry + G- DI 110 + [19 (30) (40) + 29 x (30) + 19] = 23699 Let T be a rooted binary tree whose vertices are labelled with symbols a, b, ¢ d, € fe 81 Iti js ke Suppose the inorder (visit left subtree, visit root, visit right subtree) and post-order (visit left subtree, visit right subtree, visit root) traversals of T produce the following sequences. Invorder: 1b, 6, dy &, fi & Ih i jek Post-order: a, ¢, by &, fi yj, ky ip yd ‘The number of leaves in T are 6) We can easily construct a unique binary tree from the Postorder and Inorder information given above, The leaf nodes will be a, ¢, ¢, hj; and therefore number of leaves = 5. rere near Day2 : Q.26toQ.50 © Copright:maveeasy Pages Delhi Poo ea Eee ne 8 Important Questions MADE Easy for (ENTETPA | Cs Q.38 Consider the following C code: Q.39 Consider the following C program: #include int main(void) { static int i= 6; if(—-i) { main( ); printt(“%d”, i + 1); } return 0; I Let X and Y denote the number of 0’s and 1’s printed by the above program respectively. Then the value of 2X + Y will be equal to 6) Output will be T1111 No of 0's printed = 0, no of 1's printed = 5. ‘Therefore 2X+Y =5 #include #define black(a) a+ ata define white(a) a ~ ata int main(void) { inta = 3; printf(’%d”, black(a)"black(a + 1) - white(a)); return 0; } ‘The output of the above program will be (a) 25 (b) 26 (©) 30 (d) 28 39, (b) The expression will evaluate to, 343*3*3414341341-3-393 3+ (3*3*3)+1+3+(1*3)+1-3-(3*3) 34+274+14+343-2-9 26 www.madeeasy.in Day2 : Q.26toQ.50 > Copytight:manceasy Page 7 Delhi Coe ey famane Easy for cs Q40 Consider the following C function which adds a given node pointed by q at the tail of the linked list. (Note that the next pointer of the new node to be inserted is already initialized as NULL). Struct node * add (struct node * head, struct node * q) t if (1 head) return q; struct node * p = head; while (p > next! = NULL) B, I By Bs return head; } A person new to programming attempts to fill these boxes B,, B,, B, in ways as follows: 1 By: [p=p—next By: [pnext=q: By: [q—>next = NULL; UL By: [p=p—next; By: [next = NULL; UL B,: By: [ponext=g; The number of correct ways of filling the boxes so that the implementation works correctly is equal to 40. 2) Only | and II will produce the expected output III will go into infinite loop because of box B and won't work. ‘Therefore 2 is the answer. rere near Day2 : Q.26toQ.50 © Copright-aveeasy Pages ee aa Eee ne Important Questions famaoe Easy for (TRIPE 4 cs Q.41 An implementation of a stack S, using two queues Q1 and Q, is given below. Delete the element from stack S$ and store it in x: void pop(S, x) { if (isEmpty QQ) printf("S is empty”); return; 1 while (1) { x = dequeue(QI); if (isEmptyQ(Ql)) break; enqueue(Q2, x); I while (! isEmptyQ(Q2)) enqueue(Q1, dequeue(Q2)); } Insert the element x into stack S void push(S, »)f enqueue(QL, x); } How many enqueue contains n elements ly? dequeue operations are required to perform a pop operation if Ql (a) 3041, 2n-1 (b) 2n-1, 20 (©) 2n-2,2n-1 (d) 2n-2,3n-3 nC) rere near Day2 : Q.26toQ.50 © Copright:aveeasy Page? ee oc 8 MADE EBSH for CIXTAELEA | CS Q.42 Consider the following C code: Q43. Consider the following expression in reverse polish notation, #include #include int bar(int m, int n) return bar(n%m, m); 1 int foo(int m, int n) t I int main() t return (ni*r/bar(m, n)); int x = fo0(1000, 1500); printf ("%d”, 2); return 0; } The output of the program will be . (3000) The function foo( ) computes the LCM of the integers m and m, given as input. Let's see why. We know the relation, LCM(m, 1) * GCD(m, n) = m* n So we can write, LCM(m, n) = m* n/GCD(m, n) ‘So we know that the LCM of 1000 and 1500 will be 3000. Therefore 3000 will be the answer. 100 200 +2 / 5*7 + 245-256 -4* Let the value obtained by the evaluation of the above expression be X. Then the value of VX will be 43. (2) Upon evaluating using the postfix evaluation algorithm, we see that X = 1024. Therefore VX = 32 Hence 32 will be the answer. ae Day2 : Q.26toQ.50 © Copyight:Maveeasy Page 10 Delhi Poo ea Eee ne 8 Important Questions MADE Easy for (ENTETPA | Cs Q.44 Consider the following C implementation which when given 3 numbers 4, b, ¢ as input, finds the maximum of 3 numbers a, b and c. int kickstart(int a, int b, int ¢) { if{(B1) return a; if (a >= b) return B2; return kickstart(c, a, b); } A student named Deepak attempts to fill in these boxes B1, B2, and B3 in the following ways: 1 Bl:a >= b &6 a> c; B2: kickstart(c, b, a); IL, Bl: a >= b &8 a >= c; B2: kickstart, b, a); IIL, Bi: a >= b && a >= c; B2: kickstart, 9, b); IV. Bl: a >= b &&e a >= ¢; B2: kickstart(®,¢, a); For which of the above approaches, the implementation fails to work correctly? (a) Only (b) Iand I only (©) U, Land IV only (4) Land IV only (@ I will fail the case when @= For example, take kickstart(1, 1, 1) kickstart(1, 1, 1) will keep calling itself in case 1, and won't produce any output, thus overflowing the stack. Tl and IMI will work totally fine for all inputs. However IV will fail in some cases, for example take kickstart(2, 4, 3). kickstart(2, 4, 3) => kickstart, 2, 4) = kickstart(2, 4, 3) = kickstart (3, 2,4) =... ‘So as it can be seen the recursion looks like a cycle of length 2 (just for the sake of clarity) and the program will go into a hang. So (d) is also an incorrect implementation, ae Day2 : Q.26toQ50 © copyightiMaveeasy Page 17 Delhi Poo ea Eee ne famane Easy for cs Q45_ Consider the following function height, to which the pointer to the root node of a binary tree shown below is passed. Note that max(a, b) is defined by the declaration #define max(a, 6) (a> b) ? a:b. int height(Node *root) t if (froot) return =1; if(root + left && toot — right) return 1 + height{root + left); if(troot — left && root — right) return 1 + height(root + right); return 1 + max(height(root — left), height(root > right)); Q ®@ @ ® @ The output obtained when the root of the tree given below is passed to the above function is (a1 (b) 2 @3 (d) None of these 45. (d) ‘The output obtained will be ~1. The reason is because, the expansion of max(a, b) will be (a> b) ? a :b. There's a big problem in this, as we need another set of parenthesis in bolds like this: ((a> b) 20:8) In the absence of these brackets, the conditional operator 2: has lower precedence than ‘+’, so first the comparison between a and b (leftHeight and rightHeight) will be done and then + will be evaluated before 2, leading to a different result than expected, and it can be verified that the program will output -1 for the given input. Q.46 Let S be a binary search tree with 127 nodes. Let x and y denote the maximum and minimum possible height of S. Then the value of x and y will be (a) 127,7 (b) 127, 6 (2 126,6 (A) 128, 8 6. Height will be maximum if the tree is either left skewed or right skewed. So Max Height = x= 127-1= 126 Height will be minimum if $ is a complete binary tree. So Min Height = y = log(127 +1) -1 log(128) - 1= 6 Hence (c) will be the answer. ae Day2 : Q.26toQ50 © copyightimapeeas Page 12 ee aa Eee ne 8 Important Questions MADE Easy for (ENTETPA | Cs 47. Q47 What values will be printed by the program given below? Q.48 Consider the following C codes: Hinclude void flint *p, int *g, int *r) int main() { inti=0, S(Sej, &i, &); printt(“%d %d d\n", ky i fs return 0; } (a) -1,2,-3 () 1.2.3 (©) -1,3, -2 (d) -2, -1,3 (a) ‘After the execution of the above code, the values of i,j,k will be ~1, 3 and -2 respectively. Since the output is asked in the order of k, i and j, the values printed will be -2, ~1 and 3. So option (d) will be the correct answer. int foo (Struct Binary Tree Node * root 1, Struct Binary Tree Node * root 2) { if (root 1 = = NULL && root 2 = = NULL) return 1, if (root 1 = = NULL|| root 2 = = NULL) return 0; if (root 1 data ! = root 2 data) return else return foo (root 1 — left, root 2 —> right) && foo (root 1 > right, root 2 — left); I ‘The root node of two Binary trees are passed as an argument to the given code. The code returns 1 if, {a) Both the trees are having same height if height is defined as the distance from root to the farthest leaf node. (b) Both the trees are Binary Search ‘Trees, (©) Both the trees are mirror images of each other. (d) Both the trees have equal number of nodes. 48. () The code is checking whether the two trees are mirrors of each other. It returns 1 if the above property exist, 0 otherwise. www.madeeasy.in Day2 : Q.26toQ.50 > Copyihtmaneeasy Page 13 Delhi Poo ea Eee ne 8 MADE EBSH for CIXTAELEA | CS 49, Q49 Consider the following function secret( ). Q.50 Consider the following C declaration: Hinclude unsigned char secret(unsigned char x, int y) { return ((x & OxOF) << y| (x & OxF0) >> y); } int main() unsigned char x = 100; printf(“%u", secret (x, 4)); return 0; } ‘The output obtained corresponding to the program is (70) Since the value of y is 4, the above function will swap the two nibbles in the given byte. The idea is to first get the last 4 bits ie. lower nibble and get the first 4 bits i. the higher nibble and then combine them using bitwise OR. ‘The expression “x & OxOF” gives us last 4 bits of x, which in turn is left shifted 4 times in order to promote the lower nibble to a higher nibble. Similarly the expression “x & OxFO" gives us first four bits of x, which is right shifted 4 times in order to set it up for becoming the lower nibble. And finally we apply bitwise OR between them, which will give the final number after having swapped the 2 nibbles of the given byte. Since x = 100, the binary representation of x will be 01100100, Now after interchanging the nibbles, we get 01000110, whose value in decimal is 70. ‘Therefore 70 will be the answer. struct { short x5]; union { float y; long 2; }u; short w)2]; te Assume that the objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable ¢ (ignore alignment considerations) is: (@) 22 bytes (&) 4 bytes (©) 18 bytes (a) 10 bytes 50. (a) Memory required = 2x 5 + max(sizeof(y), sizeof(2)) + 2* 2 10+8+4 0000 ae Day2 : Q.26toQ50 © copyightiMaveeasy Page 14 Delhi Poo ea Eee ne

You might also like