You are on page 1of 48
1.1 Study the following program written ina block- structured language: var x, ¥ : integer; procedure P(n:integer); begin x:=(n+2)/(n-3); end; procedure Q var x,y: integer; begin ae PO); Write (x) ‘ en) end; begin xi57 wo) What will be printed by write statements marked (1) and (2) in the program if variables are statically scoped? (a) 3,6 &) 6,7 © 3,7 @ None of these (1987: 2 Marks] 1.2 Inwhich of the following case(s) isit possible to obtain different results for call-by-reference and call-by-name parameter passing? @) Passing an expression asa parameter () Passing an array as a parameter (© Passing a pointer as a parameter @ Passing an array element as a parameter [1989: 2 Marks] 1.3. Match the pairs in the following List-I A. Pointer data type B. Activation record C. Repeat-until D. Coercion * Programm Ing List-I ~ P. Type conversion Q. Dynamic data structure R. Recursion S. Nondeterministic loop 11950,» 14 Indicate all the true statem following: (a) A programming language either recursion or pointer type dj” the support of dynamic memory 4 i () Although C does not support cally, parameter passing, the cell canta simulated in C, PO uy, (1991-1 May 1.5. Match the pairs in the following: List-I A. Smalltalk B. LISP C. Prolog D. VAL tI P. Logic programming Q. Data flow programming R. Functional programming S. Object-Oriented programming [1993 :2 arts Common Data Questions 1.6 & 1.7: Consider the Pascal program shown in below. program PARAM (input, output): var m, n : integer; procedure P(var x, y : integer); var m : integer; y+; end; a Procedure Q (x : integer; var y :integ*™" begin x:=y41; end; 4 Scanned with CamScanner Poa al programming & Data Structures a pm, m); write (m); rite (n) ve 20% 9, Q(t nein onty he Drogram PARAM FF gase mia lca! variable in P ip Hemant m inthe actual parameter that ut Coord tothe formal parameter inp srige both xandy are just reference to @ oy has the value 0 Ppecase both xand y are just references to Mich gots modified in procedure P 11993 : 2 Marks} spevalve orm, outPat by the program PARAM : 12 use n is the actual parameter toxin procedure Q. the actual parameter to y in : becat OO pond because 118 onedure "gen in the actual parameter 2 epondingtoxin procedure Q. g arteenne n in the actual parameter J emndingtoy in procedure Q. fg nme ofthe above [1993 : 2 Marks} 1.13 shat isthe soope of m declared in the main program? (@PARAM,P,Q — @) PARAM,P (@ PARAM, Q @P.Q [1993 :2 Marks] $ What does the following code do? integer; esa ath; ben; ab ed (9 exchanges (a) and (b) i heed (@) and stores in (b) Gen Mand storesin (@) es (a) and (b) unchanged [1993:2 Marks] 114 orm, Nim : : plementation do not permit “Tien because (0) they use static allocation for variables () they use dynamic allocation for variables (© stacks are not available on all machines (@) it in not possible to implement recursion on. all machines. An unrestricted use of the “goto” statement is. harmful because (a) it makes it more difficult to verify programs () it increases the running time of the programs (© it increases the memory required for the programs (@ it results in the complier generating longer machinecode [199421 Mark), In which one of the following cases is it possible to obtain different results for call-by reference and call-by-name parameter passing methods? (a) Passing aconstant value asa parameter () Passing the address of an array 43 4 parameter (© Passing an array element as a parameter (@ Passing an array {1994 :2 Marks} What is the value of X printed by the following program? program COMPUTE (input, output): var Xinteger: procedure FIND (real); begin X: = sart(X); end; begin x Find) Writeln®) end @1 ®2 © Runtimeerror None of these {1995 :2 Marks} ‘The correct matching for ‘the following pairs is List-I List II ‘A. Activation record 1. Linkingloader B. Locationcounter 2. Garbage collection C. Referencecounts 3. Subroutine call D. Address relocation 4. Assembler Scanned with CamScanner 00 Codes: @ A-3,B-4,C-1,D-2 () A-4,B-3,0-1,D-2 (9 A-4,B-3,C-2,D-1 +3,B-4,C-2,D-1 @A-aB [1996:2 Marks] 1.15 Heap allocation is required for languages (@) that support recursion () that support dynamic data structures (© that use dynamic scope rules he abovt Natalee [1997:1 Mark] 1.16. Given the following Pascal like program segment Procedure A; x yiinteger; Procedure B; x,zreal; si end B; Procedure C; itinteger; s2 end C; end A; ‘The variables accessible in S1 and S2 are (@) xofA, y,xof Bandzin $1 and xof B, yandiinS2 © xofB,yandzinSi and xof B, iandzin S2 © xofB,zandyin Si and xof A, iandy in S2 @ None of the above [1997:2Marks] 1.17 Faster access to non-local variables is achieved using an array of pointers to activation records calleda (@) stack © display (©) heap @ activation tree (1998: 2 Marks] 1.18 Given the programming constructs @ assignment @ forloops where the loop parameter cannot be changed within the loop Gi) ifthen-else Gv) forward goto (W) arbitrary goto GATE Previous Years Solved Papers 300 | S| Moe (vi) nor Procedure ea)) 1.19 1.20 121 N (vii)reoursive procedurelfunct, (viii) repeat loop, which constructs will you not i, programming language such thay tie 4 possibleto program that terminatgs Showy function the same programing ional, © G.GD.G9 © (cay gee © (A, 649, 4) Goa [i999 i Consider the following program in aly that has dynamic scoping: Rg var x: real; procedure show: begin print(x);end; procedure small; var x: real; begin x:= 0.125; show; end; begin x:=0.25; show; small end. Then the output of the program is; (@) 0.125 0.125 &) 0.250.25 (© 0.250.125 @) 0.1250.25 [1999:2 Marg Acertain processor supports only the immaie and the direct addressing modes, Which ltr following programming language featurescamt be implemented on this processor? (@) Pointers © Arrays (© Records @ Recursive procedures with local variable [u999:2Marls Consider the following C function definition int Trial (int a, int b, int c) { if (a>=b) && (e =b) return Trial (2.64 else return Trial (b,2.0); } ‘The function Trial: (@) Finds the maximum of a, bande () Finds the minimum of a, bande 7 © Finds the middle number of a, bam Nc @ None of the above j1999:2 art) Scanned with CamScanner programming & Data Structures om post @PP ¥ pis List-I1 sot ist" addressing 1. Loops 5 ainteadressing 2 Pointers, y. me erement 3 Constants toe BT aressing ao ar y-32-2 OX gy-a0-1 Gaye? petting asarations F u0del int post (2000: 1 Marky B etsoe 10] . define tobe array, © s pare of type node ip etructare of 2 ld, cach field being @ iter to an array of 10 elements ig Astrcturo of 8 fields: an integer, a oat, sndanarray of 10 elements (gan array, each element of which is @ structure of type node .ch element of which is a pointer [2000: 1 Mark] 10 Themost appropriate matching for the following pis List-l X memalloo(6); m = NULL; ¥, fee(n);n->value=5; 2 dary *p=a List 1. using dangling pointers 2 using uninitialized pointers 2 bstmemory (oes: OxXty 32-2 OX-2v-123 OX-ayo7 1 OX-sy 179 [2000: 1 Mark] y ing ey text of programming languages 126 127 1.28 l CoO nstintemms, ropriate matching for the followin, 1B (@) multiple variables having the same memory. location () multiple variables having the same value (©) multiple variables having the same identifier (@) multiple uses of the same variable [2000: 1 Mark] Consider the following C declaration struct { short [6]; union { floaty; long z yu; Ht ‘Assume that objects of the type short, float and Tong occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is (@) 22bytes () 14bytes (© 18bytes @ lbytes [2000:1 Mark] ‘The value of j at the end of the execution of the following C program is__ int inor (int i) { static int count = 03 count = count + i;014 return (count); } main () { int i, for (= 0;1<=45 +4)” j=iner(); i @ 10 o4 @6 @7 {2000 :2 Marks) What is printed by the print statements in the program Pl assuming call by reference parameter, passing? Program P10 { x=10; y=3; funel(y, x, ¥); print x; Scanned with CamScanner So Awe eee I rrrterrerer rerearecteer: | care revousvenrs Solved Papers: (ED. | tore, won); printy; end ) bogin begin P2 funcl(s, Ys) n= 10; ( D; +4 end paxty th If the language has dynamic scoping ) ais parameters #72 passe by reference, vane 10,3 ited by the program: i o 27,7 (@ Noneof these ta mal us wu (200112 Mar! 03 (@ None of these 1129 Consider the following three C functions: (2001 2Mary ‘The results returned by function under vy (Pi) _int*g (oid) ( int x= return (8); , (2) int*e (void) i int px: *px= 10; return px: ) (P3]_—_int*g(void) ( int *px; P *px = 10; return px; } Which of the above three functions are likely to cause problems with pointers? (@) Only PS () Only Pi and P3 (© OnlyPlandP2 (@ Pl, P2andP3 [200: 1.30 Consider the following program Program P2 var n:int; procedure W(var x:int) begin x=xtl; printx; end procedure D begin var n:int; (int *)malloc (size of (int); Marks} reault and reference parameter passi, ing conventions (@ Donot differ © Differin the presence of loops (© Differin all cases (@ May differ in the presence of exception [2002:1 Mark) Inthe C language {@) at most one activation record exists betwee the current activation record and ty activation record for the main () the number of activation records betweenth current activation record and the activate record for the main depends on the actu function éalling sequence (© The visibility of global variables depends the actual function calling sequence @ Recursion requires the activation recor it the recursive function to be saved cat different stack before the recursive funtia canbe called. [2002:1 Masi Consider the following declaration of # dimensional array in C char a{100](100}; Assuming that the main memory is addressable and that the array is stored from memory address 0, the address o!* J [50] is (@) 4040 (b) 4050 © 5040 (@ 5050 - 1200223 Scanned with camScanner” ramming & Data Structures following C variable declaration che ie) ssh BOIL! iin xression® 3) £921 t give compile-time error ifused : in er eassienmentsatomeraing sino a cprend SonlY ‘only () 2, Band 4 only @ 4only sand @? [2003 :1 Marky 1g C program fragment), k, nand rd wire intoger variables, and A is an 1 inter ‘The variable n is intialized to spinteger 25 and ‘TwoLog_n is initialized to evaluc of 2*[Joge(n)] rte sik <= mH AL=05 <= TwoLog_n; k++) keh j<= mi Gy 1 626405 eG 3a) fqAG) print £%4" 5 ‘he set of numbers printed by this program ents @ fal msn, Gi) [m= i) 6 (| msn, Gi) [m=¥]} @ (m| mSn, mis prime} ao [2003 : 2 Marks] | Ca ler the C program shown below. #include # define print (x) printf ('%4”, 3) intx; void Q (int 2) { 2+=x; print (@); } void p Git *y) { j intx=*y +2; Q@;*y=x-L ; print (x); : ‘main (void) { x55; sr 30! PG; print @);, © 1466 @ 166 {2003 :2 Marks} 1.87. The goal of structured programming is to (a) have well indented programs @) be able to infer the flow of control from the compiled code (© be able to infer the flow of control from the program text @ avoid the use of GOTO statements (2004: 1 Mark] 3 } ” ‘The output of this program is (@) 1276 @) 221211 1.38. Consider the following C function void swap (int a, intb) {int temp; temp=as a=b; b=temp; } Inorder to exchange the values of two variables xandy. @) call swap (9) (b) call swap (&x, ‘&y) © swap ,y)! cannot betused as it’ does not return. any value @ swap (xy) cannot be used as the parameters are passed by value (2004:1 Mark] 1.30. Let xbe an integer which can take a value of © or 1. The statement if(@==0)x=15 else x=0; is equivalent to which one of the following? @ x=14% ® x =1-% @ x=x- @ x=1%x% {2004 : 1 Mark} 140 Consider the following © program which is eupposed to compute the transpose of a given 4x 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace Xin the program. Scanned with CamScanner Hinclude #define ROW 4 Hdefine COL 4 int M[ROW][COL] = {1, 2, 8, 4, 5, 6, 7, 8: 9. 10» 11, 12, 13, 14, 15, 16}; main() { int i, j, ts for (i= 0; i < 4; +4) { x } for (i= 0; < 4; +4) for (j = 0; j < 4; ++i) printf (%d”, MUG); } (@) forg = 0; j < 4; +4) { t= MU: ‘MGJG] = MGIB); MOJli) = & 4 ©) forg = 0; j < 4; +43) { MUiGl = t t= Milli; MO) El = MEE); © for =) <4; 4) { t= MOIti); M{ilG] = MU)Ei; M@jlbi) } [2004 : 2 Marks} 141 Whats the outputofthe following program? #include int funcf (int »); int funcg (int y); main() 14: 8 J as int x= 5, y= 10, count; for(count = 1; count <= 2 Hou { y+=funcfi(x) + funcg(s, printf (“%d", y); ’ } } funcf(int x) { int y; y= funeg(x); return (y); } funcg(int x) { static int y= 10; y+=1; return (y +x); } (@) 43 80 © 3337 () 4274 @ 3232 [2004 : 2 Marty Consider the following C program: #include typedef struct. { char *a; char *b; +t void f(t s); void £2(t *p); main() ‘ static ts = {“A”, “BY; printf(‘%s %s\n", s.a, s.b); £16); printf(‘%s %s\n", s.a, s.b); £2(&s); } void £1(t s) { sa =U; sb="v printf(%s %s\n", s.a, s.b); return; Scanned with CamScanner pa, pb); o ue\t oe ogg (680) yet } output generated by the program? ote wAB oy uv wv a v Ww vw @ A B ya B @ ay Wey, vw vv yw UV [2004 : 2 Marks] sjerthe following C function a ON gt fat) {_— staticinti= 15 sf(a>= 5) return n; nentij its return f(a); Fi returned by f(1) is 5 ws a7 @8 [2004 :2 Marks] 1 Onsider the following C program main() t int x, y, m,n; scanf ("%d 9d”, x, &y); 1* Assume x>Oand y>0*/ m=xn=Y; n=n-m; } printf (°% d”, n); The Pemam computes 1.45 1.46 147 (a) x ey. tsingerepeated subtraction (6) xmod y using repeated subtraction (6 the greatest common divisor of x andy (@ the least common multiple of x and y [20042 Marks} Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2 Group-t Group? P. Functional f., Command-based, procedural 2. Imperative, abstract data types Side-ffect free. declarative, expression QLogic R. Object-oriented 3. evaluation 4. Declarative, clausal S. Imperative : represent, theorem proving Codes: @ P-2,Q-3,R-4,S-1 (b) P-4,Q-3, R-2,S-1 © P-3,Q-4,R-1,S-2 @ P-3,Q-4,R-2, $1 {2004: 2 Marks] Consider the following program fragment for reversing the digits in a given integer to obtain ® new integer. Let n= dd, ~~ dy int n, rev; rev=0; while (n> 0) { rev=rev* 10 +n % 105 n=n/10; } ‘The loop invariant condition at the end of the i* iteration is (@) n=d; dyn, amd rov= dy dy doi n ) n= jey om dye dy oF POV = yg a @ nerev @ n=d,4, dy a d,, or rev = 4, [2004 : 2 Marks] b+ Consider the following C program segment: char p [21 char * s = “string” ; int length = strlen (s) ; for (i= 0; i < length; i++) Scanned with CamScanner carePrevousvearssoved Papers: 306 | i Ma De ey 1.52. Consider the following C-program double foo (double); /* Line 1 pliJ=sflength—i}; Fa ree int main 0{ ‘The output ofthe program is mnie © grists ‘input da ©) sizing b= foo (da); © enirt ) (@ nooutputis printed double foo (double a), [2004:2 Marks] return 8; it a? } 148 on int ivan tare cola comet ith up, (@) A function that takes an integer pointer a ee ItLine 1is deleted, the aboveca “. argument and returns an integer shor ; o ‘function that takes an integer pointer a8 (@) nocompile renee or orror argument and returns an integer pointer @) some compliorarsig rtd, (oA pointer to a function that takes an a Its integer pointer as argument and returns (9 Someconsl jerwaring de totype mig an integer eventually leading to unitended results (@ A function that takes an integer pointer as @ Complier errors argument returns a function pointer [2005 12 Mari [2005:1Mark] 1.53 Consider the following C-program void foo (int n, int sum ) { int k=0,j=0; 149 An Abstract Data Type (ADT) is @ sameas an abstract class () adata type that cannot be instantiated (© a data type for which only the operations defined on itcan be used, but none else eae PS @ allofthe above dean , [2005 :1 Mark] printf ("% dk); 1.50 A common property of logic programming } Janguages and functional languages is int main 0) { @) both are procedural language int a = 2048, sum = 0; (©) both are based on 4-caleulus fo0 (a, sum); © both are declarative printf ("%d\n", sum); @ allofthe above } [2005 :1 Mark What does the above program print? 1.51 Which of the following are essential features of ee Ora an object-oriented programming languages? ema @ 2,048.0 1. Abstraction and encapsulation ee 2. Stitly-typednese Linked Answer Questions 1.54 & 1.55: Asinkin a directed graph is a vertex isuch that He 3. Type-safe pr coupled with sub- : @1and2only 1 ofinheritanee edge from i to any other vertex. A directed grFb © 1,20nd4ont and 4 only with n vertices is represented by its adjacency ™=™ 2 ly @ 1, 3and 4 only A, where A[i]fi] = 1 if there is an edge direct [2005:1Marky vertex: toj and 0 otherwise. The following alge" determines whether there is a sink in the gr?" Scanned with CamScanner tAtilLi) and . 4 ‘api and By fo 5 AUDED and Ey? i+ [2005 : 2 Marks} thocorrect expression for By vs rg] ATED (atl && ‘agli fo cat) | ABIED fo cago) 111866 [2005 : 2 Marks} 1 C function takes two ASCII strings 136 Thefollowins ne is an anagram of and determines whether theother, An anagram of a string s is a string obtained by permuting the letters ins. int anagram (char *a, char *b) { int count [128], j; for G= 0; j < 128; j++) count{j] = 0; i= 0; while (afj] && bij]) for G = 0; | < 128; j+4) if (count fi) return 0; return 1; ) Corset ain he correct alternative for statements A . rome (aBy}+ and ‘Sount{bGj]|~— <= 1.67 1.58 \_307 [np] and int {hhh nt [alee] and AbGIF (@ A: count [afl}++ and B; count{bhj +41 = [2005 : 2 Marks} What is the output printed by the following program? # include int f(int n, int k) 0) return 0; ff (n% 2) return f(n/2, 2*K)+K; else return f(n/2, 2*k) — ki; , int main() printf(‘%d", f(20, 1)); return 0; , @ 5 © 8 ©9 @ 2 [2005 : 2 Marks] Which one of the choices given below would be printed when the following program is executed? #include struct test 7 int i; char *¢; } st[] = {5, “become”, 4, “better”, 6, “jungle”, 8, “ancestor”, 7, “brother"}; main() { struct test *p = sti pra Hp 6 printf(‘%s", ptt — ©); printf(‘%c”, *+tp — c); printf(‘%d”, p[0].i); printf(‘%s \n”, p — ¢); @) jungle, n, 8, ncestor © etter, u, 6, ungle © cetter, k, 6, jungle @ etter, u, 8, ncestor [2006 : 2 Marks} Scanned with CamScanner 308 | 1.59. Which one of the choices give! 1.60 sn below would be isexecuted? printed when the following program is #inelude void ewap (int *x, int *y) static int *temp; temp = x: ) void printab() { static int i, a= 3, b = 6; i=0; while (i <= 4) { if G+4)% ) continue; azati b ) swap (&a, &b); printf(‘a = %d, b = %d\n", a, b); } main() [2006 : 2 Marks} Which one of the choices given below would be printed when the following program is executed? #include 6, 7, 8, 18, 34, 67); (23, 56, 28, 29}; 1, a2, a3}; void print(int *a[}) { printf("%d,”", a[0][2)); printf(‘%d,", *a[2)); printf(‘%6d,", Halo); GATE Previous Years Solv 161 1.62 | og printf%d,”, “¢++ayiop, printC9éd\n",a[-i}f+1p, ) main() { print(x); } (@) 8, -12, 7, 23, 8 ) 8, 8, 7, 23,7 © -12, -12, 27, (@) -12, -12, 27, 2006 9 Ma ‘The following Function computes th my ay (¢)oorretly for algal values man, : n20andm>n) m4 int func(int m, int n) ( if (E) return 1; else return(fune(m —1, n) + fy D); Mata. } In the above function, which of the sl the correct expression for E? @ @==0)II(m==1) © (n= =0) && (m==1) © @==0)II(m==n) @ (== 0) && (m==n) [2006 : 2 Marks) ‘Match the following concepts and thei bet possible descriptions. Concept Description ] (@ overloading | (@) allows to deine a cassie Ihave properties of another class friend (© dofining a ser of similar functions Gi constructor | ()_used in derefereacing Go) protected — | (@) used to give a now member function access tothe private parts ofan object ‘Function which i ‘automatically called whet an object is created allows a derived lass Ihave access to the private parts ofthe base lass © this © (Wi inheritance | ‘pointer tothe abet associated withthe ‘eurrent function ) TH) used to obtain objet persistence ad Scanned with CamScanner vig [2006 : 2 Marks} ae the ollowingcode written ina pass.by, cons pce language like FORTAN and these ert nts about the code. ot routine swap (ix,iy) it= ix iy end ia p28 call swap (ia, ib + 5) print ia, ib end gq The oompler will generate code to allocate a temporary nameless cell, initialize itto 13, and passtheaddress of the ell toswap 0n execution the code will generate a runtime error on line L1 g0n execution the code will generate a runtime error on line L2 $k: The program will print 13.and 8 $§:The program will print 13 and -2 Tully the following set of statement (s) is caret: f Stand S2 (b) Stand S4 as @ Stand $5 [2006 : 2 Marks] 'W Consider the following C-function in which a [n] ‘adb[m] are two sorted integer arrays andc [n+m] ‘eanother integer array. ‘eiday2 int af}, int bl ], int ef J) { inti,j, 0; Mhile ( 1, (i) The function f terminates for infinitely many different values of n 2 1, (Gi) The function f does not terminate for finitely many different values of n > 1. Gv) The fumetion f does not terminate for infinitely many different values of n > 1, Which one of the following options is true of the above? (@ (and Git) © @ and Gv) (© Gi) and Gi) @ Gi) and Gv) (2007: 2 Marks) GATE Previous Years Solved Papers: 310 | pers: | Wap, nt f Consider the C Y rectly for 9 Consider the C program given |; implements the swap procedure © # include low: some but not all valid in ‘$5: may add or subtract integers a” int main() { int sum = 0, maxsum = 9, ; int a() = (2,2, -1, 3, 4, 9) a for(i = 0; i maxsum) maxsum = sum; sum = (ali] > 0) ? afi; 9, } else sum + = afi); if{sum > maxsum) maxsum = g, printf (‘%d\n", maxsum), } Whatis the value printed out when this pang, isexecuted? @9 ® 8 o7 @6 [2007 : 2 Ma 1.70 Consider the following C program: # include #define EOF -1 void push (int); * push the argument onthestai " int pop (void); /* pop the top of the stack */ void flagError (); int main() { int ¢, m,n, r; getchar ()) != EOF) if Gisdigit (c)) push (©); { flagError (); J printf(“% c”, pop ()); Scanned with CamScanner & rogramming |_ Programming & Data Structures i ie tg B2*332+%+ ® 2% 15 i" Si @ 150 [2007 : 2 Marks} der the program below in @ hypothetica, yp ge which allows global variables and dont ofcall by reference or call by value methods jot ii program” main() {int = 0% yeytis } {Which one ofthe following options represents the correct output of the program for the two parameter passing mechanisms? (@ Call by value : i = 70, j = 10; Call by reference + = 60; Call by reference : @ Call by value = 70; Call by reference : i = 100, j = 60 @ Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70 [2007 : 2 Marks] 1 Consider the program below in a hypothetical Programming language which allows global ‘wriables and a choice of static or dynamic Sping, 173 14 jg the output of the pro, Lan tis Program fj call g(); } procedure g() { } Let x be the value printed under static scopit andy bethovalueprntdunderdyrome eat i ‘Then, x and y are @) x=10,y=10 @) x=20,y=10 @ x=10,y=20 @ x=20,y=20 [2007 Marks) Early binding refers to a binding performed at compile time and late binding refers toa binding performed at execution time. Consider the following statements: (@ Static scope facilitates w1 bindings. i) Dynamic scope requires w2 bindings. (ii) Early bindings w3 execution efficiency. (iv) Late bindings w4 execution efficiency. ‘The right choices of wl, w2, w3 and w4 (in that order) are (a) Early, late, decrease, increase (&) Late, early, increase, decrease (©) Late, early, decrease, increase @ Early, late, increase, decrease {2007 : 2 Marks} Match the programming paradigms and languages given in the following table. List-I (Paradigms) I. Imperative IL Objectoriented TIL Funetional IV. Logic List-I (Languages) A. Prolog B. Lisp C. ©, Fortran 77, pascal D. C+, Smalltalk, Java Cod (@ [-C, ILD, LB, V-A © LAID, ILC, VB © LD, ILC, IB, IV-A @ LC, IED, I-A, IV-B {2008 : 1 Mark} Scanned with CamScanner nS 20IVOG Papers ; 312 | & ee ey F jables.¥ 1.78 Consider the C progenin give 1.75 Which combination of the ines int cae ™ piv elon yp, it he nd zmakes the variablon get # include wy following expression? ty: int main() na (x>y)2 (x22) 2852): (P2) 294 ( int i, i: int af8] = 11, 2.3, 4,5, 6, 7 4 [20081 Mark] nowing C for(i 3; ite 1.76 What is the output printed by the fol re ovde? # include int main() : for Gj = 7; j > 4, j-~) char a[6]=*world’s i int j--); int i = j/2; for G@=0,j=5;i 1.79 AC program is given below: # define swap! (a,b) tmp = # include void swap2 ( int a, int b) int main() t ( int tmp; int i, i; tmp = a; a= b; b= tmp; char a[2][3] = {{'’, b,c}, fd 'e, : char b(3][2): void swap3 (int*a, int*b) char *p = *; { for (i = 0; i < 2; i+4) int tmp; { ; tmp = *a; b; *b = tmp; for Gj = 0; j < 3 j++) 7 { i main() “(p+ 24 +i) =a fi] Gk int numi = 5, num? = 4, tmp; } if(num! > num2) d { swap1(numl, num2); } ) it if (num1 < numa) What should be the contents of the array > { swap2(num] + 1, num2); } the end of the program? iffnum > = numa) @ ad ®ad { swap3 (&num1, &um2), ed pie printf ('%d, %d, num, num2); ef ef } @ac @ae @ 55 ©) 5,4 eb de © 45 @ 4,4 af bf [2008 : 2 Mast (2008 : 2 Marks) Scanned with CamScanner © 2} rent 12); ) ) (175) print? Cae @ o1o101101 101 s 00 @ 10101101 01 ene [2008 : 2 Marks} following implementations will ofthe ate se output for f(173) as the one Feasbove question? Pz: Mots) void nt) ) if@<=D { it ol2); printf(‘%d", n);} } else printf'%6d", 022); {printf(“%d", n%2); } fov2);} } Both Pl and P2 (6) P2 only @ Plonly @ Neither P1 nor P2 [2008 : 2 Marks] 1 Whats printed by the following C program? infty, int “py, int ** pp2) | ity, 2; fmt 1p2=**ppz; a “py: j33 7 @ 19 @2 [2008: 2 Marks} () 18 a 1189 Choose the correct option to fill? and 72 90 that the program below prints an input string in foverse order, Assume that the input string is Terminated by a newline character. void reverse (void) { inte: if (21) reverse 0: 2 main ()( printf (Enter Text) ; pr reverse (); printf (Nn intf ("\n"); , (@) ? Lis (getchar oO nm’) 2 2is getchar (c); ( 7 Lis (c= getchar ())!!="\8) 22 is getchar (c) © 21is@!=%n) 22 is putchar (©); (@ 2 Lis (= getehar ())!= 2) 22s putehar (¢); [2008: 2 Marks] and Q.1.86 Linked Answers Q. 1.84 Consider the following C prograi Jocate an element x in an array search. The program is erroneous. 1. fant Y[10] , int x) { 1m that attempts to ‘Y[] using binary 2 inti,i, a i= 035 4, dof 5. k= (it 12% 6 if (Z[K] 2 @ Y is [2 4 6 8 10 12 14 16 18 20] and 2 {nt fun Gnt n, int *€){ *Lp=ls return 1; } t=fun(a-1, fp); f=t+*£p; “Ege return f; ) int main() ( intx=15; printf (% d\n", fun (6, x); return 0; ) The value printed is @6 ws ou @% [2009:1 Mark] 1.87 What does the following program print? include void flint *p, int *q) int main() { Ai, &}); printf ("%d %d\n", i,j; return 0; } ou 02 B019,, 1.88 What is the value printed by May program? (len 7 Hinclude g int f(int *a, int n) { if (n <=0) return 0; else if (4a % 2 ==0) return *a+fla+1, np, else return *a—f(a +1, n—; } int main () { int a[]= (12, 7, 13, 4, 11, 6); printf('%d", fa, 6)); return 0; } @9 ws © 15 @ 19 (2010: 2 Marty 1.89 The following program is to be tested fe statement coverage: b) {S1; exit;} else (83; exit;) S4; end ‘The test cases T1, T2, T3 and T4 given bel are expressed in terms of the properties sts by the values of variables a,b, cand d. Thee values are not given. T1:a,b, cand dare all equal b, cand d are all distinct T3:a=bande!=4 ™ and Which of the test suites given below e"* coverage of statements S1, $2, S3andS#? @ 1, 72, 73 @®) 72, T4 (© 13, T4 @ T1, T2, T4 [2010 Marl Scanned with CamScanner ve Gard toss’, p+ PLAI~ PLA; eamare2o1 = @) B01 ott @ou 2011-1 Mang pata Question 1.91 and 1,92; 980% pe fllowing recursive C function that takes sient no dint foo(unsigned int n, unsigned intr) la poy revarn (0%) foo(ahn2); aaseroturm OF 1 hati the return value ofthe function foo when 19h jscalled as fo0(845, 10)? 5 (b) 12 i 7 @3 [2011:2Marks} 2 Whats the return value ofthe function foo when itiscalled as fo0(613, 2)? @9 © 8 @5 @2 [2011:2 Marks} 193 What will be the output of the following C program segment? harinChar='A"; sviteh(inChar) as ‘A’: printf (“Choice A\ n"); case Be: case case D cae EB: defnult + printf (“NO Choice”);} @ NoChoice © Choice A Choice A Choice B No Choice gram gives no output as it is erroneous {2012:1 Mark] printf (‘Choice B") ; Consider the program given below, in a block- ‘Pseudo-language with lexical scoping ‘ndnesting of procedures permitted. _ Program mai Var... Procedure At; Var wn Call Az; Bnd At Procedure A2; Var Procedure Az; Ver... Call’, End Aat Call A21; End A2 Call Al; End main; we owing fragmes nar does the fOllOWiNg Fragment of Cy, | \ as Consider the calling chain: Main Al A2— A215 a1. ‘The correct set of activation records along with their access links is given by POINTER Access. LINKS ACCESS LINKS {2012:2 Marks] Scanned with CamScanner el il 6: Common Data for Questions 1.95 and 1.91 Consider the following C code segment int a,b, e= 0; void prtFun (void); main {otatic int a= 1)/"* Line 1*/ prtFund; at=l; prtFun0; printf(‘\n %d %d' a,b); void prtFun(void) {static int a= 2/*Line 2*/ intb=1; atstb; printf(’\n %d %4",a,b)s) 1.95 What output willbe generated by the given code segment? @3 1 m4 2 4 1 6 1 4 2 6 7 @4 2 @3 1 6 2 5 2 2 0 5 2 (2012: 2 Marks] 1.96 What output will be generated by the given code segment if: Line 1isreplaced by autoint =I; Line 2s replaced by register int aaa 4 2 4 1 6 1 4 2 6 1 @4 2 @4 2 C2 4 2 2 0 2 0 [2012:2Marks] 1.97 Whats the return value of fp, p), ifthe value of is initialized to S before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value int f (int &x, int ¢) ‘ { c=e-1; if (C= =0) return 1; x=xt+h return f(x, ¢) * x; } (@) 30% ©) 6561 (©) 55440 @ 161051 P2013: 2 Marks) 1.98 1.99 ty Consider the following program in g N #include lang, mainQ ‘te { int f int *pi= &i; seanf("%d" pi); printf(‘%d\n", i+5); } Which one of the following statement, (@) Compilation fails. @®) Execution results in a run-time erg, (© On execution, the value printed js than the address of variable @ On execution, the value printed jg 5 = than the integer value entered, me (2014 et-1): 1m sieTRD, Consider the following pseudo code What, total number of multiplications tobe peroryay D=2 for tondo for j=itondo fork=j+1tondo D=D+3 (@) Half of the product of the 3 consecutine integers. (©) One-third of the product of the 3 consecutive integers. (© One-sixth of the product of the 3 consecutie integers. @ None of the above. [2014 (Set-1) :2 Marks] 1.100 Consider the function func shown below: int func(int num) {int count = 0; while (num) { count++; num>>= 1; a return (count); } The value returned by function (495)i8—— [2014 (Set-2):1 a" nt 1.101 Suppose n and p are unsigned int variables" Cprogram. We wish to set p to"Cy, Hfnis cot which one of the following statements ! likely to set p correctly? Scanned with CamScanner [2014 (Set-2): 1 Mari ae following fanetion le cone joules) { a ate << 0,01) return x; ier fe 15: 1 qvatued (02 decimals) such that /(q) will ace urn [2014 (Set-2): 2 Marks} igerthe function given below. secant) it aticint (= 90: int n0=i printf'something”) K=fl0: return 0; is peru \ pith one ofthe following is TRUE? ‘The function returns O for all values of. ‘Thefunction prints the string something for allvaluesofj. (9 Thefunction returns 0 when j= 50. i Thefunction will eshaust the runtime stack rrun into an infinite loop when j =50. [2014 (Set-2):2 Marks} IN Theoutput of the following C program is. voidfl Gint a, int b) { inte; Hb; be; } ‘wid 2 (int a, int *b) { inte; 1 Ses taath bes ‘etmain() { inte=4, b=5, e=6; f(a, b); 2b, &; yet Odea by; [2015 (Set-1) : 1 Mark] lav 1.105 Consider the following paetida code, where yare positive integers, sierra begin while r2ydo begin r= yiqsatt; end end ‘The post condition that needs tobe satisfied after the program terminates is @ fr © =aetrader 0} [2015 (Set-1):2 Marks} 1.106 Consider the following C function. int fun (int n) { int x=1, k; if (n==1) return x; for (k=1; ken; +h) xex + fun (k) * fun (n—k); return x; i ‘The return value of fun (5) is: : [2015 (Set-2): 1 Mark] 1.107 Consider the following function written in the C programming language. void fo0 (char *a) { if (ta &&tal=") { ‘The output of the above function on input “ABCD EFGH” is (@) ABCDEFGH (b) ABCD () HGFEDCBA @) DCBA [2015 (Set-2):1 Mark] 1.108 A Young tableau is a 2D array of integers increasing from left to right and from top to bottom. Any unfilled entries are marked with =, and hence there cannot be any entry to the right of, or below a , The following Young tableau consists of unique entries. Scanned with CamScanner ~ ad Young hon an eloment in removed ON ta he tableau, other Jomontanhould bom | Young, Pane tet romDIO TG wih tableau (unfilled entries may be i he ia Tho minimum number oft Young 1) tobe shifted, to remove 4 from the B tableau is . jpors (Set2):2Mariel ygment 1.109 Consider the following C program #¢ # include int main() char al(7] = “1234”, *P: printf ("%8", 81); } What will bo printed by the program? (@ 12 (&) 120400 (©) 1204 @ 1034 [2015 (Set-3): 1 Mark] 1.110 Consider the following C program. # include < stdio.h> int main() { static int af ] = (10, 20, 30, 40, 50}; static int *p[] = (a, a3, a¥4, atl, a2}; int “*ptr =p; ptrt++; printf (4d%6d”, prt-p, **ptx); } ‘The output of the program is __. [2015 (Set-3):2 Marks] 1111 Consider the following recursive C function. void get (int n) { if (n < 1) return; get (n-1); get (n-3); printf (6a, n); } If get(6) function is being called in mai how many times will the get() fae pert before returning to the maing? 7 @b b) 25 © 3% @ 45 [2015 (Seta); 2 Marks] GATE Pravious Years Solved Pa, a | lt, 1.112 Consider the following € prog N # include ram: int main() { i,j k = 0; *BIN42015 4975 1 < 5; it) | k for(i awiteh(i + k) t case 1: case 2: print\n%e", gy printh(\ ner, jg ® intl 4 ‘The number of times printf stateme m Nisei, [2015 et): 1.113 Consider the following C program, # include int £1(void); int £2(void); int £3(void); int x = 10; int main( ) { int x = 1; x 42f1() + £2) + £3() + 205 pirntf(‘%d”, x); return 0; } int f1() { int x = 25; x++; return 5} int £2()) {static int x = 50; x44 retunt) int £3() {x *= 10; return x; } ‘The output of the program is __—~ 2015 Sees) :23a 1,114 Consider the following C program. void f (int, short); void main() { int i= 100; short s short *p = &s; Wcall tof () Scanned with CamScanner programming & Ds | Pros 19 & Data Structur fe ae of the following expressions, one blank above, will NOT rest fa wringerror? a orga © i= fG.0) whey @ fi") of [2016 (Set-1) #1 Maiq 1 ‘heoutput ofthe program is [2016 (Set-1) : 1 Mark] refollowing function computes the maximum ralve contained in an integer array p[] of size nin>=1)- : int max(int *p, int m) { inta=0,b=n—1; while ( ) ui it(ple] <=ple) { a=atl; } return pla}; pesistng loop condition is nes b!=0 Ob>@+y @o [2016 (Set-1) :2 Marks] Son, at will be t Pomang oe the out tput of the following Cc 1m? & void count(int n) {static intd=1; printf("%d" n); printf(%a", ); att if (n> Ye print((‘%el int (n= 1); } void main() { count (3); ) (@) 912213444 @) 312111222 (3122134 © (3121112 [2016 (Set-1) :2 Marks} 1.118 What will be the output of the following pseudo- code when parameters are passed by reference and dynamic scoping is assumed? a=3; x= x* a; prints); 3 void m() { a=le=y-anay print(a); } void main() { m(a); } @ 6,2 © 42 &) 6,6 @ 4,4 [2016 (Set-1) :2 Marks} 1,119 The value printed by the following program is. : void f(int* p, int m) { m=m+5 *p=*ptm, return; 3 void main () { inti=5, A&i, i); printf (*%d",i+J)5 } {2016 (Set-2):1 Mark] Scanned with CamScanner 320 | positive 1.120 The following function ‘computes X7 for integers X and ¥. int exp Gnt.X,int Y) {int res 1,02X, b> Ys while ( ! { £0% { a=ara; else {res 0) ) return resi , ae UE Which one of the following conditions 38 ‘TRI befor every iteration of the loop? ; @ x=0 0) oes Xrerseat — @ XT= (rest) : (2016 (Get-2):2 Marks) v= gest? 1,121 Consider the following program: int int *p, int n) {if <=) return 0; else return max (to#1, n-1), p(O] ~PLDs J int main () { intal]= 48, 5,2, 6, 4 print /(%d”, fi, 6); } Note: max (x, y) returns the maximum of x and y. ‘The value printed by this program is [2016 (Set-2) : 2 Marks} 1.122 The output of executing the following C program is include int total (int ») { static int count = 0; while (») { count+ = v & 1; v>>=; } return count; } void main () {static int x = 0; int i= 5; GATE Previous Years Solved Paper for (i> 0;i—-){ x=x+ total (); } printf (“%d\n", x); (2017 (Set) 9, May 1.123 Consider the C functions foo and) bers on int foo (int val) { int x = 0; while (val > 0) { x= x + foo (val — } return val; } int bar (int val) { int x { while (val > 0) x =x + bar (val - 1); J return val; } Invocations of foo (3) and bar (3) will resultin: (a) Return of 6 and 6 respectively. ©) Infinite loop and abnormal termination respectively. (© Abnormal termination and infinite loop respectively. @ Both terminating abnormally. [2017 (Set- Marks} 1.124 Consider the following C program. Hinclude #include void printlength (char *s, char *) { unsigned int ¢ = 0; intlen = ((strlen(s)—strlen(®)> 9 strlen(); printf(‘%d\n", len); tle } void main () Scanned with CamScanner |, programming & Data Structures NN oO gat “abe oe ay el Sonat 9% nae stron is defined in string gg gectll ON yalue of type size_t, which is an fe ne The output of the program is wos oa [2017 (Get-1) : 2 Marks} ager te following 0 functions: consi re fant Gnt ™) ee = 0) return} ) \peoutpt printed when funt (6) is called is 5999120233445) 69423120112288 jp soamizn132495 () 99425120213049 [2017 (Set-1) : 2 Marks] site the following: List-I (P)static char var; (m= malloo(10); m = NULL; W char *ptr[10]; 9 reister int var; List Sequence of memory locations to store addresses @ Avariable located in data section of memory ore to allocate a CPU register to store a bi list memory which cannot be freed op. ° 9> 6), R> @, 8 > Gi ppc 8>O,R> Gv), § > Gi) “Gi, Q (iv), R Gil), S> @) PGi, 9G Gi, S>@) 1 Q— fiv), R (i), S— Gi) [2017 (Get-2) : 1 Markl 1.127 Consider the following function implemented in C: void printxy (int x, int y) { int *ptr; y= tpt *ptr = 1; printf ("%d, %d", x, 9); } ‘The output of invoking printxy (1, 1) is () 0, 0 0,1 @10 @iL1 [2017 (Set-2) : 1 Mark] 1.128 Consider the following C program. # include # include int main( ) {chart char* p printf('%d", (int) strlen (ct2lp] ~ 6[Pl-1)) GATECSIT2017"; return 0; } ‘The output of the program is [2017 Get-2) : 2 Marks] 1.129 Consider the following C program # include int main() { int m = 10; int n, nl; n= +m; nl = mt; Aes — nl; n-=nl; printf(‘%d", n); return 0; } ‘The output of the program is : [2017 (Set-2) : 2 Marks] 1.130 Consider the following snippet of a C program. ‘Assume that swap (&x, &y) exchanges the contents of x and y. Scanned with CamScanner int main() ( 14 63% int arrayl] = © int done = 0: int while (do ( done = 1 for sf (arrayli] < 77" ig, gare ye == 0) <4 it) e+) { +i swap(arrayl done = 0; } ) } for (= 5022 HED st exraytd > arrayli 1) swap(&arrayl, garrayli- 1} I printfved”, arrayl3)s I ‘The output of the program is [2017 (Set2) : 2 Marks} 11131 Consider the following C code: #inelude int *assignval (int *, int val) { void main() {int *2 = malloe (size of (nt); if (NULL = 2) return; x= assignval(, 0} iff) { = (int*) malloc (size of (int)); if (NULL ==) return; x= assignval (t, 10); } print? (*%6d\n", *); free (x); GATE Previous Years Solved Papers: Me bi antes ‘The code suffers from which one of, ly, “4 tm problems: (a) compiler error as the return of typecast appropriately (6) compiler error because the compa be made as x == NULL and nop 95 4 © compiles successfully but execu in result in dangling pointer hay (@ compiles successfully but execu, result in memory leak ” hy [2017 Bett) :14, Moca 1.132 Consider the C program fragment below yy, jg meant to divide x by ¥ using repens subtractions. The variables x, y,q anda ited unsigned int. Teal while (>=) { r=r-% qzath } Which ofthe following conditions onthe vars ,qand rbefore the execution ofthe frags ‘will ensure that the loop terminates in sty satisfying the condition x == (7X q +2 @@=7 8&6 =) © &> 0) && (r= 2) &E YD) © @= 0) && (= 2) BEY >D) @ @=0 &&O>O [2017 (Set-2) : 2 Marks 1.133 Consider the following C program: #include struct ournode { char x, 52 i int main () { struct ournode p = { struct ournode *q = &p; (1,0, a +2 printi(‘%e, %e", *(char*) a+ D> *(char*) q+ 2); return 0; } ‘The output ofthis program is: @ O¢ (Oat? © O,at2 0, 1] @ 2018: Mass) Scanned with CamScanner programming & Data Structures oe 7 pe following C program: tea, 8) return (6% € XC); } ' 4 asin 04 inl 80 pent 6 counter } . : sutput of this program is : ‘he 11 Mark} corthe following program written in pseudo- [2011 Cons 10h. Assume that x and y are integers: count 9) Df if yt print ("*"); Count (x/2, 9); } else { yey Count (1024, 9); if( ) ! Thenumber of times that the print statement is aaecuted by the call Count (1024, 1024) is [2018: 1 Marks] "WiOsider the following C program: Raclude ‘idfunl (char *e1, char *s2) { sl, char **52) { int main () { char *strl = “Hi", *ste2 = “Byo" funt (str1, str2); printi('%s%s", ste, str2); fun2 (str, &str2);print{C%s%e", str, ste2); return 0; 1 The output of the program above is (@) HiBye Bye Hi (b) Hi Bye Hi Bye © ByeHiHiBye @ Bye Hi Bye Hi {2018:2 Marks} 1.137 Consider the following C code. Assume that unsigned long int type length is 64 bits. ‘unsigned long int fun (unsigned long int n) { unsigned long int i, j= 0, sum = 0; for (i= nj i> 1 i= 2) js for (;j>1;j=,/2) sum++; return (sum); } ‘The value returned when we call fun with the input 2"%is @a4 (b) 5 6 @ 40 {2018:2 Marks} 1.138 Consider the following C program: #include int main () { int arr[] = {1, 2, 3, 4, 5,6, 7, 8, 9, 0,1, 2, 5}, “ip = arr + 4; printf (‘%d\n", ip{1])’ return 0; i ‘The number that will be displayed on execution of the program is 12019: 1 Mark] 1.139 Consider the following C program: #include int jumble(int x, int 9) { xe2*xty; return x; } int main () { intx=2,9=5; y= jumble(y, x); Scanned with CamScanner ae 324 | x=jumble(y, print{('%d\n".); return 0; } inted by the program is ———* The vale printed bythe oer ark] 1.140 Consider the following C function: void convert (int n) { if(n<0) printf(%d’, m)s else convert (n/2); printf(%d’, n % 2); } } Which one of the following will happen when the function convert i called with any positiveinteger nas argument? (@) It will print the bina in the reverse order and terminate. (©) Tewill print the binary representation of but will not terminate. (© It will not print anything and will not ry representation of 2 terminate, (@ It will print the binary representation of n and terminate. [2019:2 Marks] 1.141 Consider the following C programs: #include int main (){ float sum = 0.0, j= 1.0, 1= 2.0; sum= printf(‘%4f\n", sum); } return 0; } The number of times the variabl ; ‘€ sum wil printed, when the above program is cura . Lis (20192 Marks): 1.142 Consider the following © p, 1 #include am intr() { Static int num = 7; return num —; t int main() { for (r(); ¥(); r()) printf(%d",r()); return 0; } Which one ofthefllowing values ling, lang on execution of the programs? @ 41 (©) 630 © 63 @ 82 [2019:2 Mari) 1.148 Consider the following C program: #include int main () { int a] = {2, 4, 6, 8, 10}; int i, sum =0, *b=a +4; for (i= 0; 1<5; i++) sum = sum + (*b—i)—*(b-); printf (“%d\n", sum); return 0; } ‘The output of the above C program is. [2019:2Marks, Scanned with CamScanner Array, 21 In a compact single dimensional array representation for lower triangular matrices (i.e. all the clements above the diagonal are zero) of sizen xn, non-zero elements (i.e. elements of the lower triangle) of each row are stored one after another, starting from the first row, the index of the (i, j)" clement of the lower triangular matrix in this new representation is @ iti Witj-1 6 6-2 iv? [1994:2 Marks] 2.2 Let A be a two-dimensional array declared as, follows: A:array [1... 10] [1... 15] of integer; Assuming that each integer takes one memory location. The array is stored in row-major order and the first element of the array is stored at location 100, what is the address of the element ANGI? @) 15i+j+84 (b) 15) +i +84 © 1Wi+j+39 @ 10) +i+89 [1998 : 2 Marks] 2.3 Ann x narray vis defined as follows: vli, jJ =i-j for alli,j, 1sisn,1sj 0. O;j= 1; while G S — @) ali] -ali <8 - * 8 © ali]-afl bli] => ef2i] 2 afi] 1 afl > bfi] => cl2i] > bli] 1 8 > oil => ell s afi o ail > bfil => cl2i} < bli] _ghofthe following is TRUE Line Tand Il) only Tana ry . gay and HT @) only TT ang ty é [2005 : 2 Marks} consider the following C function in which gaeisthe number of elements in the array Myton *B unsigned int size) { int Y= 0; ‘Ssuming Consider Y) Y=; } retum Y; } Thevalue returned by the function MyX is the ® maximum possible sum of elements in any sub-array of array E. maximum element in any sub-array of array E, Sum of the maximum elements in all possible Sub-arrays of array E. © ltesum ofall the elements in the array E. [2014 (Set-1) : 2 Marks] 4 . WAbea square matrix of size n xn. Consider ‘tilowing pseudocode. What is the expected "yee 241 212 output (Af (i); ‘The matrix A itself ‘Transpose of the matrix A Adding 100 to the u and subtractin; pper diagonal elements i 100 from k elements of A a @ None of the above [2014 (Set-3) :1 Mark] What is the output of the following © code? Assume that the address of xis 2000 (in decimal) and an integer requires four bytes of memory. int main() { unsigned int x{4][3] = {{1, 2, 3}, (4, 5, 6}, (7, 8, 9}, (10, 11, 12)}; Printf(‘%u,%u, %u", x3, *(c+3),*(x+2)43); } (a) 2036, 2036, 2036 (b) 2012, 4, 2204 (© 2036, 10, 10 @ 2012, 4,6 [2015 (Set-1) :2 Marks] Consider the following two C code segments. Y and Xare one and two dimensional arrays of size n and n x n respectively, where 2 < n < 10. Assume that in both code segments, elements of Yare initialized to 0 and each element XIU] of array Xisinitialized toi +). Further assume that when stored in main memory all elements of X are in same main memory page frame. Code segment 1: 11 initialize elements of Y to 0 J initialize elements X{i][j] of X to i+j for @ > \90 “if > 100 then fun=x-10°° °° ; lee fun = fundun (e#iD) S775 “Ends yo dor ag as iri? 0 va OP) 168 @ 2 [1998:2Marks) of size n2 1, Starting with the ye we push the first n natural ‘and then perform n pop —~ Push and Pop operation ‘and Y seconds elapse ion and 39 LetSbeastack empty stack, Suppo numbers in sequence, operations. Assume that take X seconds each, petween the end of one such stack operat 1enextoperation. For m2 1, define is the time elapsed from the start of the pop operation §. The average stack life of the start of th the stack-life of m end of push(m) to the that removes m from; anelement of this stack is @ n&+¥) @) BY+2X @ak+Y-X — @Y+2K [2003 :2 Marks] Scanned wih camecanner 354 | GATE Previous Years Solved Papers: | wi Eg : 310 A single array A[luMANSIZB] is used to order onto n stack, but it may implement two stacks. The two stacks grow from charneterat any time, Which once hey apposite ends of the array. Vavinblestopl and 4, strings CANNOT be genera eta? top2 (top! < top2) point to the location of the 8. program? 4 Using ic topmostelement ineachofthostacks.Ifthespnce f(a) abed ©) dba ‘ is tobe used efficiently, the condition for“stack —¢/—(@)_ chad, @ caba fallis (of eet oO 4 ¢ 2004 , (@) (top1 =MAXSIZE2) and (top2=MAXSIZE2 9 1 ati idefined on stacks eng +1) ©) top1 + top2= MAXSIZE (© (top1 = MAXSIZED)or (top2=MAXSIZE) c {> the following properties, £@)=0ana. Tha = max (((S), 0) + i for all stac ksS andintag Ifa stack S contains the integers a tegers 2,5 gt @ top1=top2—1 inorder fh , (e004 Mra] a ler from bottom to top, what is gy et 6 w) 4 7 3.11 The best data structure to check whether an © 3 @2 arithmetic expression has balanced parentheses a (2005 ; 1 Mary ao oma 316 An implementation of « queue @, usin oe om stacks S1 and S2, is given below sd Boe aie void insert(Q, x) : ( 3.12 A circularly linked list is used to represent a push(S1,x); Queue. A single variable pis used to access the ) Queue. 'To which node should p point such that void delete(Q, x) both the operations enQueue and deQueue ean ( a : : ' performed inconstant time? if(Gtack-empty($2) then if(tack-empty(S1)) then ( print(‘Qis empty’); return; } ) front node i © notpossible witha single pointer ~ = (ataccempyi) @ node next to front > x= pop(S2) = pop(S1); [2004:2 Marks} push(S2, x); 318 Assume that the operators +, -, x are left } associative and “is right associative. The order x= pop(S2); of precedence (from highest to lowest) is 6, x, +, ) i ~. The postfix expression corresponding to the Let n insert and m (< n) delete operates infix expression a+bxe-d“efis performed in an arbitrary order on a” e## ‘abe x + def 4 *2 are Mace A queue Q. Let x and y be the number efi @ abexsdente 4 Cm JN pop operations performed respective’ } ae © abtexd—erfs Ody C7 oar Nn p.xrosstns. Which ne ofthe florns @-+axberAdef qa yhe — ea i m and n? “Bood:2Markgy yh- @) n+msxs2nand2msysnt™ &) n+msx<2nand2msy $2 3.14 A program attempts to generate as many © 2msx<2nand2msysnt™ permutations as possible of the string, ‘abed’ by (@ Qmsx<2nand2msyS20 cd pushing the characters a, b, c,d in the same 2006: d Scanned with CamScanner programming & Data Structures rng postfix expression with single digit i nevaluated using a stack g28°/23" +51*— at *istbe exponentiation operator, The st elomentsof the stack after the first ig te got © 5, Oy @1 (2007: 2 Marks) oe you are given an implementation of a yf Sm fintegers. The operations that can be peforeton the queue are: jsfimpty (Q) : returns true if the queue is ‘my, false otherwise. «> galte (Q) deletes the element at the front ofthe queue and returns its value, pincer QI: ofthe queue, consider the following function: wid (queue Q ( = int i if ( is Empty (Q)) { i= delete (Q); £Q; insert (Q, i); ) inserts the integer iat the rear ) What operation is performed by the above function? (@ Leaves the queue Q unchanged ©) Reverses the order of the elements in the queue Q (© Deletes the element at the front of the queue Qand inserts it at the rear keeping the sther elements in the same order {9 Empties the queue Q 7" [2007 : 2 Marks} ‘ippose a circular queue of capacity (n — yD ‘enents is implemented with an array of B "tents, Assume that the insertion and deletion ne are carried out using REAR and Tan #8array indeed variables, respectively: tilly, REAR = FRONT =0. The conditions to ‘queue full and queue empty are 3.20 B21 (@) full: (REAR +1) mod n empty :REAR == FRONT. (©) full: (REAR +1) mod empty : (FRONT + 1) mod © full: REAR== FRONT empty : (REAR +1) mod n. (@ full: (FRONT + 1) mod empty : REAR == FRONT {2012:2 Marks} Consider the following operation along with Enqueue and Dequeue operations on queues; where kisa global parameter. Multi-Dequeue (@) { m=k; while (Q is not empty) and (m> 0) { Dequeue (Q); m=m-1; y } ‘What is the worst case time complexity of a sequence of n queue operations on an initially empty queue? @ e@) ©) m+) © Ok) @ O@? [2013 :2 Marks] ‘Suppose’a stack implementation supports an instruction REVERSE, which reverses the order ofclementson the stack, in addition to the PUSH and POP instructions. Which one of the following, statements is'TRUE with respect to this modified stack? (@ Aqueue cannot be implemented using this stack. () A queue can be implemented where ENQUEUE takes a single instruction and DEQUEUE takes a sequence of two instructions. (© A queue can be implemented where ENQUEUE takes a sequence of three instructions and DEQUEUE takes a single instruction, (@ A queue can be implemented where both ENQUEUE and DEQUEUE take a single instruction each. [2014 (Set-2) : 2 Marks} Scanned with CamScanner 356 | 8.22 Consider tho C program below: # include int *A, stk To} int stkFune (int opcode, int val) { static int size=0, stkTop = 0; switeh (opcode) t case ~I: size = val; break; case 0: if(stkTop next = =NULL)}| ((pdata<=pnextdata)&& f(pnext))); } Fora given linked list p, the function freturns 1 ifand only if (a) the list is empty or has exactly one element (b) the elements in the list are sorted in non- decreasing order of data value (© the elements in the list are sorted in non- increasing order of data value (@ not all elements in the list have the same data value [2003 : 2 Marks] 45 46 47 Let Pea singly linked list. Let be, toan intermediate node xin the lst yr" worst-case time complexity of the algorithm to delete the node x from (@) O(n) (b) O(log? ny © O(log n) @ 0g [2004 ; May The following C function takes a singly list of integers as a parameter and rea! the elements of the list. The list isrepresn : as pointer toa structure, The function seg with the listeontaining the integers 1,2 1 6,7 inthe given order. What will bethe ert, of the ist after the function completesexentinn struct node {int value; struct node *nert void rearrange (struct node *list) Pring iy est hn, he heligg { struct node *p, * int temp; if (list [list — next) return; p= list; 4 = list > next; while (q) { temp = p — value; p> value = q — value; 4 value = temp; p=q-— next; a= p2p— next : 0; } } @ 12,345,670 2143657 © 1,325,4,7,6@ 234565) [2005 : 2 Marks! yale The following C function takes a sing! list of integers as a parameter and reat le the elements of the list. The function =™ with the list containing the integers 4, 5, 6, 7 in the given order. What will tes contents ofthe list after the function oo"? execution? yrranges A Scanned with CamScanner oh ors srefollowing i roving the a mate = 4 Values = temp; p= a~> next; 1554567 02143657 9,547.6 @ 2,3, 4,5, 6,7, 1 [2008: 2 Marks} C function takes a singly-linked argument. It modified the list by last element to the front of the list the modified list. Some part of the st as input nd returns codeis eft blank. typed struct node { int value; struct node *next; Node; Node *mode_to_front (Node *head) { Node*p, “a5 ifead == NULD)|(headmnext = e (p next != NULL) a= Pp; p=ponext; } ' return head; 49 alternative to replace the Choose the corree! blank line. (0) q= NUL (W) qonext (@) qonext= NULL Consider the C code fragment given below typedef struct node {int data; node* next; } node; void join (node* m, node* 1) { node* p = 1; while (p > next ! = { NULL) p=p next; } p> next =m; } “Assuming that m and 7 point to valid NULL- terminated linked lists, invocation of join will 0 apa ln on erent list n for all inputs. & either cause a null pointer dereference or append list m to the end of list n- (© cause a null pointer dereference for all inputs. oan = list m for all inputs. [2017 (Set-1) : 1 Mark] Scanned with CamScanner Scanned with CamScanner Trees sible to’ construct a binary tree uniquely 0 re-order and post-order traversals are oie trueFalse) ” [1987: 1 Mark] pe nuimber ofTeaves ina tree is not a power of 2 ii the treeis nota binary tree.(True/False) ; [1987:1 Mark] sinoinay 0° in figure is traversed in inorder, 8 sponte order in which the nodes willbe visited is_— @Q oj g O® @ [1991 : 2 Marks] uy Abinay treo Thas n leaf nodes, The number of nodes of degree 2 inTis (@) log,” @) n-1 @ 2 @n [1995 : 1 Mark] 45 Inthe balanced binary tree in the figure given telow, how many nodes will become unbalanced mihen a node is inserted as a child of the node " @ ~ © © @® ® er @s [1996:1 Mark] ry ea ‘the following sequences denotes the post ler traversal sequence of the tree of above ‘Westion? (9) fegbdba (&) gebdafe © wea @ fedgeba [1996: 1 Mark] 5.7 Abinary search tree is generated by inserting % 58 59 5.10 5.11 cnder of the following integers: 60, 15, 62 8.20, 58, 91, 3, 8, 37, 60, 24. The number of nodes in the left subtree and right subtree of the TOO respectively is @ 47) © (14) © @3) @ 38) [1996:2 Marks] Abinary search tree is used to locate the number 413, Which of the following probe sequences 7° possible and which are not? Explain. (@) 615214174043 @) 2 3 50 40 60 43 (© 106531483743 @ 81615214 4143 (© 17772766 18 43 [1996 :2 Marks] “Abinary search tree is generated by inserting in Seder the following integers: 50, 18, 62, 5, 20, 58, 91, 8, 8, 37, 60, 24. The number of nodes in the left gubtree and right subtree of the root respectively is @ 40 0.4 © 63 @ 6.8 [1996 :2 Marks] [Abinary search tree contains the values 1, 2, 3, 4, 5, 6, 7, 8. The tree is traversed in pre-order and the values are printed out. Which of the following sequences is a valid output? (@) 53124786 (&) 53126487 (© 58241678 @ None of these [1997: 2 Marks] Which of the following statements is false? (@ Atree with n nodes has (n— 1) edges (b) Alabeled rooted binary tree can be uniquely constructed given its postorder and preorder traversal results. (© Acomplete binary tree with n internal nodes has (n+ 1) leaves (@ The maximum number of nodes in a binary tree of height h is (2**!— 1) [1998: 1 Mark] Scanned with CamScanner 5.12 _Acomplete n-ary tree is one in which every node has 0 or n sons, If x is the number of internal nodes of a complete n-ary tree, the number of Jeaves in itis given by @ x(n) 41 @) xn-1 © m+. @ x(n+1) [1998: 2 Marks] 5.13 Consider the following nested representation of binary trees: (XY Z) indicates ¥ and Z are the eft and right subtrees, respectively, of node X. Note that Y and Z may be NULL, or further nested, Which of the following represents a valid Dinary tree? @ (124567) (b) (1 ((234) 56)7) (© (@34) 667) @ (@3NULL) (45)) [2000:1 Mark] 5.14 Let LASTPOST, LASTIN and LASTPRE denote the last vertex visited in a postorder, inorder and Preorder traversal. Respectively, of a complete binary tree. Which of the followingis always true? (@) LASTIN=LASTPOsT () LASTIN=LASTPRE (© LASTPRE=LASTPOST @ Noneof the above [2000 : 2 Marks} 5.15 The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is @) n/2 &) @-1/2 © (n-1)2 @ [@n+1y31 [2002 :2 Marks} 5.16 In a heap with n elements with the smallest element at the root, the 7 smallest element can be found in time @ O@logn) &) O(n) © O(ogn) @ ea) (2003: 1 Mark] 5.17 Let T(n) be the number of different binary search trees on n distinct elements, ‘Then T(n) = x ‘T(k-1) T(x), where xis it (@ n-k+1 @)n-k © n-k-1 @ n-k-2 [2003 : 1 Mark] GATE Previous Years Solved Papers . & ~ | 58, 5.19 5.20 21 Suppose the numbers 7, 5 are inserted in that ordering,’ 6,0, : init, binary search tree. The binaye ath. ty theusual ordering on natunt neat “ the inorder traversal tree? mmeneot a @) 7510324689 > ©) 0243165987 © 0123456789 @ 9864230157 12003514, Addata structure is required for sori, “ty integers such thateachotthefolomye Fy can be done in O(log n) time, where y hi number of elements in the set. “e 1. Deletion of thesmaest lemons 2 Insertion of an element it inno, presentiin the set, amy Which of the following data struct ‘used for this purpose? : (@) Aheap can be used but notabal search tree () A balanced binary search t butnot a heap lance, tree can be usd © Both balanced binary search tresanthay can be used @ Neither balanced binary search teeny can be used {20032 ‘The following numbers are insertedintosnex#t binary search tree in the given order: 10,145 15, 12, 16. Whatis the height of the binary tree (the height is the maximum distanced leaf node from the root)? @2 ©) 3 O4 @6 2004: 131041 Level order traversal ofa rooted tree ante by starting from the root and performing (@) preorder traversal (© inorder traversal (© depth first search @ breadth first search (20 pg sti Scanned with CamScanner { i programming & Data Structures wre by one in the given order into a ‘he resultant maxHeap is eye 5.24 5.25 @ a @ 6 B® ® [2004 :2 Marks] «Ons the following C program segment: sérct CellNode ( struct CellNode *leftChild ; intelement; struct CelINode *right Child ; k int DoSomething (struct CellNode *ptr) t intvalue=0; it(pte!= NULL) { if(pte— leftChild ! = NULL) value= 1+ DoSomething (ptr — leftChild); if(ptr rightChild != NULL) value= max (value, 1+DoSomething (tr rightChild)); 5.26 5.27 Mea value); Thevah ‘en Mts A luereturned by the function DoSomething Pointer to the root of a non-empty tree is ‘sargument is for nmmanan aw sponts 92, 15; 20, 30, 12, 25, 16 are | 367 (@) The number of leaf nodes in the tree () The number of nodes in the tree (© The number of internal nodes in the tree (@ The height of the tree Marks} [200 Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree, Which of these pairs identify a tree uniquely? L._ preorder and postorder 2. inorder and postorder 8. preorder and inorder 4, levelorder and postorder (@) Lonly ( 2ands © Bonly @ 4only [2004:2 Marks] rees has its Which one of the following binary 8 BCAD and inorder and preorder traversals a5 ABCD, respectively? A A B @ 5 w we D A A R B © @ D e [200 a nare inserted in a binary wrder. In the resulting tree, contains p nodes. ‘The numbers 1, 2, search tree in some 0} the right subtree of the root ‘The first number to be inserted in the tree must be @p @ pth © a-P @ n-p+l {2005 : 1 Mark] Ina binary tree, for every node the difference between the number of nodes in the left and right subtrees is at most 2. Ifthe height of the tree is h> 0, then the minimum number of nodes in the tree is @ a? @ we © #- @) [2005 : 2 Marks] Scanned with CamScanner GATE Previous Years Solved Papers : — ! MaDe ) Ss, 5.83, Inabinary maxhesp containing y inary search tree contains the numbers 1, 2, eae smallest clement can be found in pe im 3, 4, 6, 6, 7, 8. When the tree is traversed in e values in ench node printed out, (@) 0m) ©) 8 Gog n omens Ines obtained is 5, 8,1, 2.456, (© og log n) oa” 8, 7. If the treo is traversed in postorder, the 1206 sequence obtained would be 5.4 Inabinarytree, the numberofinton @ 876,548.21 degree 1i85,and the numbers, 65 ofddegree 2is 10. The number oflegre lng © 214 85 binary tree ia tei 2.1, 4,3, 7, 8 6,5 a (2005: 2 Mark) 1 @u 5.29. Postorder traversal of a given binary search tree, (2006 T produces the following sequence of keys 535. Which of the following sequenes, 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29 mete 28 fa Which one of the following sequences of keys (@) (23, 17, 14, 6, 13, 10, 1, 19,7. 5 Le eee O WANE nee tree 57 13} (@) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95 a a m a : a it 2 58,3 ©) 8, 10, 15, 22, 40, 60, 60, 95, 23, 25, 27, 29 ee a (©) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95 (2006 : 2 Margy @ 95, 50, 60, 40, 27, 23, 22, 25, 10, 0, 15, 29 5.36 Suppose that we have numbers betmeen tng [2005 : 2 Marks] 100 in a binary search tree and want y for the number 56. Which of the fons 5.30 A priority-Queueis implemented as a Max-Heap. sequences CANNOT beth sequen fa Initially, it has 5 elements. The level-order ae examined? traversal of the heap is given below: @ (10, 75, 64, 43, 60, 57, 35) 10, 8, 5, 3, 2 (®) (90, 12, 68, 34, 62, 45, 5: ‘Two new elements ‘1’ and‘7’are inserted in the © 19, 85, 47, 68, 43, 57, 53} heap in that order. The level-order traversal of @ {79, 14, 72, 56, 16, 53, 53} the heap after the insertion of the elements is [2006 : 2 Marky (@) 10, 8, 7,5, 3,2,1 ©) 10, 8,7, 2, 3, 1,5 © 10,8, 7,1, 2,3,5 @ 10, 8, 7,3, 2,1,5 Common Data for Q. 5.37 & Q.5.38: A3-ary max heap islike a binary max heap, butinsteahi 2 children, nodes have 3 children. A 3-ary heapcaa® represented by an array as follows: The root i stors!? the first location, a [0], nodes in the next eve, frome 5.31 How many distinct binary search trees can be right, is stored froma [1] to [3).'The nodes from these! 12005 :2 Marks] created out of 4 distinct keys? level of the tree from left to right are stored fon! @) 5 ou Jocation onward. An item x can be inserted inant ou (@ 35 heap containing n items by placing xin the lost [2005:2Marks} and, pushing it up the tree to satisfy the heap prom" sa sequet™ 5.82 In a complete k-ary, every internal node has 6.37 Which one ofthe following isa valid) exactly k children. The number of leaves in such elements in an array representing 0" a tree with n internal nodes is Beant | @ nk © @-HkH @ 1,3,5,6,89 ©6315) (© nk-1) +1 @ n&-1) © 9,3,6,8,5,1 @9568 onan [2005 :2 Marks} 7 Scanned with CamScanner the elements 7,2, 10, and 4 arwinserted, # ast Sper, nto the valid 3-ary max heap found question. Which one of the following in jqtnesbovese inte uence ofitems in the array representing jathes i Meeattant heap? 12006 :2 Marks} tion 5.39 to 5.41: integers is interpreted as a leRChild = = NULL) && (ptr > rightChild == NULL) Value = 1s else value= value + GetValue (ptrvleftChild) + GetValue(ptr— rightChild); } return (value); ) ‘The value returned by Get Value when a pointer to the root of a binary tree is passed as its argument is Scanned with CamScanner r I {@) the number of nodes a the number: ‘of internal nodes in the tree | (© the number of leaf nodes in the tree Hi (@ the height of the tree [2007:2 Marks} 1e key value 60 in a binary 5.48 When searching for th 3 10, ! earch tree, nodes containing the key value: 30, 40,50, 7080, 90 are traversed, not necessarily Hi in the order given. How many different orders in which these key values can occur are possible i 10 the node | on the search path from the root t containing the value 60? @) 35 ) 6 © 128 @ 5040 [2007 : 2 Marks] 5.49 Which of the following is TRUE? (a) The cost of searching an AVL tree is O(log n) but that of a binary search tree is O(n) | (b) The cost of searching an AVL tree is (log n) but that ofa complete binary tree is O(n log n) (©) The cost of searching a binary search tree is O (log n) but that of an AVL tree is @(n) @ Thecost of searching an AVL tree is (n log n) but that of a binary search tree is O(n) [2008 : 1 Mark] Directions for Question 5.50 to 5.52: A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of keys. IL 81, 537, 102, 439, 285, 376, 305 IL 52, 97, 121, 195, 242, 381, 472 IIL. 142, 248, 520, 386, 345, 270, 307 IV. 550, 149, 507, 395, 463, 402, 270 5.50 Suppose the BST has been unsuccessfully searched for key 273. Which all of the above sequences list nodes in the order in which we could have encountered them in the search? (@ and I only ) Tand I only (© Wand IV only @ Uonly [2008 : 2 Marks} 5.51 Which of the following statements is TRUE? (@) I, [and IV are inorder sequences of three different BSTs GATE Previous Years Solved Papers: (ay () Tisa preorder sequence of Q I Hee, 439 as the root some Ry (9 isan inorder sequence of, 121 is the root and 52 ig 1 a postorder sequence of, 149 as the root ‘4 eat ome Pans: May, @ 5.52 How many distinet BSTScan be eonsn, Sdistinct keys? eg @4 ©6 & 5 @9 [2008 : 2 Mary Linked Questions 5.53 & 5.54: : Abinary tre with n> I nodes hasan, and of degree one, two and three respectively, The —_ anode is defined as the number ofits neighbouy, 5.53 n,can be expressed as: @ n,+n,-1 — ) n,-2 © (a+r) @ n,-1 [2008 : 2 Marty 5.54 Starting with the above tree, while there remiss a node v of degree two in the tree, add ani between the twonneighbours of vand then exe vfrom the tree. How many edges will remain at the end oft process? @ 2*n,-3 (@) n,+2*n,-2 © ny-n, @ ny +n,-2 [2008 : 2 Mark 5.55 You are given the postorder traversal, Pat binary search tree on the n elements 1,2. You have to determine the unique binerys"* tree that has Pas its postorder travers Wb# is the time complexity of the most ee algorithm for doing this? (@) ©(ogn) (b) O(n) © O(nlogn) (@ none of the above, as the tre uniquely determined. cannot {2008:2 Marks) Scanned with CamScanner os | Programming & Data Structures é avoabinary heap on n elements and wish ’ wet eamore elements (Not necessarily ng inher) into this heap. The total ne teed for this is ee ©) em 006”) ere) = @ Ot ® [2008 Marks] « following three are known to be the Ter, inorder and postorder sequences of, etree Butitsnot known which s which, L ‘MBCAFHPYK yp KAMOBYPFH yp MABCKYEPH the true statement from the following. (g Land Tlaropreorder and inorder sequences, respectively J and III are preorder and postorder sequences, respectively {9 Tlsthe inorder sequence, but nothing more can be said about the other two sequences (g land IIT are the preorder and inorder sequences, respectively [2008 : 2 Marks} 498 Whstis the maximum height of any AVL-tree vith Tnodes? Assume that the height of a tree vitha single node is 0. @2 3 @4 @5 [2009: 2 Marks} lisked Answer Question 5.59 and 5.60: (sider a binary max-heap implemented using an my 15) Which one of the following array represents a binary max-heap? @ (25, 12, 16, 13, 10, 8, 14} ©) (25,14, 13, 16, 10, 8, 12) © (25,14, 16, 13, 10, 8, 12} @ (5, 14, 12, 13, 10, 8, 16} [2009:2 Marks) 'S Vatisthe content of the array after twodelete ‘erations on the correct answer to the previous ‘Westion? 4 ‘04,13, 12, 10,8) ¢) (14, 12, 13, 8, 10} 9 (14,13,8, 12,10). @ {14, 13, 12, 8, 10) [2009 : 2 Marks] 5.62, 5.63 | 371 ina heap where the value of each Parent is grenter than thevale oft than or equal tothe value ofits Children, Which of the following is a mnache (ia) @ o PR » \ © O@ f Do ® iO} @ Aas OOOO COOOL {2011:1 Mark} Weare given a set ofn distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set 80 that it becomes a binary search tree? @o 1 © nt @ PCat) [2011:2Marks} ‘The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudocode below is invoked as the height (coot) to compute the height of a binary tree rooted at the tree pointer root. intheight(treeptrn) { ‘ffn==NULL)return —1; ‘f(a left==NULL) ifn right=NULLyreturnd; else return[ Bt |y//Box1 else {h1=height(n— left); iff right=NULL) return(1+h1); else {h2=height(n— right); return[ B2 }/Box2 } } } ‘The appropriate expressions for the two boxes BL and B2are Scanned with CamScanner (@) Bi: (1 +height (a righ) B2: (1+max(hl, h2)) () Bi: (height(n > right) Ba: (1+ max(hl, h2)) (© Buheight(n > right) 1B2: max(hl, h2) (@ B1:(1 +height(n > right) 1B2: max(hl, h2)) (2012: 2 Marks] i | 5.64 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? (@) 10, 20, 15, 23, 25, 35, 42, 39, 30 @) 15, 10, 25, 23, 20, 42, 35, 39, 30 © 15, 20, 10, 23, 25, 42, 96, 39, 30 @ 15, 10, 23, 25, 20, 35, 42, 39, 30 [2013:2 Marks] 5.65 Consider a rooted n node binary tree represented using pointers. The best upper bound on the time required to determine the number of subtrees having exactly 4 nodes is O(n*log’n). Then the value of a + 108 is. Mark] [2014 (Set-1) 5.66 A priority queue is implemented asa Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is: 10, 8, 5, 3, 2. Two new elements 1 and 7 are inserted into the heap in that order. The level-order traversal of the heap after the insertion of the elementsiis: @) 10,8, 7,3,2,1,5 © 10,8,7,2,3,1,5 © 10,8,7,1,2,3,5 @ 10,8,7,5,3,2,1 [2014 (Set-2) : 1 Mark] 5.67 Consider the following rooted tree with the vertex labeled Pas the root: ® @ {®) ®® O® @ GATE Previous Years Solved Paper 5.68 Lmao ts, ‘The order in which the nodes are y anin-order traversal ofthe tree jy ty (@) SQPTRWUV ™ @ sQPTUWRV (© SQPTWUVR @ SQPTRUWV 12014 et) any Consider the pseudocode given jy, function DoSomething( takes as aug pointer to the rootof an arbitrary troej et hy the leftMost Child rightSibingremey Bach node of the trees type treeNogs typedef struct treeNode* treeptr; " struct treeNode { treeptr leftMostChild, rightSibling h int DoSomething (treeptr tree) { int value=0; if (tree != NULL) { if (ree>leftMostChild =NULL) value= 1; else value= DoSomethingttree> JeftMostChild, valuevaluetDoSomething(tree> right } return(value); } When the pointer to the root of a tree is pas as the argument to DoSomething, the s* returned by the function corresponds totb? (@) number of internal nodes in the te () height of the tree. © number of nodes without a right the tree. (® number of leaf nodesin the tree- [2014 (Set-3): siblinsi® amas! Scanned with CamScanner J v the Cfunction given below, Assume that cool st contains 0) elements sorted Fee ngonder. ie oarray Gin USCA, int, int n) is geids 20; jer { | Progamming & Data Structures Re (dE st (r= TistLKD) jekeli it GistAlR] <=) iz k+l; )while <=: st(istLE] ==) return(t)s ae return-1; trish oneofthe following statements about the function. ProcessArray is CORRECT? (9 lwillruninto an infinite loop when xis not in lista. {9 Itisan implementation ofbinary search (@ ltwill always find the maximum element in lista @ It will return -1 even when x is present in lista. [2014 (Set-3):2 Marks} 1 Which of the following is/are correct inorder traversal sequence(s) of binary search tree(s)? 1. 3,5, 7,8, 15, 19, 25 2 5,8,9, 12, 10, 15, 25 4 2,7,10,8,14, 16, 20 4 4,6,7,9, 18, 20, 25 @ land4only —(@) Zand only © 2and4only —@)_ 2only [2015 (Set-1) :1 Mark] a Tebohtote tree is the length of the longest eulleat path init, The maximum and um number of nodes in a binary tree of tht are © Ghand6, respectively S4and5, respectively a 373 (© 32and6, respectively @ 31and6, respectively [2015 (Set-1): 1 Mark] 5.72, Consider a max heap, represented by the array: 40, 80, 20, 10, 16, 16, 17, 8, 4. ‘Array index [1 [2 [3 [4 [6 [6 [7 [4|9 Value 40 [30 [20 [10 [15 [16 [17 [8] 4 Now consider that a value 5 is inserted into this heap. After insertion, the new heap is @) 40, 30, 20, 10, 15, 16, 17, 8, 4,95 (©) 40, 35, 20, 10, 30, 16, 17, 8, 4, 15 © 40, 30, 20, 10, 35, 16, 17, 8, 4, 15 @ 40, 35, 20, 10, 15, 16, 17, 8,4, 30 [2015 (Get-1):2 Marks] 5.73 A binary treo ’ has 20 leaves. The number of nodes in T having two children is [2015 (Set-2): 1 Mark] 5.74. While inserting the elements 71, 65, 84, 69,67, 83in an empty binary search tree (BST) in the sequence shown, the element in the lowest level @) 65 ) 67 © @ 8 [2015 (Set-3) : 1 Mark] 5.75 Consider the following array of elements (89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100). ‘The minimum number of interchanges needed to convert it into a max-heap is @ 4 ©) 5 @2 @3 [2015 (Set-3):1 Mark] 5:16 Consider the following directed graph: @—O. @ (D @—O ‘The number of different topological ordering of the vertices of the graph is__. [2016 (Set-1):1 Mark] 5.77 An operator delete (f) for a binary heap data structure is to be designed to delete the item in the i-th node, Assume that the heap is implemented in an array and i refers to the i-th Scanned win CamScanner i. 374 | GATE Previous Years Solved Papers: mio, index of the array. If the heap tree has depth d 5.81 The pre-order traversal Of a bina, (number of edges on the path from the root to the is given by 12, 8, 6, 2, 7, 9, 19 16 ear farthest leaf), then what is the time complexity 20. Then the post-order traversal gee?" '8 1p tore-fix the heap efficiently after the removal of @ 2, 6, 7, 8, 8, 10, 12, 15, 16 yy ttt, the element? ©) 2,7, 6, 10, 9, 815,17, a9, ig ® @ oa) © 7, 2,6, 8,9, 10, 20, 17, 19, yg 8 ©) O(@ butnot O@) © 76,210, 8,8 15,16, 37 ge © O@%)butnotO(@) (2017 Get) 97/2 @ (424) but not Od) re 2 Mary [2016 (Set-1):2Marks) 58% The postorder traversal ota binary ng 6,7, 4, 5, 2,8, 1. The inorder travers 8 5.78 Acomplete binary min-heap is made by including same treeis 8,6,9, 4,7, 2,5, 1,3 Tek, ot ty cach integer in [1, 1023] exactly once. The depth a treeis the length ofthe longest pats of a node in the heap is the length of the path root to any leaf. The height of the yon from the root of the heap to that node. Thus, the above is . ary ty root is at depth 0. The maximum depth at which (eos, integer 9can appear is__. a ea = (2016 Gor2):2Marksy 58° Lat Thea full binary tree with Slaves iq binary tree has every level fll) Suppe nt 5.79 The number of ways in which the numbers 1, 2, leaves a and b of T are chosen unfomly 3, 4, 5, 6, 7 can be inserted in an empty binary independently at random. The expected vl search tree, such that the resulting tree has the distance between a and b in T (e, ty height 6, is__. Note: The height of a number of edges in the unique path betweens tree with a single node is 0. and 0) is (rounded off to 2 decimal plas | [2016 (Set-2):2 Marks] - | 5.80. Let Tbea binary search tree with 15 nodes. The an om minimum and maximum possible heights of T are: Note: The height of a tree with a single node is 0. (@) 4 and 15 respectively (®) Band 14 respectively © 4 and 14 respectively @ Band 15 respectively [2017 (Set-1): 1 Mark] 4 Scanned with CamScanner a ast | Programming & Data Structures we | | 375 | Trees hi @ 55 (b) 56 (ce) 5.7 (b) 59 (b) 5.10 @ 511 (b) 512 (@) 5.13 CC) i () 515 @ 5.16 (©) 5.17 (b) 5.18 (©) 5.19 (b) 5.20 (b) 6.21 (2) 5.22 (a) gn @ 524 (b) 6.25 (2) 5.26 (c) 527 (b) 528 (@) 5.29 (a) 5.30 @) 5.31 (b) ie (9 533 (a) 5.34 (b) 5.35 (©) 5.36 (©) 5.37 @) 5.38 (a) 5.39 (a) 5.40 (b) a (9) 542 ©) 5.43 (b) 544 (a) 545 (©) 5.46 (b) 547 (c) 5.48 (a) 5.49 (a) 50 (@) 5.51 (c) 5.52 (b) 5.53 (b) 5.54 (a) 5.55 (c) 5.56 (b) 5.57 (A) 5.58 (b) 559() 5.60 (@) 5.61 (b) 5.62 (b) 5.63 (a) 5.64 (d) 5.66 (a) 5.67 (a) 5.68 (A) 889 (b) 5.70 (a) 5.71 (a) 5.72 (b) 5.74 (b) 5.75 @) 5.77 (b) 5.80 (b) 5.81 (b) Scanned with CamScanner

You might also like