0% found this document useful (0 votes)
23 views

Disha Publication Computer Past Questions Programming - Data Structures. CB512871741

The document discusses various programming concepts and problems related to data structures, specifically in the C programming language. It includes code snippets, questions, and multiple-choice answers related to linked lists, function calls, memory management, and algorithm complexity. The content appears to be sourced from an educational book, focusing on programming and data structure exercises.

Uploaded by

imanmol92452
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Disha Publication Computer Past Questions Programming - Data Structures. CB512871741

The document discusses various programming concepts and problems related to data structures, specifically in the C programming language. It includes code snippets, questions, and multiple-choice answers related to linked lists, function calls, memory management, and algorithm complexity. The content appears to be sourced from an educational book, focusing on programming and data structure exercises.

Uploaded by

imanmol92452
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

57

Programming & Data


Structures

This Chapter “Programming & Data Structures” is taken from our Book:

ISBN : 9789386629043
58

PROGRAMMING &
DATA STRUCTURE
3
Programming in C if (NULL == x) return;
x = assignval (x, 10) ;
1. Consider the C code fragment given below. }
[2017, Set 1, 1 Mark]
printf ( “%d\n” , *x);
typedef struct node {
int data ; free ( x ) ;
node* next; }
} node ; The code suffers from which one of the following problems:
void join (node* m, node* n){ (a) compiler error as the return of malloc is not typecast
node* p = n; appropriately
while (p – > next != NULL) {
p = p– > next ; (b) compiler error because the comparison should be
} made as x == NULL and not as shown
p– > next – m ; (c) compiles successfully but execution may result in
} dangling pointer
Assuming that m and n point to Valid NULL-terminated (d) compiles successfully but execution may result in
linked lists, invocation of join will memory leak
(a) append list m to the end of list n for all inputs. 4. Consider the following two functions.
(b) either cause a null pointer dereference or append list [2017, Set 1, 2 Marks]
m to the end of list n.
void fun1(int n) { void fun 2 (int n) {
(c) cause a null pointer dereference for all inputs.
if (n == 0) return; if (n == 0) return;
(d) append list n to the end of list m for all inputs.
2. Consider the following intermediate program in three printf (“%d”, n) ; printf (“%d”, n) ;
address code [2017, Set 1, 1 Mark] fun2(n - 2); funl ( ++n);
P= a – b printf (“%d”, n); printf (“%d”, n);
q= P*c } }
P= u * v The output printed when funl (5) is called is
q=p+q (a) 53423122233445 (b) 53423120112233
Which one of the following corresponds to a static single (c) 53423122132435 (d) 53423120213243
assignment form of the above code? 5. Consider the C functions foo and bar given below:
(a) p1 = a – b (b) p3 = a – b [2017, Set 1, 2 Marks]
q1 = p1 * c q4 = p3 * c int foo (int val) {
p1 = u * v p4 = u * v int x = 0;
q1 = p1 + q1 q5 = p4 + q4 while (val > 0) {
x = x + foo (val--);
(c) p1 = a – b (d) p1 = a – b }
q1 = p2 * c q1 = p * c return val;
p3 = u * v p2 = u * v
}
q2 = p4 + q3 q2 = P + q
int bar (int val) {
3. Consider the following C code: [2017, Set 1, 1 Mark]
#include < stdio.h > int x = 0;
int *assignval (int *x, int val) { while (val > 0) {
*x = val; x = x + bar (val–l);
return x; }
} return val;
void main () { }
int *x = malloc (sizeof (int) ) ; Invocations of foo (3) and bar (3) will result in:
if (NULL == x) return; (a) Return of 6 and 6 respectively.
x = assignval(x,0); (b) Infinite loop and abnormal termination respectively.
if (x) { (c) Abnormal termination and infinite loop respectively.
x = (int *) malloc (sizeof (int) ) ; (d) Both terminating abnormally.
59
6. Consider the following C program. The output of invoking printxy (1,1) is
[2017, Set 1, 2 Marks] (a) 0, 0 (b) 0, 1
#include <stdio.h> (c) 1, 0 (d) 1, 1
#include <string.h> 10. Consider the C program fragment below which is meant
void print length (char *s, char *t) { to divide x by y using repeated subtractions. The variables
unsigned int c = 0; x, y, q and r are all unsigned int. [2017, Set 2, 2 Marks]
int len = ((strlen (s) – strlen (t) ) > c) ? strlen (s) : strlen (t) ; while (r > = y) {
printf (“%d\n’’, len); r=r–y;
} q=q+I;
void main ( ) { }
char *x = “abc”; Which of the following conditions on the variables x, y,
char *y = “defgh”; q and r before the execution of the fragment will ensure
printlength (x,y) ; that the loop terminates in a state satisfying the condition
} x == (y * q + r)?
Recall that strlen is defined in string.h as returning a value (a) (q == r) && (r == 0)
of type size_t, which is an unsigned int. The output of the (b) (x > 0) && (r == x) && (y > 0)
program is ________. (c) (q == 0) && (r == x)&& (y > 0)
7. The output of executing the following C program is _______. (d) (q == 0) && {y > 0)
[2017, Set 1, 2 Marks] 11. Consider the following C function.
#include <stdio.h> [2017, Set 2, 2 Marks]
int total (int v) {
int. fun (int n) {
static int. count = 0;
int i, j ;
while (v) {
for (i = 1; i <= n; i++) {
count + = v&l ;
for (j = 1; j < n; j += i) {
v >>= 1;
printf{“ %d %d”, i, j};
}
}
return count;
}
}
}
void main() {
Time complexity of fun in terms of Q notation is
static int x = 0;
int. i = 5; (a) Q (n n ) (b) Q (n2)
for (; i > 0; i --) { (c) Q (n log n) (d) Q (n2 log n)
x = x + total (i); 12. Consider the following C Program.
} [2017, Set 2, 2 Marks]
printf (“%d\n”, x) ; #include<stdio.h>
} int. main ( ) {
8. Match the following: [2017, Set 2, 1 Mark] int m = 10 ;
(P) Static char var; (i) Sequence of memory int n, n1 ;
locations to store addresses n = ++m;
(Q) m = malloc (10); (ii) A variable located in data n1 = m++;
m = NULL; section of memory n – –;
(R) char * ptr [10]; (iii) Request to allocate a CPU – – n1;
register to store data n – = n1;
(S) register int var 1; (iv) A lost memory which cannot printf (“%d”, n) ;
be freed return 0;
(a) P ® (ii), Q ® (iv), R ® (i), S ® (iii) }
(b) P ® (ii), Q ® (i), R ® (iv), S ® (iii) The output of the program is _________.
(c) P ® (ii), Q ® (iv), R ® (iii), S ® (i) 13. Consider the following C Program.
(d) P ® (iii), Q ® (iv), R ® (i), S ® (ii) [2017, Set 2, 2 Marks]
9. Consider the following function implemented in C: #include<stdio.h>
[2017, Set 2, 1 Mark] #include<string.h>
void printxy (int x, int y) { int main ( ) {
int *ptr; char* c = “GATECSIT2017” ;
x = 0; char* p = c ;
ptr = &x; printf (“%d”, (int) strlen (c+2[p]–6[p]–1));
y = *ptr; return 0 ;
*ptr = 1; }
printf (“%d, %d”, x,y); The output of the program is ________.
}
60
14. Consider the following C program: [2016, Set 1, 1 Mark] 18. What will be the output of the following pseudo-code when
void f (int, short); parameters are passed by reference and dynamic scoping is
void main () assumed? [2016, Set 1, 2 Marks]
{ int i = 100; a = 3;
short s = 12; void n(x) {x = x * a; print(x);}
short *p = &s; void m(y) {a = 1; a = y – a; n(a); print(a);}
__________ ; // call to f () void main( ) {m(a);}
} (a) 6, 2 (b) 6, 6
Which one of the following expressions, when placed in the (c) 4, 2 (d) 4, 4
blank above, will NOT result in a type checking error? 19. The attributes of three arithmetic operators in some
(a) f (s,*s) (b) i = f (i, s) programming language are given below.
(c) f (i,*s) (d) f (i,*p) Operator Precedence Associativity Parity
15. Consider the following C program: [2016, Set 1, 1 Mark] + High Left Binary
#include <stdio.h> – Medium Right Binary
void mystery (int *ptra, int *ptrb){ * Low Left Binary
int *temp; The value of the expression 2 – 5 + 1 – 7 * 3 in this language
temp = ptrb; is _________ . [2016, Set 1, 2 Marks]
ptrb = ptra; 20. The value printed by the following program is _______.
ptra = temp; [2016, Set 2, 1 Mark]
} void f(int* p, int m){
int main() { m = m + 5;
int a=2016, b=0, c=4, d=42; *p = *p+m;
mystery (&a, &b); return;
if (a < c) }
mystery (&c, &a); void main () {
mystery (&a, &d); int i=5, j=10;
printf (“%d\n”, a); f(& i, j);
} printf (“%d”, i+j);
The output of the program is_______. }
16. The following function computes the maximum value 21. The following function computes XY for positive integers
contained in an integer array p[ ] of size n (n >= 1). X and Y. [2016, Set 2, 2 Marks]
[2016, Set 1, 2 Marks] int exp (int X, int Y) {
int max (int *p, int n){ int res = 1, a = X, b = Y;
int a = 0, b = n –1; while (b != 0) {
while (__________){ if (b%2 == 0) {a = a*a; b = b/2;}
if (p[a]<= p[b]){a = a + 1;} else {res = res*a; b = b – 1;}
else {b = b – 1;} }
} return res;
return p[a]; }
} Which one of the following conditions is TRUE before
The missing loop condition is every iteration of the loop?
(a) a != n (b) b != 0 (a) XY = ab
(c) b > (a+1) (d) b != a (b) (res*a)Y = (res*X)b
17. What will be the output of the following C program? (c) XY = res*ab
void count (int n){ [2016, Set 1, 2 Marks] (d) XY = (res*a)b
static int d=1; 22. Consider the following program: [2016, Set 2, 2 Marks]
int f(int *p, int n)
print f (“%d”, n); {
print f (“%d”, d); if(n <= 1) return 0;
d++; else return max (f (p+1, n–1), p[0]–p[1]);
if (n>1) count (n – 1); }
print f (“%d”, d); int main()
} {
void main( ){ int a[] = {3, 5, 2, 6, 4};
count(3); printf(“%d”, f(a, 5));
} }
(a) 3 1 2 2 1 3 4 4 4 (b) 3 1 2 1 1 1 2 2 2 Note: max(x, y) returns the maximum of x and y.
(c) 3 1 2 2 1 3 4 (d) 3 1 2 1 1 1 2 The value printed by this program is ________ .
61
23. The output of the following C program is ______.
void f1(int a, int b) [2015, Set 1, 1 Mark]
{ 1 p=q+r
s=p+q
int c;
u=s*v
c=a; a=b; b=c;
}
void f2(int *a, int *b)
{
2 v=r+u 3 q=s*u
int c;
c=*a; *a=*b; *b=c;
}
int main ( )
4 q=v+r
{
int a=4, b=5, c=6;
f1(a, b); The variables which are live at the statement in basic block
f2(&b, &c); 2 and at the statement in basic block 3 of the above control
printf(“%d”, c–a–b); flow graph are [2015, Set 1, 2 Marks]
} (a) p, s, u (b) r, s, u
24. Consider the following pseudo code, where x and y are (c) r, u (d) q, v
positive integers. 27. Consider the following C function.
begin int fun(int n)
q: = 0 {
r:=x int x = 1, k;
while r > y do if (n = = 1) return x;
being for (k =1; k<n; ++k)
r:=r–y x = x + fun(k) * fun(n–k);
q:=q+1 return x;
end
}
end
The return value of fun(5) is _____.[2015, Set 2, 1 Mark]
The post condition that needs to be satisfied after the
28. Consider the following function written in the C programming
program terminates is [2015, Set 1, 2 Marks]
(a) {r = qx + y Ù r < y} languages.
(b) {x = qy + r Ù r < y} void foo(char *a)
(c) {y = qx + r Ù 0 < r < y} {
(d) {q + 1 < r – y Ù y > 0} if(*a && *a != ‘ ’)
25. What is the output of the following C code? Assume that the {
address of x is 2000 (in decimal) and an integer requires four foo(a+1);
bytes of memory? putchar(*a);
int main( ) }
{ }
unsigened int x[4] [3] = {(1,2,3), {4,5,6}, {7, 8, 9}, {10, 11, The output of the above function on input "ABCD EFGH"
12}}; is [2015, Set 2, 1 Mark]
printf(“%u, %u, %u”, x+3, *(x+3), (a) ABCD EFGH (b) ABCD
*(x+2)+3); (c) HGFE DCBA (d) DCBA
} [2015, Set 1, 2 Marks]
29. Consider the C program below.
(a) 2036, 2036, 2036
#include <stdio.h>
(b) 2012, 4, 2204
int *A, stkTop;
(c) 2036, 10, 10
(d) 2012, 4, 6 int stkFunc(int opcode, int val)
26. A variable x is said to be live at a statement Si in a program {
if the following three conditions hold simultaneously: static int size = 0, stkTop = 0;
I. There exists a statement Sj that uses x switch (opcode)
II. There is a path from S i to S j in the flow graph {
corresponding to the program case –1: size = val; break;
III. The path has no intervening assignment to x including case 0: if(stkTop < size) A[stkTop++]=
at Si and Sj val; break;
62
default: if (stkTop) return A[– –stkTop]; {
} char sl[7] = “1234”, *p;
return – 1; p = sl + 2;
} *p = ‘0’;
int main( ) printf (“%s”, s1);
{ }
int B[20]; A = B; stkTop = –1; What will be printed by the program?
stkFunc(–1, 10); [2015, Set 3, 1 Mark]
stkFunc(0, 5); (a) 12 (b) 120400
stkFunc(0, 10); (c) 1204 (d) 1034
printf("%d\n", stkFunc(1,0)+stkFun(1,0);
32. Consider the following C program:
}
# include<stdio.h>
The value printed by the above program is ______.
int main ( )
[2015, Set 2, 2 Marks]
30. Suppose you are provided with the following function {
declaration in the C programming language. int i, j, k = 0;
int partition(int a[ ], int n); j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
The function treats the first element of a[ ] as s pivot, and k –= – –j;
rearranges the array so that all elements less than or equal for (i = 0; i < 5: i++)
to the pivot is in the left part of the array, and all elements {
greater than the pivot is in the right part. In addition, it moves switch (i + k)
the pivot so that the pivot is the last element of the left part. {
The return value is the number of elements in the left part. case 1:
The following partially given function in the C programming
case 2: printf (“\ n%d”, i+k);
language is used to find the kth smallest element in an array
case 3: printf (“\n%d”, i+k);
a[ ] of size n using the partition function. We assume k £ n.
default: printf (“\n%d”, i+k);
int kth_smallest(int a[ ], int n, int k)
{ }
int left_end = partition(a, n); }
if (left_end+1 = =k) return 0;
{ }
Return a[left_end]; The number of time printf statement is executed is ______.
} [2015, Set 3, 2 Marks]
if(left_end+1) > k) 33. Consider the following C program.
{ #include<stdio.h>
return kth_smallest(________); int f1(void);
}
int f2(void);
else
{ int f3(void);
return kth_smallest(________); int x = 10;
} int main ( )
} {
The missing argument lists are respectively
int x = 1;
[2015, Set 2, 2 Marks]
(a) (a, left_end, k) and (a+left_end+1, n–left_end–1, x + = f1 ( ) + f2 ( ) + f3( ) + f2( );
k–left_end–1) pirntf (“%d”, x);
(b) (a, left_end, k) and (a, n–left_end–1, k–left_end–1) retirm 0;
(c) (a+left_end+1,n–left_end–1, k–left_end–1) and (a, }
left_end, k)
(d) (a, n–left_end–1, k–left_end–1) and (a, left_end, k) int f1 ( ) { int x = 25; x++; return x;}
31. Consider the following C program segment. int f2 ( ) { static int x = 50; x++; return x;}
#include <stdio.h> int f3 ( ) {x *= 10; return x};
int main( ) The output of the program is ____. [2015, Set 3, 2 Marks]
63
34. Consider the following recursive C function. return 0;
void get (int n) }
{ else return 0;
if (n<1) return; }
get (n–1); Which one of the following is TRUE?
get (n–3); [2014, Set-2, 2 Marks]
printf(“%d”, n); (a) The function returns 0 for all values of j.
} (b) The function prints the string something for all values
If get (6) function is being called in main ( ) then how many of j.
times will the get () function be invoked before returning to (c) The function returns 0 when j = 50.
the main ( )? [2015, Set 3, 2 Marks] (d) The function will exhaust the runtime stack or run
(a) 15 (b) 25
(c) 35 (d) 45 into an infinite loop when j = 50.
35. Consider the following program in C language : 39. Consider the function func shown below:
#include <stdio.h> int func(int num) {
main () int count = 0;
{ while (num) {
int i; count++;
int *pi = &i;
num>>= 1;
scanf ("%d", pi);
}
printf ("%d\n", i + 5);
return (count);
}
Which one of the following statements is TRUE ? }
[2014, Set-1, 1 mark] The value returned by func (435) is __________.
(a) Compilation fails [2014, Set-2, 1 Mark]
(b) Execution results in a run-time error. 40. Consider the following function
(c) On execution, the value printed is 5 more than the double f(double x){
address of variable i. if( abs(x*x – 3) < 0.01) return x;
(d) On execution, the value printed is 5 more than the else return f(x/2 + 1.5/x);
}
integer value entered.
Give a value q (to 2 decimals) such that f(q) will return
36. Consider the following pseudo code. What is the total q:_____. [2014, Set-2, 2 Marks]
number of multiplications to be performed? 41. The minimum number of arithmetic operations required to
[2014, Set-1, 2 Marks] evaluate the polynomial P(X) = X5 + 4X3 + 6X + 5 for a given
D=2 value of X, using only one temporary variable is _____.
for i = 1 to n do
[2014, Set-3, 1 Mark]
for j = i to n do
42. Consider the following function:
for k = j + 1 to n do
int unknown (int n) {
D = D*3
(a) Half of the product of the 3 consecutive integers. int i, j, k=0;
(b) One-third of the product of the 3 consecutive integers. for (i=n/2; i<=n; i++)
(c) One-sixth of the product of the 3 consecutive integers. for (j=2; j<=n; j=j*2)
(d) None of the above. k = k + n/2;
37. Suppose n and p are unsigned int variables in a C program. return (k);
We wish to set p to nC3. If n is large, which one of the following }
statements is most likely to set p correctly? The return value of the function is [2013, 2 Marks]
[2014, Set-2, 1 Mark] (a) Q(n2) (b) Q(n2 log n)
3
(a) p = n*(n–1)*(n–2) / 6; (c) Q(n ) (d) Q(n3 log n)
(b) p = n*(n–1) / 2*(n–2) / 3; 43. What is the return value of f(p,p), if the value of p
(c) p = n*(n–1) / 3*(n–2) / 2; is initialized to 5 before the call? Note that the first
(d) p = n * (n-1) * (n–2) / 6.0; parameter is passed by reference, whereas the second
38. Consider the C function given below. parameter is passed by value. [2013, 2 Marks]
int f(int j) int f (int &x, int c) {
{ c = c – 1;
static int i = 50; if (c==0) return 1;
int k; x = x + 1;
if (i == j) return f(x,c) * x;
{ }
printf(“something”); (a) 3024 (b) 6561
k = f(i); (c) 55440 (d) 161051
64
44. Consider the program given below, in a block-structured (d)
pseudo-language with lexical scoping an nesting of Main
procedures permitted. A1
Program main;
Var ...... A2
Procedure A1;
A21
Var .....
Call A2; Prame A1
pointer Access
End A1 links
Procedure A2;
45. What will be the output of the following C program segment?
Var ...
Procedure A21; char inChar = ‘A’; [2012, 2 Marks]
Var.... switch (inChar) {
Call A1; case ‘A’ : prinf(“Choice A\n”);
End A21 case ‘B’ :
Call A21; case ‘C’ : printf (“Choice B’):
End A2; case ‘D’ :
Call A1; case ‘E’ :
End main. default : printf (“No Choice”);}
Consider the calling chain: (a) No choice
Main ® A1 ® A2 ® A21 ® A1 (b) Choice A
The correct set of activation records along with their access (c) Choice A choice B No choice
links is given by [2012, 2 Marks] (d) Program gives no output as it is erroneous
(a) 46. What does the following program print? [2011, 2 Marks]
Main
#include < stdio.h >
A1 void f (int *p, int *q)
p = q;
A2
*p = 2;
A21 }
Prame int i = 0, j = 1;
A1
pointer Access int main () {
links f(&i, &j);
(b) printf(“%d%d/n”, i, j);
Main }
A1 (a) 2 2 (b) 2 1
(c) 0 1 (d) 0 2
A2 47. What does the following fragment of C-program print?
A21 char c[ ] = “GATE 2011” [2011, 1 mark]
char * p = c;
Prame A1
pointer Access printf (“%s”, p + p[3] – p[1];
links (a) GATE 2011 (b) E 2011
(c) (c) 2011 (d) 011
Main Common Data for Questions 48 and 49.
Prame A1 Consider the following recursive C function that takes two
pointer arguments unsigned into foo (unsigned int, n, unsigned int r) {
A2 if n > 0 return n% foo (n/r, r);
Access
A21 links else return 0,
}
48. What is the return value of the function foo, when it is called
as foo (513, 2)? [2011, 2 Marks]
(a) 9 (b) 8
(c) 5 (d) 2
49. What is the return value of the function foo, when it is called
as foo (345, 10)? [2011, 2 Marks]
(a) 345 (b) 12
(c) 5 (d) 3
65
50. The following program is to be tested for statement coverage. 53. Consider the program below: [2009, 1 Mark]
begin [2010, 2 Marks] #include < stdio.h >
if (a = = b) {S1; exit;} int fun (int n, int * f_p){
else, if (c = = d) {S2;} int t, f;
if (n < = 1) {
else {S3; exit;}
*f_p = 1
S4;
return 1;
end }
The test cases T1, T2, T3 and T4 given below are expressed t = fun (n – 1, *f_p);
in terms of the properties satisfied by the values of variables f = t + *f_p;
a, b, c and d. The exact values are not given. *f_p = t;
T1: a, b, c and d are all equal return f;
T2: a, b, c and d are all distinct }
T3: a = b and c! = d int main () {
T4: a! = b and c = d int x = 15;
Which of the test suites given below ensures coverage of printf (“% d\n”, fun (5, & x));
statements S1, S2, S3 and S4? return 0;
(a) T1, T2, T3 (b) T2, T4 }
(c) T3, T4 (d) T1, T2, T4 The value printed is
(a) 6 (b) 8
51. The program below uses six temporary variables a, b, c, d, e,
(c) 14 (d) 15
f. [2010, 2 Marks]
54. Which combination of the integer variables x, y and z makes
a= 1 the variable a get the value 4 in the following expression?
b = 10 a = (x > y)? ((x > z)? x : z) : ((y > z)? y : z) [2008, 1 Mark]
c = 20 (a) x = 3, y = 4, z = 2
(b) x = 6, y = 5, z = 3
d=a+b
(c) x = 6, y = 3, z = 5
e=c+d (d) x = 5, y = 4, z = 5
f= c+ e 55. What is printed by the following C program?
b=c+e [2008, 2 Marks]
int f (int x, int * py, int ** ppz)
e=b+f
{
d=5+e int y, z;
return d + f **ppz + = 1; z = *ppz;
Assuming that all operations take their operands from *py + = 2; y = *py;
registers, what is the minimum number of registers needed x + = 3;
to execute this program without spilling? return x + y + z;
}
(a) 2 (b) 3 void main ()
(c) 4 (d) 6 {
52. What is the value printed by the following C program? int c, *b, **a,
#include < stdio.h> [2010, 2 Marks] c = 4; b & c; a = & b
int f (int *a, int n) printf(“%d”, f(c, b, a));
{ }
if (n < = 0) return 0; (a) 18 (b) 19
else if (*a % 2 = = 0) return *a + f(a + 1, n – 1); (c) 21 (d) 22
else return *a – f(a + 1, n – 1); 56. Choose the correct option to fill ? 1 and ?2 so that the program
below prints an input string in reverse order. Assume that
}
the input string is terminated by a newline character.
int main ()
void recerse (void){ [2008, 2 Marks]
{ int c;
int a [ ] = {12, 7, 13, 4, 11, 6}; if (?1) reverse ();
print f (“%d, f(a, 6)); ?2
return 0, }
} main ( ) {
(a) – 9 (b) 5 printf(“Enter Text”); printf(“/n”);
(c) 15 (d) 19 reverse ( ); printf(“/n”)
66
(a) ?1 is (getchar ( )! = ‘\n’)
?2 is getchar (c); void swap (int *px, int *py) {
(b) ?1 is (c = getchar ( ))! = ‘\n’) *px = *px – *py;
?2 is getchar (c);
(c) ?1 is (c! = ‘\n’) *py = *px + *py;
?2 is putchar (c); *px = *py – *px;
(d) ?1 is (c = getchar ( ))! = ‘\n’)
}
?2 is putchar (c);
57. Which of the following are true? [2008, 2 Marks]
S1 : will generate a compilation error
1. A programming language which does not permit global S2 : may generate a segmentation fault by runtime
variables of any kind and has no nesting of depending on the arguments passed
procedures/functions, but permits recursive can be S3 : correctly implements the swap procedure for all input
implemented with static storage allocation pointers referring to integers stored in memory
2. Multi-level access link (or display) arrangement is locations accessible to the process
needed to arrange activation records only, if the S4: implements the swap procedure correctly for some but
not all valid input pointers
programming language being implemented has nesting S5 : may add or subtract integers and pointers
of procedures/functions. (a) S1 only (b) S2 and S3
3. Recursion in programming languages cannot be (c) S2 and S4 (d) S2 and S5
implemented with dynamic storage allocation 61. Consider these two functions and two statements S1 and S2
4. Nesting of procedures/functions and recursion require about them. [2006, 2 Marks]
a dynamic heap allocation scheme and cannot be
implemented with a stack-based allocation scheme for int work1 (int*a, int i, int j)
{
activation records.
int x = a[i + 2];
5. Programming languages which permit a function to
a[j] = x + 1;
return a function as its result cannot be implemented
with a stack based storage allocation scheme for return a[i + 2] – 3;
activation records }
(a) 2 and 5 (b) 1, 3 and 4
(c) 1, 2 and 5 (d) 2, 3 and 5 int work2 {int *a, int i, int j)
58. Consider the following segment of C-code [2007, 1 Mark] {
int j, n; int t1 = i + 2
j = 1; int t2 = a[t1]
while (j < = n) a[j] = t2 + 1;
j = j*2; return t2 – 3;
The number of comparisons made in the execution of the }
loop for any n > 0 is
S1: The transformation form work1 to work2 is valid, i.e., for
(a) êé log 2 n ùú + 1 (b) n any program state and input arguments, work2 will compute
(c) éêlog 2 n ùú (d) êëlog 2 n úû + 1 the same output and have the same effect on program state
as work 1.
59. Consider the following C function: [2007, 2 Marks]
int f (int n) S2: All the transformations applied to work1 to get work2
{static int r = 0; will always improve the performance (i.e., reduce CPU time)
if (n < = 0) return 1; of work2 compared to work1
if (n > 3) (a) S1 is false and S2 is false
{r = n; (b) S1 is false and S2 is true
return f (n – 2) + 2; (c) S1 is true and S2 is false
} (d) S1 is true and S2 is true
return f(n – 1) + r; 62. Consider the following code written in a pass-by-
} reference language like FORTRAN and these statements
What is the value of f(5)? about the code
(a) 5 (b) 7 Subroutine swap (ix, iy)
(c) 9 (d) 18 it = ix
60. Consider this code to swap integers and these five L1 : ix = iy
statements:
L2 : iy = it
The code [2006, 2 Marks]
end
67
ia = 3 (a) P–2, Q–3, R–4, S–1 (b) P–4, Q–3, R–2, S–1
ib = 8 (c) P–3, Q–4, R–1, S–2 (d) P–3, Q–4, R–2, S–1
call swap (ia, ib + 5) 68. Consider the following C program segment:
print *, ia, ib [2005, 2 Marks]
end char p [20]
S1 : The compiler will generate code to allocate a char *s = “string”;
temporary nameless cell, initialize it to 13 and pass int length = strlen (s);
the address of the cell swap for (i = 0; i < length, i++)
S2 : On execution the code will generate a run time error p[i] = s [length – i];
on line L1 print f(“%s”, p);
S3 : On execution the code will generate a run time error The output of the program is
on line L2 (a) gnirts
S4 : The program will print 13 and 8 (b) garbage dashing or no value printed
S5 : The program will print 13 and –2 (c) gnirt
Exactly which of the following sets of statements is / are (d) no output is printed
correct? [2006, 2 Marks] 69. Consider the following C-program [2005, 2 Marks]
(a) S1 and S2 (b) S1 and S4 double foo (double); /* Line 1 */
(c) S3 (d) S1 and S5 int main () {
63. Which one of the following are essential features of an double da, db;
object-oriented programming language? [2005, 1 Mark] // input da
1. Abstraction and encapsulation db = foo (da);
2. Strictly - typedness }
3. Type-safe property coupled with sub-type rule double foo (double a) {
4. Polymorphism in the presence of inheritance return a;
(a) 1 and 2 (b) 1 and 4 The above code compiled without any error or warning. If
(c) 1, 2 and 4 (d) 1, 3 and 4 Line 1 is deleted, the above code will show
64. A common property of logic programming languages and (a) no compile warning or error
functional languages is [2005, 1 Mark] (b) some compiler-warnings not leading to unintended
(a) both are procedural languages results
(b) both are based on l-calculus (c) some compiler-warnings due to type-mismatch
(c) both are declarative eventually leading to unintended results
(d) both use Horn-clauses (d) compiler errors
65. An Abstract Data Type (ADT) is [2005, 1 Mark] 70. Consider the following program fragment for reversing
(a) same as an abstract class the digits in a given integer to obtain a new integer. Let
(b) a data type that cannot be instantiated n = d1d2 ... dm.
(c) a data type for which only the operations defined on it int n, rev;
can be used, but none else rev = 0;
(d) All of the above while (n > 0) {
66. What does the following C-statement declare? rev = rev * 10 + n% 10;
int (*f) int*); [2005, 1 Mark] n = n/ 10;
(a) A function that takes an integer pointer as argument }
and returns an integer The loop invariant condition at the end of the ith iteration
(b) A function that takes as argument and returns an is [2005, 2 Marks]
integer pointer (a) n = d1d2 ...dm – i and rev = dmdm–1 ...dm – j + 1
(c) A pointer to a function that takes an integer pointer as (b) n = dm – i + 1 ...dm – 1 dm or rev = dm – i...d2d1
argument and returns an integer (c) n ¹ rev
(d) A function that takes an integer pointer as argument (d) n = d1d2 ...dm or rev = dm ...d2d1
and returns a function pointer 71. Consider the following C program :
67. Match the following List I with List II and select the main ()
correct answer using the codes given below the lists. { int x, y, m, n;
scanf (“%d %d” , &x, &y);
List I List II / * Assume x > 0 and y > 0 */
P. Functional 1. Command-based, procedural m = x; n = y;
while (m! = n)
Q. Logic 2. Imperative, abstract data types { if (m > n)
R. Object-oriented 3. Side-effect free, declarative, m = m – n;
expression evaluation else
n = n – m;
S. Imperative 4. Declarative, clausal }
representation, theorem proving printf (“ % d”, n);
[2005, 2 Marks] }
68
The program computes [2005, 2 Marks] (a) Call swap (x, y)
(a) x + y using repeated subtraction (b) Call swap (& x, & y)
(b) x mod y using repeated subtraction (c) Swap (x, y) cannot be used as it does not return any
(c) the greatest common divisor of x and y value
(d) the least common multiple of x and y (d) Swap (x, y) cannot be used as the parameters are
72. What does the following algorithm approximate? passed by value
(Assume m > 1, Î > 0). [2005, 2 Marks] 77. The best data structure to check whether an arithmetic
x = m; expression has balanced parenthesis is a [2004, 1 Mark]
y = 1; (a) queue (b) stack
while (x– y > Î ) (c) tree (d) list
{ x = (x + y) / 2; 78. Consider the following C function :
y = m/x; float f (float x, int y) {
} float p, s; int i;
print (x); for (s = 1), p = 1, i = 1; i < y ; i ++){
(a) log m (b) m2 p* = x/i;
1 1 s + = p;
(c) m 2 (d) m 3 }
73. Consider the following C function: [2005, 2 Marks] return s;
int f (int n) }
{ static int i = 1; For large value of y, the return value of the function f best
if (n > = 5) return n; approximates [2003, 1 Mark]
n = n + 1; (a) x y (b) e x
i ++; (c) ln(1+ x) (d) x x
return f(n); 79. Which of the following statements is false?
} [2003, 1 mark]
The value returned by f (1) is (a) In statically types languages, each variable in
(a) 5 (b) 6 program has a fixed type
(c) 7 (d) 8 (b) In un-typed languages, values do not have any types
74. Consider the following C-program: [2005, 2 Marks] (c) In dynamically typed languages, variable have no
void foo (int n, int sum) { types
int k = 0, j = 0 (d) In all statically typed languages, each variable in
if (n = = 0) return; program is associated with value of only a single
k = n% 10, j = n/10; type during the execution of the program
sum = sum + k; 80. Which of the following is not an advantage of using
foo (j, sum); shared, dynamically linked libraries as opposed to using
printf (“%d”, k); statically linked libraries? [2003, 2 Marks]
} (a) Smaller sizes of executable files
int main () { (b) Lesser overall page fault rate in the system
int a = 2048, sum = 0; (c) Faster program startup
foo (a, sum); (d) Existing programs need not be relinked to take
printf (“%d\n”, sum); advantage of newer versions
What does the above program print? 81. Consider the following class definition in a hypothetical
(a) 8, 4, 0, 2, 14 (b) 8, 4, 0, 2, 0 object oriented language that supports inheritance and
(b) 2, 0, 4, 8, 14 (d) 2, 0, 4, 8, 0 uses dynamic binding. The language should not be
75. The goal of structured programming is to assuemed to be either Java or C++ , though the syntax
[2004, 1 Mark] is similar. [2003, 2 Marks]
(a) have well indented programmes Class P { Class Q subclass of P {
(b) be able to infer the flow of control from the compiled void f (int i) { void f (int i) {
code print (i); print (2*i);
(c) be able to infer the flow of control form the program }
text }
(d) avoid the use of GOTO statements Now, consider the following program fragment”
76. Consider the following C function : Px = new Q()
void swap (int a, int b)
Qy = new Q();
{int temp;
Pz = new Q();
temp = a;
X.f(1); ((P)y). f(1); z.f(1);
a = b;
Here, ((P)y) denotes a type cast of y to P. The output
b = temp;
produced by executing the above program fragment will
}
In order to exchange the values of two variables x and y. be
[2004, 1 Mark] (a) 1 2 1 (b) 2 1 1
(c) 2 1 2 (d) 2 2 2
69
Common Data for Questions 82 and 83 (d) recursion requires the activation record for the recursive
The following program fragment is written in a progrmming function to be saved on a different stack before the
language that allows global variable and does not allow nested recursive function can be called
declrations of functions. 87. What is printed by the print statements in the program P1
global int i = 100, j = 5; assuming call by reference parameter passing?
void P (x) { Program P1()
{
int i = 10;
x = 10;
print (x + 10);
y = 3;
i = 200; func1(y, x, x);
j = 20; print x;
print (x); print y;
} }
main () {P (i + j);} func 1 (x, y, z)
82. If the programming language uses static scoping and call {
by need parameter passing mechanism, the values printed y = y + 4;
z = x + y + z;
by the above program are [2003, 2 Marks]
} [2002, 2 Marks]
(a) 115, 220 (b) 25,220 (a) 10, 3 (b) 31, 3
(c) 25,15 (d) 115, 105 (c) 27, 7 (d) None of these
83. If the programme language uses dynamic scoping and 88. Consider the following three C functions
call by name parameter passing mechanism, the values [P1] int*g(void)
{
printed by the above program are [2003, 2 Marks]
intx = 10;
(a) 115, 220 (b) 25, 220 return (&x);
(c) 25, 15 (d) 115, 105 }
84. Consider the C program shown below : [P2] int*g(void)
# include < stdio. h > {
# define print(x) printf (“ %d”, x) int* px;
int x ; *px = 10;
void Q (int z) { return px;
z + = x; print(z); }
} [P3] int*g(void)
void P (int *y) { {
int x = *y + 2; int*px
Q (x); *y = x – 1; px = (int*) malloc (size of (int));
print (x);
*px = 10;
}
main (void) { return px;
x = 5; }
P (& x) Which of the above 3 functions are likely to cause
Print (x); problems with pointers? [2002, 2 Marks]
} (a) Only P3 (b) P1 and P3
The output of this program is [2003, 2 Marks] (c) P1and P2 (d) P1, P2 and P3
(a) 12 7 6 (b) 22 12 11 89. Consider the following program :
(c) 14 6 6 (d) 7 6 6 Program P2
85. The results returned by function under value – result and var n:int :
reference parameter passing conventions procedure W (var x:int)
[2002, 1 Mark] begin
(a) do not differ x = x + 1;
(b) differ in the presence of loops print x;
(c) differ in all cases end
(d) may differ in the presence of exception procedure D
86. In the C language [2002, 1 Mark] begin
(a) at most one activation record exists between the varn:int;
current activation record and the activation record n = 3;
for the main W (n);
(b) the number of activation records between the current End
activation record and the activation record for the begin || begin P2
main depends on the actual function calling sequence n = 10;
(c) the visibility of global variables depends on the D;
actual function calling sequence end
70
If the language has dynamic scopping and parameters are int i j;
passed by reference, what will be printed by the program? for (i = 0; i < = 4; i + +)
[2002, 2 Marks] j = incr (i);
(a) 10 (b) 11 }
(c) 3 (d) None of these is [2000, 2 Mark]
90. The process of assigning load addresses to the various (a) 10 (b) 4
parts of the program and adjusting the code and date in (c) 6 (d) 7
the program to reflect the assigned addresses is called
[2001, 1 Mark] Arrays
(a) assembly (b) parsing 97. Let A be an array of 31 numbers consisting of a sequence of
(c) relocation (d) symbol resolution
0’s followed by a sequence of l’s. The problem is to find the
91. The most approximate matching for the following pairs
X. m = malloc (5); m = NULL; 1. using dangling smallest index i such that A[i] is 1 by probing the minimum
pointers number of locations in A. The worst case number of probes
Y. free (n); n- > value = 5; 2. using uninitialized performed by an optimal algorithm is _____.
pointers [2017, Set 1, 2 Marks]
Z. char *p; *p = ‘a’; 3. lost memory 98. Consider the following snippet of a C program. Assume
is [2000, 1 Mark] that swap (&x, &y) exchanges the contents of x and y.
(a) X – 1, Y – 3, Z – 2 (b) X – 2, Y – 1, Z – 3 [2017, Set 2, 2 Marks]
(c) X – 3, Y – 2, Z – 1 (d) X – 3, Y – 1, Z – 2 int main( ) {
92. The most appropriate matching for the following pairs is int array [ ] = {3, 5, 1, 4, 6, 2};
X. depth first search 1. heap int done = 0;
Y. breadth-first search 2. queue int i ;
Z. sorting 3. stack while {done == 0) {
[2000, 1 Mark] done = 1;
(a) X – 1, Y – 2, Z – 3 (b) X – 3, Y – 1, Z – 2 for (i=0; i <=4 ; i + +) {
(c) X – 3, Y – 2, Z – 1 (d) X – 2, Y – 3, Z – 1 if (array[i] < array[i+1]) {
93. Aliasing in the context of programming languages refers swap (&array [i] , &array [i+1]);
to [2000, 1 Mark] done = 0;
(a) multiple variables having the same memory location }
(b) multiple variables having the same value }
(c) multiple variables having the same identifier for (i = 5; i>=l ; i – –) {
(d) multiple uses of the same variables if (array [i] > array[i – 1]) {
94. Consider the following C declaration swap (&array [i] , &array [i – 1]);
struct { done = 0 ;
short s [ 5 ] }
union { }
float y ; }
long z ;
printf (“%d”, array[3] ) ;
} u;
}
} t;
The output of the program is _______.
Assume that objects of the type short, float and long
99. A Young tableau is a 2D array of integers increasing from left
occupy 2 byte, 4 byte and 8 byte, respectively. The
memory requirement for variable t, ignoring alignment to right and from top to bottom. Any unfilled entries are
considerations, is [2000, 1 Mark] marked with ¥, and hence there cannot be any entry to the
(a) 22 byte (b) 14 byte right of, or below a ¥. The following Young tableau consists
(c) 18 byte (d) 10 byte of unique entries.
95. The number of tokens in the following C statements print
1 2 5 14
f(“i = % d, & i = % x”, i & i) is [2000, 2 Marks]
(a) 3 (b) 26 3 4 6 23
(c) 10 (d) 21 10 12 18 25
96. The value of j at the end of the execution of the following 31 ¥ ¥ ¥
C program int incr (int i)
{ When an element is removed from a Young tableau, other
static int count = 0; elements should be moved into its place so that the resulting
count = count + i; table is still a Young tableau (unfilled entries may be filled in
return (count); with a ¥). The minimum number of entries (other than 1) to
} be shifted, to remove 1 from the given Young tableau is
main () {
________. [2015, Set 2, 2 Marks]
71
(a) Only S2 is correct
100. Suppose C = c [ 0 ] ,...., c [ k - ] is an array of length k, where
(b) Only S3 is correct
all the entries are from the set {0, 1}. For any positive integers (c) Only S1 and S2 are correct
a and n, consider the following pseudocode.
(d) Only S1 and S3 are correct
DOSOMETHING (c, a, n)
103. Consider the following C function in which size is the
z ¬1
number of elements in the array E:
for i ¬ 0 to k – 1
int MyX(int*E, unsigned int size)
do z ¬ z2 mod n
{
if c[i] = 1
int Y = 0;
then z ¬ (z × a) mod n
int Z;
rutrun z
int i, j, k;
If k = 4, c = 1, 0, 1, 1 , a = 2 and n = 8, then the output of for (i = 0; i < size; i++)
DOSOMETHING (c, a, n) is _________. Y = Y + E[i];
[2015, Set 3, 2 Marks] for (i = 0; i < size; i++)
101. Consider the following C program. for (j = i; j < size; j++)
#include<stdio.h> {
int main( ) Z = 0;
{ for (k = i; k <= j; k++)
static int a[ ] = {10, 20, 30, 40, 50}; Z = Z + E[k];
static int *p[ ] = {a, a+3, a+4, a+1, a+2}; if (Z > Y)
int **ptr = p; Y = Z;
ptr++; }
printf(“%d%d”, ptr-p, **ptr); return Y;
} }
The output of the program is ______. The value returned by the function MyX is the
[2015, Set 3, 2 Marks] [2014, Set-1, 2 Marks]
102. Consider the following two C code segments. Y and X are (a) maximum possible sum of elements in any sub-array
one and two dimensional arr ays of size n and n × n of array E.
respectively, where 2 £ n £ 10. Assume that in both code (b) maximum element in any sub-array of array E.
segments, elements of Y are initialized to 0 and each element (c) sum of the maximum elements in all possible sub-arrays
X[i] [j] of array X is initialized to i+j. Further assume that when of array E.
stored in main memory all elements of X are in same main (d) the sum of all the elements in the array E.
memory page frame. 104. Let A be a square matrix of size n × n. Consider the following
Code segment 1: pseudocode. What is the expected output ?
//initialize elements of Y to 0 [2014, Set-3, 1 Mark]
//initialize elements X[i] [j] of X to i+j c = 100;
for (i = 0; i < n; i++) for i = 1 to n do
Y[i] += X[0] [i]; for j = 1 to n do
Code segment 2: {
//initialize elements of Y to 0 Temp = A [i] [j] + C;
//initialize elements X[i] [j] of X to i+j A [i] [j] = A [j] [i];
for (i = 0; i < n; i++) A [j] [i] = Temp – C;
Y[i] += X[i] [0]; }
Which of the following statements is/are correct? for i = 1 to n do
S1: Final contents of array Y will be same in both code for j = 1 to n do
segments output (A [i] [j]);
S2: Elements of array X accessed inside the for loop shown (a) The matrix A itself
in code segment 1 are contiguous in main memory (b) Transpose of the matrix A
S3: Elements of array X accessed inside the for loop shown (c) Adding 100 to the upper diagonal elements and
in code segment 2 are contiguous in main memory subtracting 100 from lower diagonal elements of A
(d) None of the above
[2015, Set 3, 2 Marks]
72
Common Data for Questions 105 and 106: 5. k = (i + j) / 2
The procedure given below is required to find and replace 6. if (Y[k] < x) i = k; else j = k;
certain characters inside an input character string supplied in 7. } while ((Y[k]! = X) & &(i < j));
array A. The characters to be replaced are supplied in array 8. if (Y[k]) = = x) print f (“x is in the array”);
oldc, while their respective replacement characters are supplied 9. else print f (“x is not in the array”);
in array newc. Array A has a fixed length of five characters, 10. }
while arrays oldc and newc contain three characters each. 109. On which of the following contents of Y and x does the
However, the procedure is flawed. program fail? [2008, 2 Marks]
void find_and_replace (char *A, char *oldc, char *newc) (a) Y is [1 2 3 4 5 6 7 8 9 10 ] and x < 10
{ (b) Y is [1 3 5 7 9 11 13 15 17 19] and x < 1
for (int i=0; i<5; i++) (c) Y is [2 2 2 2 2 2 2 2 2 2] and x > 2
(d) Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and
for (int j=0; j<3; j++)
x is even
if (A[i]==oldc[j]) A[i] = newc[j];
110. The correction needed in the program to make it work
} properly is [2008, 2 Marks]
The procedure is tested with the following four test cases. (a) change line 6 to ; if (Y [k] < x) i = k + 1;
1. oldc = “abc”, newc = “da b” else j = k – 1;
2. oldc = “cde”, newc = “bcd” (b) change line 6 to ; if (Y [k] < x) i = k – 1;
3. oldc = “bca”, newc = “cda” else j = k + 1;
4. oldc = “abc”, newc = “bac” (c) change line 6 to ; if (Y [k] < = x) i = k; else j = k;
105. The tester now tests the program on all input strings of (d) change line 7 to ; }while ((Y [k] = = x) & &(i < j))
length five consisting of characters ‘a’, ‘b’, ‘c’, 111. An array of n numbers is given, where n is an even number.
‘d’ and ‘e’ with duplicates allowed. If the tester The maximum as well as the minimum of these n numbers
carries out this testing with the four test cases given needs to be determined. Which of the following is true about
above, how many test cases will be able to capture the the number of comparisons needed?[2007, 2 Marks]
flaw? (a) At least 2n – c comparisons, for some constant c, are
[2013, 2 Marks] needed
(a) Only 1 (b) Only 2
(b) At most 1.5n – 2 comparisons are needed
(c) Only 3 (d) All four
(c) At least nlog2 n comparisons are needed
106. If array A is made to hold the string “abcde”, which of
(d) None of the above
the above four test cases will be successful in exposing
the flaw in this procedure? [2013, 2 Marks] 112. A set X can be represented by an array x [n] as follows
(a) 2 only (b) 4 only ì1 , if i Î X
(c) 3 and 4 only (d) None x[i] í
î 0 , otherwise
Statements for Linked Answer Questions 107 and 108
The subset - sum problem is defined as follows : Given a set Consider the following algorithm in which x, y and z are
of n positive integers, S = {a1, a2, a3, ...an}, and positive Boolean arrays of size n :
integer W, is there a subset of S whose elements sum to W? algorithm zzz (x[], y[], z []) {
A dynamic program for solving this problem uses a 2- int i;
dimensional Boolean array, X, with n rows and W + 1 for (i = 0; i < n; + + i )
columns. X[i, j], 1 £ i £ n,0 £ j £ W, is true if and only if there z [i] = (x[i] ^ ~ y [i] Ú (~ x [i] ^ y [i])
is a subset of {a1, a2, ....ai} whose elements sum to j }
107. Which of the following is valid for 2 £ i £ n and The set Z computed by the algorithm is
ai £ j £ W? [2008, 2 Marks] [2006, 2 Marks]
(a) X [i, j] = X [i – 1, j] Ú X[i, j – ai] (a) ( X ÈY ) (b) ( X ÇY )
(b) X [i, j] = X [i – 1, j] Ú X[i – 1, j – ai] (c) (X – Y) Ç (Y – X) (d) (X – Y) È (Y – X)
(c) X [i, j] = X [i – 1, j] Ù X[i, j – ai]
113. Two matrices M1 and M2 are to be stored in arrays A and
(d) X [i, j] = X [i – 1, j] Ù X[i – 1, j – ai]
B respectively. Each array can be stored either in row–
108. Which entry of the array X, if true, implies that there is
a subset whose elements sum to W? [2008, 2 Marks] major or column–major order in contiguous memory
(a) X[1, W] (b) X[n, 0] locations. The time complexity of an algorithm to compute
(c) X[n, W] (d) X[n –1, n] M1 × M2 will be [2004, 2 Marks]
Statements for linked Answer Questions 109 and 110 (a) best if A is in row – major, and B is in column major
Consider the following C program that attempts to locate order
an element X in an array Y [] using binary search. The (b) best if both are in row – major order
program is erroneous. (c) best if both are in column – major order
1. f (int Y[10], int X) { (d) independent of the storge scheme
2. int u, j, k; 114. Consider the following C-function in which a[n] and b[m]
3. i = 0; j = 9; are two sorted integer arrays and c[n + m] be another array.
4. do { void xyz (int a [ ], int b [ ], int c [ ]) [2006, 2 Marks]
73
{ 119. Following declaration of a two-dimensional array in C.
int i, j, k; Char a [100] [100];
i = j = k = 0; Assuming that the main memory is byte addressable and
while ((i < n) && (j < m)) that the array is stored starting from memory address 0,
if (a [i] < b [j] c [k++] = a[i++]; the address of a [40] [50] is [2002, 2 Marks]
else c[k++] = b[j++]; (a) 4040 (b) 4050
} (c) 5040 (d) 5050
Which of the following conditions hold(s) after the 120. The following C declarations
termination of the while loop? struct node{
(i) j < m, k = n + j – 1 and a [n – 1] < b[j], if i = n int i :
(ii) i < n, k = m + i – 1 and b [m – 1] £ a[i], if j = m float j ;
(a) only (i) }
(b) only (ii) struct node * s [10];
(c) either (i) or (ii) but not both define s to be [2000, 1 Mark]
(d) neither (i) nor (ii) (a) an array, each element of which is a pointer to a
115. A program P reads in 500 integers in the range [0, 100] structure of type node
representing the scores of 500 students. It then prints the (b) a structure of 2 fields, each field being a pointer to
frequency of each score above 50. What would be the best an array of 10 elements
way for P to store the frequencies? [2005, 1 Mark] (c) a structure of 3 fields : an integer, a float and an
(a) An array of 50 numbers array of 10 elements
(b) An array of 100 numbers (d) an array, each element of which is a structure of type
(c) An array of 500 numbers node
(d) A dynamically allocated array of 550 numbers 121. Suppose you are given an array s[1....n] and a procedure
116. A single array A [1...MAXSIZE] is used to implement two reverse (s,i,j) which reverses the order of elements in a
stacks. The two stack graw from opposite ends of the between positions i and j (both inclusive). What does the
array. Variable top 1 and top 2 (top 1 < top 2 ) point to following sequence do, where 1 £ k £ n :
[2000, 2 Marks]
the location of the topmost element in the each of the
reverse (s, 1, k);
stacks. If the space is to be used efficiently, the condition
reverse (s, k + 1, n);
for stack full is [2004, 1 Mark]
reverse (s, 1, n);
(a) (top1 – MAXSIZE / 2 ) and (top 2 = MAXSIZE/2 + 1)
(a) Rotates s left by k positions
(b) top1 + top 2 = MAXSIZE
(b) Leaves s unchanged
(c) (top1 = MAZSIZE/2) or (top 2 = MAXSIZE) (c) Reverses all elements of s
(d) top1 = top 2 – 1 (d) None of these
117. Assume the following C variable declaration 122. Suppose you are given arrays p[1 ...N] and q[1...N] both
int* A [10], B [10] [10]; uninitialized that is, each location may contain an
Of the following expressions which will not give compile arbitrary value), and a variable count, initialized to 0.
time errors if used as left hand sides of assignment Consider the following procedures set and iset:
statements in a C program ? [2003, 1 Mark] [2000, 5 Marks]
(i) A [2](ii) A [2] [3] Set (i) {
(iii) B [1] (iv) B [2] [3] count = count + 1;
(a) I, II and IV only (b) II, III and IV only q [count] = i;
(c) II, and IV only (d) IV only p[i] = count;
118. In the following C program fragment j, k n and Two Log_n }
are integer variables, and A is an array of integers. The is_set(i) {
variable n is initialized to an integer ³ 3, and TwoLog_n if (p[i] £ 0 or p[i] > count)
return false;
is initialized to the value of 2* éêlog 2 (n)ùú if (q[p[i]] ¹ i)
for (k = 3; k < = n; k + +) return false;
A [k] = 0; return true;
for (k = 2; k < = TwoLog_n; k + +) }
for (j = k + 1; j < = n; j + +) (a) Suppose we make the following sequence of calls:
A[j] = A[j] || (j%k); set (7); set (3); set(9);
for (j = 3; j < = n; j + +) After these quence of calls, what is the value of
if (!A[j]) printf (“%d”, j); count, and what do q[1], q[2], q[3], p[7], p[3] and
The set of numbers printed by this program fragment is p[9] contain?
[2003, 2 Marks] (b) Complete the following statement “The first count
elements of ................ contain values i such that set
(a) {m | m £ n ( $i ) [m = i!]}
(....................) has been called”.
(b) {m | m £ n, ( $i ) [m = i2]}
(c) Show that if set (i) has not been called for some i,
(c) {m | m £ n, (m is prime)} then regardless of what p[i] contains, is_set (i) will
(d) { } return false.
74
123. A recursive program to compute Fibonacci numbers is 128. An implementation of a queue Q, using two stacks S1 and
shown below. Assume you are also given an array S2 is given below [2006, 2 Marks]
f[0...m] with all elements initialized to 0. [2000, 5 Marks] void insert (Q, x) {
fib(n){ push (S1, x);
if (n > m) error (); }
if (n = = 0) return 1; void delete (Q) {
if (n = = 1) return 1; if (stack-empty (S2)) then
if ( ) .......................... (1) if (stack-empty (S1)) then {
return .......................... (2) print (“Q is empty”);
t = fib (n – 1) + fib (n – 2); return;
.......................... (3) }
return t; else while (! (stack-empty (S1))) {
} x = pop (S1);
(a) Fill in the boxes with expressions/statements to push (S2, x);
make fib() store and reuse computed Fibonacci }
values. Write the box number and the corresponding x = pop (S2);
contents in your answer book. }
(b) What is the time complexity of the resulting Let n insert and m(£ n) delete operations be performed in an
program when computing fib(n)? arbitrary order on an empty queue Q. Let x and y be the
124. An array contains four occurrences of 0, five occurrences number of push and pop operations performed respectively
of 1, and three occurrences of 2 in any order. The array in the process. Which one of the following is true for all m
is to be sorted using swap operations (elements that are and n?
swapped need to be adjacent). [2000, 5 Marks] (a) n + m £ x < 2n and 2m £ y £ n + m
(a) What is the minimum number of swaps needed to (b) n + m £ x £ 2n and 2m £ y £ 2n
sort such an array in the worst case?
(b) Give an ordering of elements in the above array so (c) 2m £ x < 2n and 2m £ y £ n + m
that the minimum number of swaps needed to sort (d) 2m £ x < 2n and 2m £ y £ 2n
the array is maximum. 129. Let S be a stack of size n ³ 1. Starting with the empty
stack, suppose we push the first n natural numbers in
Stacks sequence, and then perform n pop operations. Assume
125. Which one of the following is TRUE at any valid state in that Push and Pop operations take X seconds each, and
Y seconds elapse between the end of one such stack
shift – reduce parsing? [2015, Set 1, 1 mark]
operations and the start of the next operation. For m ³ 1,
(a) Viable prefixes appear only at the bottom of the stack define the stack – life of m as the time elapsed from the
and not inside end of Push (m) to the start of the pop operation that
(b) Viable prefixes appear only at the top of the stack and removes m from S. The average stack – life of an element
not inside of this stack is [2003, 2 Marks]
(c) The stack contains only a set of viable prefixes (a) n (X + Y) (b) 3Y + 2X
(c) n (X + Y) – x (d) Y + 2X
(d) The stack never contains viable prefixes 130. To evaluate an expression without any embedded function
126. Suppose a stack implementation supports an instruction calls [2002, 2 marks]
REVERSE, which reverses the order of elements on the (a) one stack is enough
stack, in addition to the PUSH and POP instructions. Which (b) two stacks are needed
one of the following statements is TRUE with respect to (c) as many stacks as the height of the expression tree
this modified stack? [2014, Set-2, 2 Marks] are needed
(a) A queue cannot be implemented using this stack. (d) a turning machine is needed in the general case
(b) A queue can be implemented where ENQUEUE takes 131. What is the minimum number of stacks of size n required
a single instruction and DEQUEUE takes a sequence to implement a queue of size n? [2002, 2 Marks]
of two instructions. (a) one (b) Two
(c) A queue can be implemented where ENQUEUE takes (c) Three (d) Four
a sequence of three instructions and DEQUEUE takes
Queues
a single instruction.
(d) A queue can be implemented where both ENQUEUE 132. A circular queue has been implemented using a singly
and DEQUEUE take a single instruction each. linked list where each node consists of a value and a
127. The following postfix expression with single digit operands single pointer pointing to the next node. We maintain
is evaluated using a stack: [2007, 2 Marks] exactly two external pointers FRONT and REAR pointing
823^/23*+51*- to the front node and the rear node of the queue,
Note that ^ is the exponentiation operator. The top two respectively. Which of the following statements is/are
elements of the stack after the first * is evaluated are CORRECT for such a circular queue, so that insertion
(a) 6, 1 (b) 5, 7 and deletion operations can be performed in 0 (1) time?
(c) 3, 2 (d) 1, 5 [2017, Set 2, 1 Mark]
75
I. Next pointer of front node points to the rear node. (c) full (REAR = = FRONT
II. Next pointer of rear node points to the front node. empty: (REAR + 1) mod n = = REAR
(a) I only (b) II only (d) full (FRONT + 1) mod n = = REAR
(c) Both I and II (d) Neither I nor II empty: (REAR = = FRONT
133. A queue is implemented using an array such that ENQUEUE 137. A priority–queue is implemented as a max–heap. Initially,
and DEQUEUE operations are performed efficiently. Which it has 5 elements. The level – order traversal of the heap
one of the following statements is CORRECT (n refers to is given below.
the number of items in the queue)? [2016, Set 1, 1 Mark] 10, 8, 5, 3, 2
(a) Both operations can be performed in O(1) time Two new elements 1 and 7 are inserted in the heap in that
(b) At most one operation can be performed in O(1) time but order. The level – order traversal of the heap after the
insertion of the element is [2005, 2 Marks]
the worst case time for the other operation will be W(n)
(a) 10, 8, 7, 5, 3, 2, 1 (b) 10, 8, 7, 2, 3, 1, 5
(c) The worst case time complexity for both operations
(c) 10, 8, 7, 1, 2, 3, 5 (d) 10, 8, 7, 3, 2, 1, 5
will be W(n)
(d) Worst case time complexity for both operations will Linked Lists
be W(log n)
134. Let Q denote a queue containing sixteen numbers and S be 138. N items are stored in a sorted doubly linked list. For a
an empty stack. Head (Q) returns the element at the head of delete operation, a pointer is provided to the record to be
the queue Q without removing it from Q. Similarly Top (S) deleted. For a decrease-key operation, a pointer is
returns the element at the top of S without removing it from provided to the record on which the operation is to be
S. Consider the algorithm given below. [2016, Set 1, 2 Mark] performed.
An algorithm performs the following operations on the list
in this order: Q(N) delete, O(log N) insert, O(log N) find,
and Q(N) decrease-key. What is the time complexity of all
these operations put together? [2016, Set 2, 1 mark]
(a) O(log2N) (b) O(N)
(c) O(N ) 2 (d) Q(N2 log N)
139. The following C function takes a simply-linked list as input
argument. It modifies the list as input argument. It modifies
the list by moving the last element to the front of the list and
returns the modified list. Some part of the code is left blank.
type def struct node { [2010, 2 Marks]
int value;
The maximum possible number of iterations of the while struct node *next;
} Node*;
loop in the algorithm is _________.
Node *move_to_front (Node *head) {
135. Consider the following operation along the Enqueue and Node *p, *q;
Dequeue operations on queues, where k is a global if (head = = NULL | | (head -> next = = NULL)) return head;
parameter. q = NULL; p = head;
Multi-Dequeue(Q){ while (p - > next ! = NULL) {
m=k q = p;
while (Q is not empty) and (m > 0){ Dequeue(Q) p = p - > next;
m=m–1 }
} return head;
} }
What is the worst case time complexity of a sequence of Choose the correct alternative to replace the blank line.
n queue operations on an initially empty queue? (a) q = NULL; p - > next = head; head = p;
[2013, 2 Marks] (b) q - > next = NULL; head = p; p - > next = head;
(a) Q(n) (b) Q(n + k) (c) head = p; p -> next = q; q -> next = NULL;
(c) Q(nk) (d) Q(n2) (d) q-> next = NULL; p-> next = head; head = p;
136. Suppose a circular queue of capacity (n – 1) elements is 140. The following C function takes a single-linked list of integers
implemented with an array of n elements. Assume that the as a parameter and rearranges the elements of the list. The
insertion and deletion operations are carried out using REAR function is called with the list containing the integers 1, 2, 3,
and FRONT as array index variables, respectively. Initially 4, 5, 6, 7 in the given order. What will be the contents of the
REAR = FRONT = 0. The conditions to detect queue full and list after the function completes execution? [2008, 2 Marks]
queue empty are [2012, 2 Marks] struct node {
(a) full (REAR + 1) mod n = = FRONT int value;
empty: REAR = = FRONT struct node *next;
(b) full (REAR + 1) mod n = = FRONT };
empty: (FRONT + 1) mod n = = REAR void rearrange (struct node *list){
76
struct node *p, *q; n
int temp; (a) log n (b)
if (!list | | !list -> next) return; 2
p = list, q = list –> next; (c) log 2( n-1) (d) n
while (q){
temp = p - > value; p -> value = q - > value; Trees
q - > value = temp; p = q - > next;
q = p?p -> next: 0; 145. Let T be a tree with 10 vertices. The sum of the degrees of all
} the vertices in T is _______. [2017, Set 1, 1 Mark]
} 146. The result evaluating the postfix expression
(a) 1, 2, 3, 4, 5, 6, 7 (b) 2, 1, 4, 3, 6, 5, 7 10, 5, +, 60, 6, /, *, 8, – is [2015, Set 3, 1 Mark]
(c) 1, 3, 2, 5, 4, 7, 6 (d) 2, 3, 4, 5, 6, 7, 1 (a) 284 (b) 213
141. A circularly linked list is used to represent a queue. A single
(c) 142 (d) 71
variable p is used to access the queue. To which node should
p point such that both the operations en-queue and de- 147. Consider the following rooted tree with the vertex labeled P
as the root :
queue can be performed in constant time? [2005, 2 Marks]
P

Front Rear
Q R

p P
U V
(a) Rear node T
(b) Front node S
(c) Not possible with a single pointer (d)
(d) Node next to front
142. Suppose each set is represented as a linked list with W
elements in arbitrary order. Which of the operations
among union, intersection, membership, cardinality will
The order in which the nodes are visited during an in-order
be the slowest? [2004, 2 Marks]
traversal of the tree is [2014, Set-3, 1 Mark]
(a) Union only
(a) SQPTRWUV (b) SQPTUWRV
(b) Intersection, membership (c) SQPTWUVR (d) SQPTRUWV
(c) Membership, cardinality 148. Consider the expression tree shown. Each leaf represents a
(d) Union, intersection numerical value, which can either be 0 or 1. Over all possible
143. Consider the function f defined below : choices of the values at the leaves, the maximum possible
struct item { value of the expression represented by the tree is ______.
int data;
struct item * next :
};
int f (struct item * p) {
return ((p = = NULL) P (p – > next = = NULL) P
((p– > data < = p – > next –> data) &&
f (p–> next)));
}
For a given linked list p, the function f returns 1, if and
only, if [2003, 2 Marks]
(a) the list is empty or has exactly one element
0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1
(b) the elements in the list are sorted in non-decreasing
order of data value [2014, Set-2, 2 Marks]
(c) the elements in the list are sorted in non-increasing 149. Consider evaluating the following expression tree on a
order of data value machine with load store architecture in which memory can be
(d) not all element in the list have the same data value accessed only through load and store instructions. The
144. In the worst case, the number of comparisons needed to variables a, b, c, d and e are initially stored in memory. The
search a singly linked list of length n for a given element binary operators used in this expression tree can be evaluated
is [2002, 1 Mark] by the machine only when the operands are in registers. The
instructions produce result only in a register. If no intermediate
77
results can be stored in memory, what is the minimum number The value returned by the function Dosomething when a
of registers needed to evaluate this expression? pointer to the root of a non-empty tree is passed as
[2011, 2 Marks] argument is
+
(a) the number of leaf nodes in the tree
(b) the number of nodes in the tree
– – (c) the number of internal nodes in the tree
(d) the height of the tree
a b e 156. The number of leaf nodes in a rooted tree of n nodes,
+
with each node having 0 or 3 children is [2002, 2 Marks]
c d n ( n - 1)
(a) (b)
(a) 2 (b) 9 2 3
(c) 5 (d) 3
150. What is the maximum height of any AVL-tree with 7 nodes? ( n - 1) ( 2n + 1)
(c) (d)
Assume that the height of a tree with a single node is 0. 2 3
[2009, 1 Mark] 157. (a) Suppose you are given an empty B+-tree where
(a) 2 (b) 3 each node (leaf and internal) can store up to 5 key
(c) 4 (d) 5 values. Suppose values 1, 2, ... 10 are inserted, in
151. A B-tree of order 4 is built from scratch by 10 successive
order, into the tree, show the tree pictorially
insertions. What is the maximum number of node splitting
operations that may take place? [2008, 2 Marks] (i) After 6 insertions, and
(a) 3 (b) 4 (ii) After all 10 insertions
(c) 5 (d) 6 Do NOT show intermediate stages.
152. A complete n-array tree is a tree in which each node has n (b) Suppose instead of splitting a node when it is full,
children or no children. Let l be the number of internal nodes we try to move a value to the left sibling. If there
and L be the number of leaves in a complete n-array tree. If L is no left sibling, or the left sibling is full, we split
= 41, and l = 10, what is the value of n? the node. Show the tree after values, 1, 2, ..., 9 have
[2007, 2 Marks] been inserted. Assume, as in (a) that each node can
(a) 3 (b) 4 hold up to 5 keys.
(c) 5 (d) 6 (c) In general, suppose a B+-tree node can hold a
153. In a complete k-array tree, every internal node has exactly
maximum of m keys, and you insert a long sequence
k children. The number of leaves in such a tree with n
of keys in increasing order. Then what approximately
internal nodes is [2005, 2 Marks]
(a) nk (b) (n – 1) k + 1 is the average number of keys in each leaf level node.
(c) n (k – 1) + 1 (d) n (k – 1) (i) In the normal case, and
154. Assume that the operators +, –, ×, are left associative and ^ (ii) with the insertion as in (b). [2000, 5 Marks]
is right associative. The order of precedence (from highest
to lowest) is ^, ×, +, –. The postfix expression corresponding Binary Tree and Binary Search Trees
to the infix expression a + b × c – d ^ e ^ f is [2005, 2 Marks] 158. Let T be a binary search tree with 15 nodes. The minimum
(a) abc × + def ^^ – (b) abc × + de^f^– and maximum possible heights of T are:
(c) ab + c × d – e^f^ (d) – + a × bc ^^ def [2017, Set 1, 1 Mark]
155. Consider the following C program segment
Note: The height of a tree with a single node is 0.
struct Cell Node{ [2005, 2 Marks]
(a) 4 and 15 respectively
struct Cell Node *left Child;
int element; (b) 3 and 14 respectively
struct Cell Node *right Child; (c) 4 and 14 respectively
} (d) 3 and 15 respectively
int Dosomething (struct Cell Node *ptr) 159. The pre-order traversal of a binary search tree is given
{ by 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20. Then the post-
int value = 0; order traversal of this tree is: [2017, Set 2, 2 Marks]
if (ptr! = NULL) (a) 2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20
{ if (ptr-> left Child! = NULL) (b) 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12
value = 1 + Dosomething (ptr -> left Child); (c) 7, 2, 6, 8, 9, 10, 20, 17, 19, 15, 16, 12
if(ptr-> rightChild! = NULL) (d) 7, 6, 2, 10, 9, 8, 15, 16, 17, 20, 19, 12
value = max (value, 1 + Dosomething (ptr-
160. In a B+ tree, if the search-key value is 8 bytes long, the
> rightChild));
} block size is 512 bytes and the block pointer size is 2
return (value); bytes, then the maximum order of the B+ tree is ______.
} [2017, Set 2, 2 Marks]
78
161. B+ Trees are considered BALANCED because subtrees of the root are max-heaps. The lower bound for the
[2016, Set 2, 1 Mark] number operations to convert the tree to a heap is
(a) the lengths of the paths from the root to all leaf [2015, Set 2, 1 Mark]
nodes are all equal.
(a) W(log n) (b) W(n)
(b) the lengths of the paths from the root to all leaf
nodes differ from each other by at most 1. (c) W(n log n) (d) W(n2)
(c) the number of children of any two non-leaf sibling 168. A binary tree T has 20 leaves. The number of nodes in T
nodes differ by at most 1. having two children is _____. [2015, Set 2, 1 Mark]
(d) the number of records in any two leaf nodes differ 169. While inserting the elements 71, 65, 84, 69, 67, 83 in an empty
by at most 1. Binary Search Tree (BST) in the sequence shown, the
162. Consider the following New-order strategy for traversing element in the lowest level is [2015, Set 3, 1 Mark]
a binary tree: [2016, Set 2, 2 Marks] (a) 65 (b) 67
• Visit the root; (c) 69 (d) 83
• Visit the right subtree using New-order; 170. Consider a binary tree T that has 200 leaf nodes. Then, the
• Visit the left subtree using New-order; number of nodes in T that have exactly two children
The New-order traversal of the expression tree are _______. [2015, Set 3, 1 Mark]
corresponding to the reverse polish expression 3 4 * 5 – 171. If the following system has non – trivial solution
2 ^ 6 7 * 1 + – is given by: px +qy + rz = 0
(a) + – 1 6 7 * 2 ^ 5 – 3 4 * qx + ry + pz = 0
(b) – + 1 * 6 7 ^ 2 – 5 * 3 4 rx + py +qz = 0
(c) – + 1 * 7 6 ^ 2 – 5 * 4 3 then which one of the following options is TRUE?
(d) 1 7 6 * + 2 5 4 3 * – ^ – (a) p – q +r = 0 or p = q = – r
163. The number of ways in which the numbers 1, 2, 3, 4, 5, 6, (b) p + q – r = 0 or p = – q = r
7 can be inserted in an empty binary search tree, such that (c) p + q +r = 0 or p = q = r
the resulting tree has height 6, is ________. (d) p – q +r = 0 or p = – q = – r
[2016, Set 2, 2 Marks] 172. Consider a B+ tree in which the search key is 12 bytes long,
Note: The height of a tree with a single node is 0. block size is 1024 bytes, record pointer is 10 bytes long and
164. The height of a tree is the length of the longest root-to-leaf block pointer is 8 bytes long. The maximum number of keys
path in it. The maximum and minimum number of nodes in a that can be accommodated in each non – leaf node of the tree
binary tree of height 5 are [2015, Set 1, 1 Mark] is ______. [2015, Set 3, 2 Marks]
(a) 63 and 6, respectively 173. Consider a rooted n node binary tree represented using
(b) 64 and 5, respectively pointers. The best upper bound on the time required to
(c) 32 and 6, respectively determine the number of subtrees having exactly 4 nodes is
(d) 31 and 5, respectively O (nalogbn). Then the value of a + 10b is ______.
165. Which of the following is/are correct inorder traversal [2014, Set-1, 1 Mark]
sequence(s) of binary search tree(s)? 174. Suppose we have a balanced binary search tree T holding n
numbers. We are given two numbers L and H and wish to
I. 3, 5, 7, 8, 15, 19, 25 II. 5, 8, 9, 12, 10, 15, 25
sum up all the numbers in T that lie between L and H. Suppose
III. 2, 7, 10, 8, 14, 16, 20 IV. 4, 6, 7, 9, 18, 20, 25
there are m such numbers in T. If the tightest upper bound on
[2015, Set 1, 1 Mark] the time to compute the sum is O (na logb n + mc logd n), the
(a) I and IV only (b) II and iii only value of 10b + 100c + 1000d is _______.
(c) II and IV only (d) II only [2014, Set-3, 2 Marks]
166. With reference to the B+ tree index of order 1 shown below, 175. Which one of the following is the tightest upper bound
the minimum number of nodes (including the Root node) that that represents the time complexity of inserting an object
must be fetched in order to satisfy the following query: "Get into a binary search tree of n nodes?
all records a search key greater than or equal to 7 and less [2013, 1 Mark]
than 15" is ____. [2015, Set 2, 1 mark] (a) O(1) (b) O(log n)
(c) O(n) (d) O(n log n)
9 176. The pre-order traversal sequence of a binary search tree
is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the
5 13 17 following is the post-order traversal sequence of the
same tree? [2013, 2 Marks]
(a) 10, 20, 15, 23, 25, 35, 42, 39, 30
1 3 5 7 9 11 13 15 17 (b) 15, 10, 25, 23, 20, 42, 35, 39, 30
(c) 15, 20, 10, 23, 25, 42, 35, 39, 30
167. Consider a complete binary tree where the left and the right
(d) 15, 10, 23, 25, 20, 35, 42, 39, 30
79
177. The height of a tree is defined as the number of edges on the (a) log2 n (b) n
longest path in the tree. The function shown in the below in (c) 2n + 1 (d) 2n – 1
invoked as height (root) to compute the height of a binary 182. The maximum number of binary trees that can be formed
tree rooted at the tree pointer root. [2012, 2 Marks] with three unlabelled nodes is [2007, 1 Mark]
{ if (n = = NULL) return – 1; (a) 1 (b) 5
if (n ® left = = NULL) (c) 4 (d) 3
if (n ® right = = NULL) return 0; 183. The height of a binary tree is the maximum number of edges
else return B1 ; // Box 1 in any root to leaf path. The maximum number of nodes in a
binary tree of height h is [2007, 1 Mark]
else {h1 = height (n ® left); (a) 2h – 1 (b) 2h–1 – 1
if (n ® right = NULL) return (1 + h1); (c) 2h + 1 – 1 (d) 2h + 1
else {h2 = height (n ® right); 184. The inorder and preorder traversal of a binary tree are d b e
return B2 ; // Box 1 a f c g and a b d e c f g, respectively The postorder traversal
of the binary tree is. [2007, 2 Marks]
} (a) d e b f g c a (b) e d b g f c a
} (c) e d b f g c a (d) d e f g b c a
} 185. Consider the following C program segment where CellNode
The appropriate expressions for the two boxes B1 and B2 represents a node in a binary tree: [2007, 2 Marks]
are struct Cell Node {
(a) B1 : (1 + height (n ® right) struct Cell Node *leftChild;
B2 : (1 + max (h1, h2) int element;
(b) B1 : (height (n ® right) struct CellNode *rightChild;
B2 : (1 + max (h1, h2) }
(c) B1 : (height (n ® right) int GetValue (struct CellNode *ptr) {
B2 : max (h1, h2) int value = 0
(d) B1 : (1 + height (n ® right)
if (ptr ! = NULL)
B2 : max (h1, h2)
if ((ptr - > leftChild = = NULL) &&
178. We are given a set of n distinct elements and an unlabelled
(ptr -> rightChild == NULL){
binary tree with n nodes. In how ways can we populate the
value = 1;
tree with the given set so that it becomes a binary search tree?
else
[2011, 2 Marks] value = value + GetValue (ptr -> leftChild)
(a) 0 (b) 1 + GetValue (ptr -> rightChild);
1 2n }
(c) n! (d) Cn return (value);
n +1
179. In a binary tree with n nodes, every node has an odd number }
of descendant. Every node is considered to be its own The value returned by GetValue when a pointer to the root
descendant. What is the number of nodes in the tree that of a binary tree is passed as its argument is
have exactly one child? [2010, 1 Mark] (a) the number of nodes in the tree
(a) 0 (b) 1 (b) the number of internal nodes in the tree
(c) (n – 1)/2 (d) n – 1 (c) the number of leaf nodes in the tree
180. You are given the postorder traversal, P, of a binary search (d) the height of the tree
tree on the n elements 1, 2, ..., n. You have to determine the 186. The inorder and preorder traversal of a binary tree are
unique binary search tree that has P as its postorder traversal. d b e a f c g and a b d e c f g, respectively
What is the time complexity of the most efficient algorithm The postorder traversal of the binary tree is
[2007, 2 Marks]
for doing this? [2008, 2 Marks]
(a) d e b f g c a (b) e d b g f c a
(a) Q (log n)
(c) e d b f g c a (d) d e f g b c a
(b) Q (n)
187. Postorder traversal of a given binary search tree, T produces
(c) Q (n log n) the following sequence of keys
(d) None of the above, as the tree cannot be uniquely 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29
determined Which one of the following sequences of keys can be the
181. A scheme for storing binary trees in an array X is as follows. result of an in-order traversal of the tree T?
Indexing of X starts at 1 instead of 0. The root is stored at [2005, 2 Marks]
X[1].For a node stored at X[i], the left child, if any, is stored (a) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
in X[2i] and the right child, if any in X[2i | 1]. To be able to (b) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
store any binary tree on n vertices the minimum size of X (c) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
should be [2007, 2 Marks] (d) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
80
188. How many distinct binary search trees can be created out
(a) P
of 4 distinct keys? [2005, 2 Marks]
(a) 5 (b) 14
G L U
(c) 24 (d) 42
189. A program takes as input a balanced binary search tree
with n leaf nodes and computes the value of a function B HI N QT VXZ
g (x) for each node x. If the cost of computing g (x) is min
(number of leaf-nodes in left – subtree of x,number of leaf (b) P
– nodes in right – subtree of x) then the worst – case time
complexity of the program is [2004, 2 Marks] H L U
(a) Q(n) (b) Q(n log n)
(c) Q(n2) (d) Q(n2 log n) BG I N QT VXZ
190. Consider the label sequences obtained by the following
pairs of traversals on a labelled binary tree. Which of (c)
I P U
these pairs identify a tree uniquely? [2004, 2 Marks]
(i) Preorder and postorder
(ii) Inorder and postorder B G H LN Q T V X Z
(iii) Preorder and inorder
(d) None of these
(iv) Level order and postorder 195. A weight – balanced tree is a binary tree in which for each
(a) (i) only (b) (ii) and (iii) node, the number of nodes in the left sub tree is at least
(c) (iii) only (d) (iv) only half and at most twice the number of nodes in the right
191. The following numbers are inserted into an empty binary sub tree. The maximum possible height (number of nodes
search tree in the given order : 10, 1, 3, 5, 15, 12, 16. What on the path from the root to the furthest leaf) of such a
is the height of the binary search tree (the height is the tree on n nodes is best described by which of the
maximum distance of a leaf node from the root)? following? [2002, 2 Marks]
[2004, 1 Mark] (a) log2n (b) log4/3n
(c) log3n (d) log3/2n
(a) 2 (b) 3 196. Let LASTPOST, LASTIN and LASTPRE denote the last
(c) 4 (d) 6 vertex visited in a postorder, inorder and preorder traversal.
192. Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted Respectively, of a complete binary tree. Which of the
in that order into an initially empty binary search tree. following is always true? [2000, 2 Marks]
The binary search tree uses the usual ordering on natural (a) LASTIN = LASTPOST
numbers. What is the in-order traversal sequence of the (b) LASTIN = LASTPRE
resultant tree? [2003, 1 Mark] (c) LASTPRE = LASTPOST
(a) 7 5 10 3 2 4 6 8 9 (b) 0 2 4 3 1 6 5 9 8 7 (d) None of these
(c) 0 1 2 3 4 5 6 7 8 9 (d) 9 8 6 4 2 3 0 1 5 7 197. Consider the following nested representation of binary
193. Let T (n) be the number of different binary search trees trees: (XYZ) indicates Y and Z are the left and right sub
trees, respectively, of node X. Note that Y and Z may be
n NULL, or further nested. Which of the following represents
on n distinct elements. Then, T (n) = å T (K–1) T ( x), a valid binary tree? [2000, 1 Mark]
k =1 (a) (1 2 (4 5 6 7)) (b) (1 (2 3 4) (5 6 )7)
where x is [2003, 1 Mark] (c) (1 (2 3 4) (5 6 7)) (d) (1 (2 3 NULL) (4 5))
(a) n – k + 1 (b) n – k
(c) n – k – 1 (d) n – k – 2 Binary Heap
194. Consider the following 2 – 3 – 4 tree (i. e., B – tree with
a minimum degree of two) in which each data item is a 198. An operator delete (i) for a binary heap data structure is to
letter. The usual alphabetical ordering of letters is used in be designed to delete the item in the i-th node. Assume that
constructing the tree. [2003, 2 Marks] the heap is implemented in an array and i refers to the i-th
index of the array. If the heap tree has depth d (number of
L P U edges on the path from the root to the farthest leaf), then
what is the time complexity to re-fix the heap efficiently after
the removal of the element? [2016, Set 1, 2 Mark]
(a) O (1)

BHI N QT VXZ (b) O (d) but not O (1)


(c) O (2d) but not O (d)
What is the result of inserting G in the above tree? (d) O (d 2d) but not O (2d)
81
199. A complete binary min-heap is made by including each (c) (d)
10 5
integer in [1,1023] exactly once. The depth of a node in the
heap is the length of the path from the root of the heap 2
5 6 8
to that node. Thus, the root is at depth 0. The maximum
depth at which integer 9 can appear is _______. 4 1
8 2 1 4 6 10
[2016, Set 2, 2 Marks]
200. Consider a max heap, represented by the array: 40, 30, 20, 10, Statements for Linked Answer Questions 206, 207 and 208:
15, 16, 17, 8, 4. Consider a binary max-heap implemented using an array:
206. Which one of the following array represents a binary max-
Array Index 1 2 3 4 5 6 7 8 9 heap? [2009, 1 Mark]
Value 40 30 20 10 15 16 17 8 4 (a) {25, 12, 16, 13, 10, 8, 14}
(b) {25, 14, 13, 16, 10, 8, 12}
Now consider that a value 35 is inserted into this heap. After
(c) {25, 14, 16, 13, 10, 8, 12}
insertion, the new heap is [2015, Set 1, 2 Marks]
(d) {25, 14, 12, 13, 10, 8, 16}
(a) 40, 30, 20, 10, 15, 16, 17, 8, 4, 35 207. What is the content of the array after two delete operations
(b) 40, 35, 20, 10, 30, 16, 17, 8, 4, 15 on the correct answer to the previous question?
(c) 40, 30, 20, 10, 35, 16, 17, 8, 4, 15 [2009, 1 Mark]
(d) 40, 35, 20, 10, 15, 16, 17, 8, 4, 30 (a) {14, 13, 12, 10, 8} (b) {14, 12, 13, 8, 10}
201. Consider the following array of elements (c) {14, 13, 8, 12, 10} (d) {14, 13, 12, 8, 10}
208. The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an
(89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100)
initially empty hash table of length 10 using open addressing
The minimum number of interchanges needed to convert
with hash function h(k) = k mod 10 and linear probing. What
it into a max-heap is [2015, Set 3, 1 Mark] is the resultant hash table? [2009, 1 Mark]
(a) 4 (b) 5
(a) 0 (b) 0
(c) 2 (d) 3
202. A priority queue is implemented as a Max-Heap. Initially, it 1 1
has 5 elements. The level-order traversal of the heap is: 10, 2 2 2 12
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 3 23 3 13
after the insertion of the elements is: 4 4
[2014, Set-2, 1 Mark] 5 15 5 5
(a) 10, 8, 7, 3, 2, 1, 5 (b) 10, 8, 7, 2, 3, 1, 5
(c) 10, 8, 7, 1, 2, 3, 5 (d) 10, 8, 7, 5, 3, 2, 1 6 6
203. The number of elements that can be sorted in Q(log n) 7 7
time using heap sort is [2013, 2 Marks] 8 18 8 18
(a) Q(1) (b) Q ( log n ) 9 9

æ log n ö (c) 0 (d) 0


(c) Qç (d) Q(log n)
è log log n ÷ø 1 1
204. Which languages necessarily need heap allocation in the 2 12 2 12, 2
runtime environment? [2011, 2 Marks] 3 13 3 13, 3, 23
(a) Those that support recursion 4 2 4
(b) Those that use dynamic scoping 5 3 5 5, 15
(c) Those that allow dynamic data structures
6 23 6
(d) Those that use global variables
205. A max-heap is a heap where the value of each parent is 7 5 7
greater than or equal to the value of its children. Which of 8 18 8 18
the following is a max-heap? [2011, 1 Mark] 9 15 9
(a) (b) 10 209. We have a binary heap on n elements and wish to insert n
10
8 more elements (not necessarily one after another) into this
8 6 6
heap. The total time required for this is [2008, 2 Marks]
5 4 5 1 2 (a) Q (log n) (b) Q (n)
4 2
1 (c) Q (n log n) (d) Q (n2)
82
210. In a binary max heap containing n numbers, the smallest (a) A heap can be used but not a balanced binary
element can be found in time [2007, 2 Marks] search tree
(a) O (n) (b) O (log n) (b) A balanced binary search tree can be used but not
(c) O (log log n) (d) O (1) a heap
211. Consider the process of inserting an element into a Max (c) Both balanced binary search tree and heap can be
Heap, where the Max Heap is represented by an array.
used
Suppose we perform a binary search on the path from the
new leaf to the root to find the position for the newly inserted (d) Neither balanced binary search tree nor heap can be
element, the number of comparisons performed is used
[2007, 2 Marks] 216. Consider an array representation of an n element binary
(a) Q (log2 n) (b) Q (log2 log2 n) heap where the elements are stored from index 1 to index
(c) Q (n) (d) Q (n log2 n) n of the array. For the element stored at index i of the
Statement for Linked Answer Questions 212 and 213 array (i £ n), the index of the parent is [2001, 1 Mark]
A 3-ary max heap is like a binary max heap, but instead of 2
children, nodes have 3 children. A 3-ary heap can be êiú
represented by an array as follows : The root is stored in the (a) i – 1 (b) êë 2 úû
first location, a[0], nodes in the next level, from left to right is
stored from a[1] to a [3]. The nodes from the second level of éiù (i + 1)
the tree from left to right are stored from a[4] location onward. (c) êê 2 úú (d)
2
An item x can be inserted into a 3-ary heap containing n items
by placing x in the location a[n] and pushing it up the tree to Graph
satisfy the heap property.
212. Which one of the following is a valid sequence of 217. G is an undirected graph with n vertices and 25 edges
elements in an array representing 3 – ary max heap? such that each vertex of G has degree at least 3. Then the
(a) 1, 3, 5, 6, 8, 9 [2006, 2 Marks]
maximum possible value of n is _______.
(b) 9, 6, 3, 1, 8, 5
(c) 9, 3, 6, 8, 5, 1 [2017, Set 2, 1 Mark]
(d) 9, 5, 6, 8, 3, 1 218. Consider the following directed graph:
213. Suppose the elements 7, 2, 10 and 4 are inserted in that [2016, Set 1, 1 Mark]
order, into the valid 3 – ary max heap found in the above
question 60. Which one of the following is the sequence c
b
of items in the array representing the resultant heap?
[2006, 2 Marks]
(a) 10, 7, 9, 8, 3, 1, 5, 2, 6, 4 a f
(b) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
(c) 10, 9, 4, 5, 7, 6, 8, 2, 1, 3 d e
(d) 10, 8, 6, 9, 7, 2, 3, 4, 1, 5
214. The elements 32, 15, 20, 30, 12, 25, 16, are inserted one by The number of different topological orderings of the vertices
one in the given order into a max-heap. The resultant max-
of the graph is ________ .
heap is [2005, 2 Marks]
219. Let G be a weighted connected undirected graph with distinct
32 32
positive edge weights. If every edge weight is increased by
25
the same value, then which of the following statements is/
(a) 30 25 (b) 30
are TRUE? [2016, Set 1, 1 Mark]
15 12 20 16 12 15 20 16 P: Minimum spanning tree of G does not change
Q: Shortest path between any pair of vertices does not
32 32
change
25 (a) P only (b) Q only
(c) 30 25 (d) 30
(c) Neither P nor Q (d) Both P and Q
15 12 16 20 12 15 16 20 220. Consider the weighted undirected graph with 4 vertices,
215. A data structure is required for storing a set of integers where the weight of edge {i, j} is given by the entry Wi j in
such that each of the following operations can be done the matrix W. [2016, Set 1, 2 Mark]
in O(log n), time, where n is the number of elements in the
set é0 2 8 5ù
I. Deletion of the smallest element. ê2 0 5 8 úú
II. Insertion of an element, if it is not already present ê
W = ê8 5 0 xú
in the set ê ú
Which of the following data structures can be used for ë5 8 x 0û
this purpose? [2003, 2 Marks]
83
The largest possible integer value of x, for which at least a
one shortest path between some pair of vertices will
contain the edge with weight x is ________.
221. A graph is self-complementary if it is isomorphic to its
complement. For all self-complementary graphs on n
vertices, n is [2015, Set 2, 2 Marks] e b f
(a) A multiple of 4
(b) Even h
(c) Odd
(d) Congruent to 0 mod 4, 1 mod 4
222. In a connected graph, a bridge is an edge whose removal
disconnects a graph. Which one of the following statements g
is true? [2015, Set 2, 2 Marks] Among the following sequences
(a) A tree has no bridge 1. abeghf 2. abfehg
(b) A bridge cannot be part of a simple cycle 3. abfhge 4. afghbe
(c) Every edge of a clique with size ³ 3 is a bridge (A clique Which are depth first traversals of the above graph?
is any complete subgraph of a graph (a) 1, 2 and 4 (b) 1 and 4
(d) A graph with bridge cannot have a cycle (c) 2, 3 and 4 (d) 1, 3 and 4
223. An ordered n-tuple (d1, d2, .., dn) with d1 ³ d2 ³ ... ³ dn is 226. Maximum number of edges in a n-node undirected graph
without self loops is [2002, 1 Mark]
called graphic if there exists a simple undirected graph with
n vertices having degrees d1, d2, ..., dn respectively. Which n ( n - 1)
(a) n2 (b)
of the following 6-tuples is NOT graphic? 2
[2014, Set-1, 2 Marks] ( n + 1)( n )
(a) (1, 1, 1, 1, 1, 1) (b) (2, 2, 2, 2, 2, 2) (c) n – 1 (d)
2
(c) (3, 3, 3, 1, 0, 0) (d) (3, 2, 1, 1, 1, 0) 227. How many undirected graphs (not necessarily connected)
224. Consider an undirected graph G where self-loops are not can be constructed out of a given set V = {v1, v2,....,vn}of
allowed. The vertex set of G is {(i, j): 1 £ i £ 12, 1 £ j £ 12}. n vertices? [2001, 2 Marks]
There is an edge between (a, b) and (c, d) if |a – c| £ 1 and n ( n - 1)
(a) (b) 2n
|b – d| £ 1. The number of edges in this graph is _________. 2
[2014, Set-1, 2 Marks] n( n -1)
225. Consider the following graph : [2003, 1 Mark] (c) n! (d) 2 2
84

Programming in C Similarly all steps execute then, we get the correct.


Sequence is 5 3 4 2 3 1 2 2 1 3 2 4 3 5
1. (b) According to the given C program. It used two linked Hence, option (c) is correct.
lists. After the code execution, list ‘m’ will be appended
5. (c) In the given program, invocation of foo (3) and bar (3),
to the end of list ‘n’ due to pointer ‘p’. Move to last in which, foo (3) turn calls foo (3). This goes on infinite
node of the list but in some cases it may dereference to number of times which causes memory overflow and
null pointer. causes abnormal termination.
m n p bar (3) ® bar (2) ® bar (1) ® bar (0) and (return 0)
from here onwards bar (1) will bar (0) and bar (0) will
X X
return 0 to bar (1) and this goes on forever without
Head 1 Tail 1 Head 2 Tail 2 causing memory overflow but it goes on infinite loop
execution.
2. (b) Consider all options.
Hence, option (c) is correct.
1. In option (a), p1 and q1 are initialized twice or used
6. (3) In the given program.
again for temporary storage, which is not allowed under
static single assignment. X is pointer of string “abc” which length is 3.
2. In option (b), there is no initialization again of the Strlen (S) = 3
variables and all the statement are correct. where (S) is pointer that pointed to X.
3. In option (c), the second line statement is not correct. Y is pointer of string “defgh”, which length is 5.
It should be Strlen (t) = 5.
q1 = p1 * C. where t is pointer that pointed to y, value of C is 0.
And P2, P4, q3 are not initialized anywhere. Now consider the statement :
4. In option (d), the second line statements is not correct. Intlen = [(Strlen (S) – Strlen (t) > 0)? Strlen (S) : Strlen (t)]
It should be = [(3 – 5)] > 0
q1 = P1 * C = [–2] > 0
So, option (b) is correct. It is not true but, since it is given in question
3. (d) Consider all options.
[Strlen (S) – Strlen (t)] will give unsigned value.
1. In option (a), we don’t need to cast the, result as void * is
So, [Strlen (S) – Strlen (t)] = |–2| = 2.
automatically and safely promoted to any other pointer
Therefore
type in this case. So it is not correct.
2. In option (b), it is discarded for obvious reason, so it is Þ 2 > 0 (it is true) : Strlen (S) : Strlen (t);
also not correct. Since (2 > 0) will evaluate true so it will give output as
3. In option (c), the dangling pointer is nothing but the Strlen (S) = 3. Hence ‘3’ will be printed.
pointer which is pointing to non-existing memory. 7. (23) Count in the function Total is static :
(deallocated or deleted memory) which is not i Count Total (i)
happening here, so it is also not correct. 5 0 2
4. In option (d), when you are calling malloc second time, 4 2 3 = (2 + 1)
then new location is assigned to X and previous 3 3 5 = (3 + 2)
memory location is lost and now we do not have no 2 5 6 = (5 + 1)
reference to that location, resulting in memory leak, so
1 6 7 = (6 + 1)
option (d) is correct.
Total : 23
4. (c) In the given program, when the function fun 1( ) is
calling function fun 2 ( ), after printing value and after Check out the running code with comments
returning from fun 2 ( ), it prints the same value. for (i = 5; i > 0; i – –)
In the function fun 2 ( ) also the same thing happens x = x + total (5) ;
so by looking options we can find the correct sequence While (5) (\ v = 5)
of the output after executing fun1(5), (n = 5), fun1(n), Count = Count + V & 1
print Þ 5. = Count + 5 & 1 (5 & 1 = 1)
fun2 (n – 2) Þ fun2 (3), Print Þ 3. Count = Count + 1 (Count = 1)
85
if (V = 2) to store addresses because ptr is an array of 10
Count = 1 + 2 & 1 = 1 + 0 = 1 pointers pointing to character variables.
if (V = 1), count = (1 +(1 & 1)) = 1 + 1 4. Register int var1; : Request to allocate a CPU
Count = (1 + 1) = 2 register to store data because the complier to store
Return (2); the var 1 “value” in CPU Register.
Then, X = 2 + total (4): Hence, option (d) is correct.
Ccunt = (2 + (4 & 1)) = 2 + 0 = 2 9. (c) int * Ptr;
(\ 2 is static) X = 0;
If (V = 2)
Ptr
Count = (2 + (2 & 1 )) = 2 + 0 = 2
if (v = 1) 0
Count = ( 2 + (1 & 1)) = 3 ×
Count = 3 Ptr = & X
Y = *Ptr
return (3)
Þ (Y = 0)
X = ((2 + 3) + total (3);) *Ptr = 1;
(Q2 is static)
X = (5 + total (3);) Ptr
( Q 5 is static) 1
So, count = (3 + (3 & 1)) = 3 + 1 = 4 ×
If (V = 2) Þ (X = 1)
Count = (4 + (2 & 1)) = 4 + 0 = 4 So, the output of invoking print XY(1, 1) is
If (V = 1) Þ (1, 0)
Count = (4 + (1 & 1)) = 4 + 1 = 5 Hence, option (c) is correct.
Count = 5 10. (c) The given programe is :
return (5) While (r > = y)
{
X = (5 + 5) + total (2);
r = r – y; (/*statement 1*/)
(Q5 is static)
q = q + 1; (/* statement 2*/)
X = (10 + total (2);)
}
( Q 10 is static) so, count = 6 return (6); Condition given
X = (10 + 6) + total (1);) X = (Y*q + r)
Let q = quotient and r = remainder.
( Q 10 is static) so, count = 7 return (7);
So to divide a number with repeated subtraction,
= 16 + total (1); quotient should be initialized to zero and should be
( Q 16 is static), so count = 16 + 7 = 23 (static) incremented for each subtraction.
(Q7 is static) So, initially q = 0, then
Hence in the function Total count static is : 23.
X = (Y*0 + r)
8. (a) 1. Static char var; : Initialization of a variable located
(r = X)
in data section of memory because var is defined
as character variable whose associated storage Since y is subtracted from reach time in given code. In,
class is static. statement one and q incremented by 1, to avoid
2. m = m alloc (10); m = Null; A lost memory which undefined behavior. In statement two and the value of
cannot be freed because free (m) is missed in code y should be greater than zero.
due to 10 contiguous bytes of memory is Therefore (q = = 0) & & (r = = X) & & (y > 0).
allocated is address of first byte is stored in ‘m’ Hence, option (c) is correct.
and later it is updated with NULL. Now we lost
11. (c) In the given program,
the address of first bytes of that chunk of memory
completely, so we can’t free that space as we need First loop will execute 'n' times and the inner loop (j),
the address of first byte to free it. will execute Q(n log (n)) times because for (i = 1), j will
3. Char * Ptr [10]; : Sequence of memory locations
86
run from 1 to n by incrementing by '1' in each step j will = Strlen (100 + 11 – 1)
run for n times, for (i = 2). = Strlen (100 + 10)
Similarly j will run from 1 to n by incrementing by '2', in = Strlen (110)
each step j will run for (n/2) times and so on. =2
Then the time complexity 14. (d)
‘i’ is integer and *P is value of a pointer to short.
n n n n 15. 2016
Tn = + + + ..... ....log(n - 1)
1 2 3 (n - 1) As in C, parameters are passed by value - even if they are
pointers. So, here the pointer values are exchanged within
é1 1 1 1 ù the function only (one can use * operator to exchange the
= n ê + + + ..... ú - log( n - 1)
ë 1 2 3 ( n - 1) û values at the location of the pointers and this will affect the
values in main). So, there will be no change in a, b, c, d. The
= n log (n – 1) – log (n – 1) output of the program is 2016.
= n log (n – 1) 16. (d)
\ By neglected negative terms P[a] will have the maximum value of the array, when a = b.
17. (a)
= Q(n log n)
main()
Hence option (c) is correct.
12. (0) Consider each statement in the program Count (3)
(1) int m = 10; //m = 10
(2) int n, n1; Pr(4)
(3) n = + + m; //n = 11 Pr(3) Pr(1) Count (2)
(n = (++m)) will increment by m and it assign to
n:
(n = 11, m = 11) Pr(4)
(4) n1 = m++; //n = 11 Pr(2) Pr(2) Count (1)
n1 = m++; will assign (m) to (n1 ) and then
increment m by one.
Pr(4)
So, (n1 = 11), (m = 12)
Pr(1) Pr(3) Count (0)
(5) n– –; //n = 10
Output will be 312213444.
(n - -), decremented by 1 then (n = 10).
18. (d)
(6) - - n1; //n1 = 10 Dynamic scoping refers to definition of free variable in the
(- - n1) decremented by 1 then (n1 = 10) reverse order of calling sequence.
(7) n - = n1; //n = 0 19. 9
(n- = n1), same as n = (n – n1) = 10 – 10 = 0 2 – 5 + 1 – 7 * 3 Þ 2 – (5 + 1) – 7 * 3
(8) Print f ("%d", n); Þ 2 – 6 – 7 * 3 Þ 2 – (6 – 7) * 3 Þ 2 – (–1) * 3
Þ (2 + 1) * 3 Þ 3 * 3 = 9
Then, the output will be 0.
20. 30
Hence, 0 is correct answer.
The address of i and value of j are passed to the function of
13. (2) In the given program : f. f modifies i’s value to 20. j’s value remains same (as its
String Þ (C + 2[ P ] - 6[ P] - 1) value is passed not the reference). The value printed by the
program will be i + j = 20 + 10 = 30.
0 1 2 3 4 5 6 7 8 9 10 11 21. (c) Before Iteration 1: X^Y=64 res * (a^b)=64
G A T E C S I T 2 0 1 7 Before Iteration 2: X^Y=64 res * (a^b)=64
100 Before Iteration 3: X^Y=64 res * (a^b)=64
0[P] 2[P] 6[P]
Hence, option (c) is verify.
Let, address ((0[P] = 100) = C) 22. 3 Assume base address of array a is 100.
Strlen (C + 2[P] + 6[P] – 1)
= Strlen (100 + ('T ' – 'I') – 1) 3 5 2 6 4
No, putting ACCII value of T and I, then 100 102 104 106 108
87
main foo(ABCDE)
ß
pf (f (100, 5)) 3Þwill be printed
ß c 3 foo(BCDEFGH) print(A)(4)
max (f(102, 4), –2)
ß c 3
ret max (f (104, 3), 3)
ß c 2 foo(CDEFGH) print(B)(3)
ret max (f (106, 2), –4)
ß c 2
return max (f (108, 1), 2)
ß c 1
foo(DEFGH) print(C)(2)
Return 1
23. –5 In function "main ()"
f1 is called by value, so local variables a, b, c of f1 are
customized but not the local variables a, b, c of main print(D)(1)
foo((space)EFGH)
function.
f2 is called by reference. if condition fails & returns controls
int main () { \ DCBA will be pointed
int a = 4, b = 5, c = 6
29. 15 The code is pushing 5 and 10 on stack and then
f1(a, b)
f2(&b, &c) popping the top two elements and printing their sum.
printf ("%d", c-a-b); 30. (a)
} 31. (c)
f2(int *a, int *b)
{
1 2 3 4 10
int c;
c = * a; c 5
* a = b; [will change 'b' value of main to c value of main]
*b = c; [will change 'c' value of main to c value of f2]
}
24. (b) The loop terminater when r < y. so, r < y is one post S
condition.
In each iteration q is incremented by 1 and y is
subtracted from r. Initialvalue of r is x. P
x x After *P = '0', array will become
So, loop iterates times and q will be equal to
y y
and r = x% y Þ x = qy + r
1 2 0 4
So, (b) is correct answer.
25. (c)
26. (c)
27. 51 Recurrence Relation is
f(n) = 1, if n = 1
S
n -1
f(n) = 1 + å f (k).f (n - k) if n > 1 and we are pointing string S which is 1204.
k =1 32. 10 j and k will be evaluated to 2 and – 1 respectively.
In for loop:
n 1 2 3 4 5 6 When i = 0; 1 time printed (– 1)
When i = 1; 1 time printed (0)
f (n) 1 2 5 15 51 188
When i = 2; 3 times printed (1, 1, 1)
When i = 3; 3 times printed (2, 2, 2)
28. (d) When i = 4; 2 times printed (3, 3)
\ on the whole printf is executed 10 times
88
33. 230 P1 will be having no error, thus P will be more accurate
In function main x will be updated as follows Option (c)
x = x + f1 () + f 2 () + f 3 () + f 2 ()x n(n - 1) / 3 (n - 2) / 2
¯ ¯ ¯ ¯ ¯ P= ´
1 26 51 100 52 P1 P2
Note: static variable in f 2 ( ) will be initialized only once There is possibility of truncation in P1, the accuracy
& it retains value in between function calls. of P is less
34. (b)
g(6) 38. (d) When j = 50, j (50), the function goes to an infinite
loop by calling f(i) recursively for i = j = 50
39. 9 435 Þ 110110011
num > > 1
g(5) Thus a num is shifted one bit right every time while
g(3)
loop is executed.
g(2)
While loop is executed 9 times successfully, and 10th
g(2) g(0)
g(4) time num is zero.
g(1) g(–1) g(1) g(–1)
\ Count is incremented 9 times.
g(3) g(1) 40. 1.72 to 1.74
g(0) g(–2) g(0) g(–2) x 1.5
g(2) g(0)
g(0) g(–2) f(g) + q Þ + =x
2 x
g(1) g(–1)
x2 + 3
Þ = x Þ x2 + 3 = 2 x2
g(0) g(–2) 2x
Þ x2 = 3 Þ x = 1.73
Total calls = 25 41. 7 P (x) = x5 + 4x3 + 6x + 5
35. (d) We concentrate on following code segment: = x3 (x2 + 4) + 6x + 5
® int *Pi = & i ; Now using only one temporary variable ‘t’
Nothing wrong as ‘Pi’ is declared as integer pointer (i) t = x * x (Evaluate x2 and store in memory)
and is assigned the address of ‘i’ which is an “int”. (ii) t = t + 4 (Evaluate (x2 + 4 ) and store in memory)
® scanf (“%d”, Pi); (iii) t = x2 (Retrieve x2 from memory)
We know that scanf ( ) has two arguments: (iv) t = t * x (Evaluate x3 and store in memory)
First is control string (“%d” in this case), telling it (v) t = t * (x2 + 4) (Evaluate x3 (x2 + 4) and store in memory)
what to read from the keyboard. (vi) t = 6 * x (Evaluate 6x and store in memory)
Second is the address where to store the read item. So, (vii) t = t + 5 (Evaluate (6x + 5) and store in memory)
‘Pi’ refers to the address of “i”, so the value scanned (viii) t = t + x3 (x2 + 4) (Retrieve x2 (x2 + 4) from memory
by scanf ( ) will be stored at the address of ‘i’. and evaluate {x3 (x2 + 4) + 6x + 5}
Above statement is equivalent to: For the above steps, total number of arithmetic operation
scanf (“%d”, & i); is 7
Finally the best statement: i.e., 4 multiplications and 3 additions.
printf (“%d\n”, i + 5); 42. (b) The return value of the function is q (n2 log n)
It prints the value 5 more than the value stored in i. n
So, the program executes successfully and prints the The outer for loop goes + 1 iterations.
2
value 5 more than the integer value entered by the The inner for loop runs independent of the outer
user.
36. (c) One-sixth of the product of the 3 consecutive integers. loop.
37. (b) P = nC3 n
And for each inner iteration, gets added to k.
n(n - 1)(n - 2) 2
= n
6 \ × # outer loops × # inner loops per outer loop.
2
If we multiply, n, (n – 1) and (n – 2) together, it may go # Inner loops = q (log n) [Q 2q(log n) = q (n)]
beyond the range of unsigned integer. (\ option (a)
and (d) are wrong) n én ù
For all values of n, n(n – 1)/2 will always be an integer \ ´ ê + 1ú .q(log n) = q(n2 log n)
value, But for n(n – 1)/3, it is not certain. 2 ë2 û
Take options (b) 43. (b) Return value f (p, p) if the value of p is intialized to
n(n - 1) / 2 (n - 2) / 3 5 before the call.
P= ´
P1 P2 Since, reference of p is passed as 'x'
89
Thus, any change in value of x in f would be
reflected globally. 49. (b)
The recursion can be broken down as
5 5
f ( x, c )
6 4
x * f ( x, c )
7 3
x * f ( x, c )
8 2
x * f ( x, c )
5 + 4 + 3 = 12
9 1
x * f ( x, c ) 50. (d) In a given program we take the test cases and apply.
First take T1, if all value equal means
44. (d) Link to activation record of closest lexically enclosing block
a=b=c=d
in program text. It depends on the static program text So, due to T1, a = b condition satisfied and S1 and S4
45. (c) In switch case statements, there can be more cases, executed.
which case satisfied the condition will be executed, so
So, from T2 when all a, b, c, d distinct.
we have to add break statement after every case in
switch. If there is no break statement then all switch S1, S2 not execute, S3 execute.
cases will be executed and default case will also be from T3 when a = b then, S1 execute but c = d so, S2 not
executed. execute but S3 and S4 execute but we have no need of T3
In the given program, there is no error so, case A is because we get all result from above two.
executed then case B and then case C and then case D By using T4. If a! = b and c = d
and E and finally the default case will be executed and So, S1 not execute and S2 and S4 execute so all of S1, S2,
print. S3, S4 execute and covered by T1, T2 and T4.
46. (d) * p points to i and q points to j */ 51. (b) Let AX, BX, CX be three registers used to store results
void f(int *p, int *q) of temporary variables a, b, c, d, e, f.
{ a(AX) = 1
b(BX) = 10
p = q; /* p also points to j now */
c(CX) = 20
*p = 2; /* Value of j is changed to 2 now */ d(AX) = a(AX) + b(BX)
} e(BX) = c(CX) + d(AX)
47. (c) 2011 charc[] = "GATE2011"; f(AX) = c(CX) + e(BX)
p now has the base address string "GATE2011" b(BX) = c(CX) + e(BX)
char*p =c; e(BX) = b(BX) + f(AX)
p[3] is 'E' and p[1] is 'A'. d(CX) = 5 + e(BX)
p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4 return d(CX) + f(AX)
So the expression p + p[3] - p[1] becomes p + 4 which is Thus, only 3 registers can be used without any
base address of string "2011 overwriting of data.
48. (d) 52. (c) f() is a recursive function which adds f(a+1, n–1) to *a if
*a is even. If *a is odd then f() subtracts f(a+1, n–1) from
*a. See below recursion tree for execution of f(a, 6).
f(add(12), 6) /*Since 12 is first element. a contains
address of 12 */
|
|
12 + f(add(7), 5) /* Since 7 is the next element, a+1
contains address of 7 */
|
|
7 – f(add(13), 4)
|
|
13 – f(add(4), 3)
|
|
1+1=2
90
4 + f(add(11), 2) 56. (d) The option chosen is
| ?1 is ((c = getchar ( ))! = ‘\n’)
| ?2 is putchar (c);
11 – f(add(6), 1) Because the operator ‘1=’ has higher priority than ‘=’
| operator so according to this C = getchar () should be
| contained in brackets and when the string is reversed
6+0
then the function putchar (c) is used so that the characters
So, the final returned value is
can be printed.
12 + (7 – (13 – (4 + (11 – (6 + 0))))) = 15
53. (b) The program calculates n th Fibonacci Number. The 57. (a) 1. Statement is false since global variables are
statement t = fun ( n-1, fp ) gives the (n-1)th Fibonacci required for recursions with static storage. This
number and *fp is used to store the (n-2)th Fibonacci is due to unavailability of stack in static storage.
Number. Initial value of *fp (which is 15 in the above 2. This is true
program) doesn't matter. Following recursion tree shows 3. In dynamic allocation heap structure is used, so
all steps from 1 to 10, for exceution of fun(5, &x).
it is false.
(1) fun(5, fp) 4. False since recursion can be implemented.
/ \ 5. Statement is completely true. So only 2 & 5 are true.
(2) fun(4, fp) (10) t = 5, f = 8, *fp = 5 Hence (a) is correct option.
/ \
58. (d) From the statement j = j*2 in the code we get to know
(3) fun(3, fp) (9) t = 3, f = 5, *fp = 3
/ \ that j increases in power of 2’s. Let us say that this
(4) fun(2, fp) (8) t = 2, f = 3, *fp = 2 statement execute x times then, according to the question
/ \ for while loop
(5) fun(1, fp) (7) t = 1, f = 2, *fp = 1 2^x < = n
/ Therefore, x < = log2n
(6) *fp = 1 And also for termination of while loop there will be an
extra comparison required. Thus, total number of
54. (a) The operator “?:” in C is the ternary operator which means comparisons = x + 1
that, if the expression is exp 1? exp2 : exp3, so it means, if
exp1 is true then exp2 is returned as the answer else exp3 = ëê log 2 n ûú + 2
is the required answer. 59. (d) We follow, the following steps to obtain the value of f(5)
So, in the given expression let us consider x = 3, y = 4,
z = 2, f(5)
Then, expression becomes a r=5
= (3 > 4)? ((3 > 2)? 3:2): ((4>2?4:2) f(3) + 2 = 18
From this, we get that 3>4 is false so we go for the else
part of the statement which is 4>2 and is true thus, the ­
answer is 4, the true part of the statement. f(2) + 5 = 16
55. (b) The program gets executed in the following manner ­
Graphical Representation
f(1) + 5 = 11
c 4 b 1000 a 2000 ­
f(0) + 5 = 6
1000 2000 3000
­
1
x 4 py 1000 ppz 2000 So, f (5) = 1 + 5 + 5 + 5 + 2 = 18
5000 6000 7000 60. (c) S2: May generate segmentation fault if value at pointer
Px or Py is constant or Px or Py point to a memory location
Now, considering
that is invalid. And S4 : May not work for all inputs as
int y, z;
arithmetic overflow can occur.
**ppy + = 1; z = *ppz = 6
*py + = 2; y = *py = 6 61. (d) Both functions work1 & work 2 performs the same task,
x=4+3=7 therefore S1 is true.
return x + y + z; In S2 it is asking about improvement in performance i.e.
and reduction in CPU time. When compared work2 will reduce
c = 4; b & c; a = &b; the CPU time, because in work1 a[i+2] is computed twice
printf (“%d”, f(c, b, a)), but in work2 a[i+2] is computed once and stored in t2,
From the code, and then t2 is used. When we consider the performance
The output is printed as 6 + 6 + 7 = 19. in terms of reduction in CPU time, S2 is correct.
91
62. (b) S1 : Yes the compiler will generate a temporary 69. (d) When a function is called without being defined, C.
nameless cell & initialize it to 13 and pass to Compiler assumes if to reterm “ Int” but here (800) is
swap. returning “ double” and hence the compiler will throw
S2 : No error type mis-match error. So it is a compiler errors.
S3 : No error 70. (a)
71. (c) This is an implementation of Euclid's algorithm to
S4 : Program will print 13 and 8
find GCD
S5 : False. Here if m > n, then m = m – n
Hence (b) is correct option. m < n, then n = n – m
63. (b) Object Oriented Programming (OPP) is a programming Let take X = 24, Y = 9
paradigm. The language is object oriented as it use Then m = 24, n = 9
objects. Objects are the data structures that contain data iteration m n
fields and methods together with their interactions. 1 24 – 9 = 15 9
The main features of the Programming techniques are 2 15 – 9 = 6 9
3 6 9–6=3
1. data abstraction 4 6–3=3 3
2. encapsulation Here m = n, so n returned
3. modularity Which is GCD (Greatest common divisor) of X&Y
Hence (c) is correct option.
4. polymorphism
72. (c) Here we take let x = 16
5. inheritance Loop will stop when x – y = 0 or > 0
Therefore, the essential features are given by statements Iteration X Y
(i) and (iv). 1 (16 + 1) / 2 = 8 16 / 8 = 2
64. (c) Languages needs declaration of any statement that we 2 (8 + 2) / 2 = 5 16 / 5 = 3
3 (5 + 3) / 2 = 4 16 / 4 = 4
write before its use thus, the common property of both
Here X=Y
the languages is that both are declarative. Then take X which is 4.
65. (c) An abstract data type is a mathematical model for a certain (m)1/2 = 4=(16)1/2
class of data structures that have similar behaviour. An Hence (c) is correct option.
abstract data type is indirectly defined by the operations 73. (c) The iterations that the given code will undergo are:
and mathematical constraints thus, is a user defined data From the given conditions we does not take into account
type, not a system defined, that can perform operations when n = 1
defined by it on that data. Iteration 1
N = 1 + 1 = 2 therefore, i = 2
66. (c) Syntax to declare pointer to a function => datatype Iteration 2
(*pointer_variable)(list of arguments) N = 2 + 2 = 4 therefore, i = 3
To make a pointer to a function => pointer_variable = Iteration 3
function_name N = 4 + 3 = 2 = 4 therefore, i = 4
Note: don't use parenthesis of the function. Hence, the value returned after three iterations is 7
When, i = 4 and it also fulfill the condition of n>=5
To call (invoke) the function => pointer_variable (list of
74. (d) Sum has no use in foo(), it is there just to confuse.
arguments)
Function foo() just prints all digits of a number. In main,
67. (d) P. Functional programming is declarative in nature, there is one more printf statement after foo(), so one
involves expression evaluation, & side effect more 0 is printed after all digits of n.
free. From the given code it is found that foo is a recursive
Q. Logic is also declarative but involves theorem function and when the function foo (a, sum) is called
proving. where a = 2048 and sum = 0 as per given conditions.
R. Object oriented is imperative statement based Then, the execution of the function takes in the following
& have abstract (general) data types. manner.
S. Imperative : The programs are made giving i) k= n % 10 => modulus operator will return the
commands & follows definite procedure & remainder. for example, if n=2048, then 2048 %10 will store
sequence. the value 8 in k.
Hence (d) is correct option. ii) j is declared as integer datatype in foo function.
68. (d) Let us consider below line inside the for loop. Therefore after division if the result contains decimal
P[i] = S[length – i]; part, it will be truncated and only the integer part will be
For i = 0, P[i] will be S[6 – 0) and S [6] is ‘10’. stored in j. For example if j=2048/10, (actual result = 204.8)
So, P[0] becomes ‘10’. It does not matter what comes in then 204 will be stored in j.
P[1], P[2] ........ as P[0] will not change for i > 0. iii) The sum variable declared in the main function will
Nothing is Printed if we print a string with first character not be used by the foo function.
‘10’.
92
75. (c) The main goal of structured Programming is to get an 80. (c) The advantages of shared dynamically linked libraries
understanding about the flow of control in the given include.
program text. In structure programming various (a) smaller size of executable since less data
control structures such as switch case. If then – (b) lesser overall page fault rate.
else, while, etc. (c) No need for re-linking if newer versions of
Allows a programmer to decode the flow of the libraries are there.
program easily. But since compilation time doesn't include
76. (d) Why a, b and c are incorrect? a) call swap (x, y) will linking so a long linking time required during
not cause any effect on x and y as parameters are runtime in DLL ' s so slow startup.
passed by value. b) call swap (& x, & y) will no work Hence (c) is correct option.
as function swap() expects values not addresses
81. (d) 1. Px = newQ();
(or pointers). c) swap (x, y) cannot be used but
reason given is not correct. Here the function takes 2. Qy = newQ();
the arguments by value. 3. Pz = newQ();
Option (a) sends parameter by value but only the 4. x : f (1); print 2 # i = 2
local variable a & b will be exchanged but not the 5. ((P) y) : f (1);
actual variables x & y so incorrect. 6. z : f (1) print 2 # i = 2
Option (b) is incorrect sending address of x & y. but line 5. will print 2 because typecast to parent
Option (c) swap (x, y) is usable there is no need to class can't prevent over ridding. So function f(1) of
return. class Q will be called not f (1) of class P.
Option (d) is the opposite statement of option (a), it Hence (d) is correct option.
says that the values are passed by value so won't 82. (d) In static scoping the variables are initialized at
swap so the option is correct. compile time only
77. (b) Balanced parenthesis in an equation are such that So i = 100 &j = 5
the no. of opening and closing parenthesis and in P (i + j) = P (100 + 5) = P(105)
correct order should be there. We can check So x = 105
balancing using stack. When we get any opening x + 10 = 105 + 10 = 115
parenthesis, then we push that in the stack & if we So 115 & 105 will be printed.
get a closing one, then we pop the stack. After the Hence (d) is correct option.
complete scanning of input string if stack is found 83. (b) In dynamic scoping, the local values are considered
empty then the arithmetic expression is balanced. & variables are initialized at run time.
78. (b) The function is rewritten as Here initial value of s Since x = i + j & in P (x)
increments every time with a factor of (p)x/i i = 200 &j = 20 x = 200 + 20 = 220
& printing (x + 10)
Loop 9. = i + j + 10
P S = 10 + 5 + 10 = 25
Counter (i)
Hence (b) is correct option
1 x 1+x
84. (a) Here X is the global variable so still 5.
2 x * x / 2.1 = x / 2.1 1 + x + x 2 / 2
2
Here this is global X whose xy has been changed to
x 2 x x3 6 so 6 is printed
3 * = .2.1 1 + x + x 2 / 2!+ x 3 / 3! 12 66
2 3 3
x3
First x = 5
4 * x / 4 = x 4 / 4! 1 + x + x 2 / 2!+ x 3 / 3!+ x 4 / 4! Then by function P(&x)
3!
X = 5 +2 = 7
Thus it can be checked for every value. Here the
Then by function Q(x)
assumption is that the value of y is very large so y
z =z+x
approaches infinity So the series 1+ x + x2 / 2! + x3
/ 3!...till infinity will have infinite terms & from our = 7 + 5 = 12
previous knowledge we know that this series is Here x is global variable so still it is 5.
expansion of ex (exponential series) so. Return to function P(&x)
1 + x + x2 / 2! + x3 / 3!...........till infinity = ex Y = 7–1= 6
Hence (b) is correct option. print x =7
79. (c) (a) True for statically typed languages where each return to main
variable has fixed type. Similarly (d) is also correct. Print x = 6
(b) True, in un-typed languages types of values Here this is global x whose *y has been changed
are not defined. to 6 so 6 is printed.
But option (c) is false, since in dynamically 85. (d) In call by reference, the called function is working
typed language variables have dynamically with the memory location of the passed variables.
changing types but not that they have no type. So, any update to the variables are immediately
Hence (c) is correct option. effective.
93
In call by value-result, the called function is working 94. (c) The amount of memory required to store a structure
with a copy of the passed variable. On return, the variable is the sum of the sizes of all its members. But
updated values are copied back to the original in the case of union, the amount of memory required
variables. is the amount required by its largest member. Therefore
So during a function execution if an exception u, which is a union member of the struct, occupies
happens, in call by value-result, the passed variables only 8 bytes of memory, because the largest memory
won’t be getting update values. is 8 bytes consumed by long z;. Another member in the
86. (b) (a) There is no such restriction in C language struct is short s [5], this will occupy 10 bytes of
(b) True as soon as a function is called its memory ( 2 bytes x 5).
activation record is created in the function 95. (c) A token is strictly a string of characters that the
stock. compiler replaces with another string of characters.
(c) False. In C, variables are statically scoped, not
Token are defined as a sort of search and replace.
dynamically.
Token can be used for common phrases, page elements,
(d) False. The activation records are stored on the
same stack. handy shortcuts, etc. Token are defined with either the
87. (b) Note the order in which parameters are passed. define or replace tags and can use single or paired tag
Inside func1(), x will actually refer to y of main (); syntax.
and y and z will refer to x of main (). The statement Examples
y = y + 4; will result in 14 and statement z = x + y <define%bgcolor%#ccffcc>
+ z will make z = 3 + 14 + 14 = 31 (because y and <define (c) & copy;>
z point to same variable x of main). Since z refers to <define [email] joe@ cool.com>
x of main(), main will print 31. <:replace [mailto] <a hre =
88. (c) P1 : Here the function is returning address of the mailto: [email] > [email] </a>:>
variable x (& x ) but the return type is pointer to 96. (a) Count is static variable in incr(). Statement static int
integer not address. So incorrect. count = 0 will assign count to 0 only in first call.
P2 : *px = 0 directly assigned a value but still px Other calls to this function will take the old values
doesn't point to any memory location, so memory of count. Count will become 0 after the call incr(0).
initialization or allocation should be done before. So Count will become 1 after the call incr(1) Count will
incorrect.
become 3 after the call incr(2) Count will become 6
P3: Correction made in P2, memory pre allocated, So
after the call incr(3) Count will become 10 after the
correct.
call incr(4)
Hence (c) is correct option.
89. (d) n = 10 given but not passed to D. In D, n = 3 &W(n) Arrays
increments by 1. So n=n+1=4.
Hence (d) is correct option. 97. (5) To find out the smallest index i such that A[i] is 1 by
90. (c) The process of assigning load addresses to the probing the minimum number of location in A. Assume
various parts of the program and adjusting the code i be the index of first element and j be the index of last
and date in the program to reflect the assigned element.
addresses is called relocation. Suppose any default
By using the binary search algorithm with some
location say x is added to all the addresses in the
code leading to correct references. So, it is assembler changes.
whose output must distinguish those portions of Now,
the instructions and addresses that are relocatable. é low+high ù
91. (d) X - A pointer is assigned to NULL without freeing Find middle element of Array = A ê ú
ë 2 û
memory so a clear example of memory leak. Y -
Assuming ‘A’ is an array of 31 elements with ‘1’ and if
Trying to retrieve value after freeing it so dangling
pointer. Z - Using uninitialized pointers. it is ‘1’, we check the left part recursively and if it is ‘0’,
92. (c) X. depth first search is done in stack. we check the right part of the array recursively.
Y. breadth first search is done in queue Which take log2(31) comparisons in the worst case,
Z. sorting is done in a heap. so the total worst case probes is
93. (a) Aliasing describes a situation in which a data Þ Ceil (log231) = 5
location in memory can be accessed through different (Q Ceil means compute fast log base 2 ceiling)
symbolic names in the program. Thus, modifying the
data through one name implicitly modifies the values 5 probes performed by an optimal algorithm.
associated to all aliased names, which may not be 98. (3) The initial contents of the Array is :-
expected by the programmer. As a result, aliasing
makes it particularly difficult to understand, analyze 3 5 1 4 6 2
and optimize programs. Thus, multiple variables
having same memory location. 0 1 2 3 4 5
94
For first loop : i very from (0 to 4), then the contents of 99. 5 1 2 5 14 ¥ 2 5 14
the Array are :
3 4 6 23 3 4 6 23
10 12 18 25 10 12 18 25
i = 0 5 3 1 4 6 2 31 ¥ ¥ ¥ 31 ¥ ¥ ¥
i = 1 5 3 1 4 6 2

Ü
2shift
i = 2 5 3 4 1 6 2 2 45 14 2 4 5 14
i = 3 5 3 4 6 1 2 3 6
18 23 3 ¥ 6 23
Ü
i = 4 5 3 4 6 2 1 10 12 ¥ 25 10 12 18 25
31 ¥ ¥ ¥ 31 ¥ ¥ ¥
For second loop : i very from (5 to 1) then the contents 2 shift

Ü
of the Array are : 2 4 5 14
3 4 18 23
i = 5 5 3 4 6 2 1 10 12 25 ¥
i = 4 5 3 4 6 2 1 31 ¥ ¥ ¥

i = 3 5 3 6 4 2 1
100. 0 C i 0 1 1
i = 2 5 6 3 4 2 1
i = 1 6 5 3 4 2 1
20 something

{z = 1
Now, since done = 0, then the for loops will execute
for i = 0 + 0.3 k=0
again.
do z=1
For first loop : again i is very from 0 to 4, then the
contents of the Array are : z ¬ z2 mod z=1 z=4 z=0 z=0
if c[i] = 1 c [0] = 1 c [1] = 0 c [1] = 1 c [1] = 1
i = 0 6 5 3 4 2 1 z ¬ 2 × z mod 8 z = 2 z=0 z=0
i = 1 6 5 3 4 2 1 end
i = 2 6 5 4 3 2 1 return 2
}
i = 3 6 5 4 3 2 1
i = 4 6 5 4 3 2 1 101. 140
a 10 20 30 40 50
For second loop : Again i is very from (5 to 1) then the
contents of the array are :

i = 5 6 5 4 3 2 1
i = 4 6 5 4 3 2 1 p
i = 3 6 5 4 3 2 1
i = 2 6 5 4 3 2 1
i = 1 6 5 4 3 2 1

ptr after ptr + + Þptr – p = 1


The value of done is still "zero". Hence the for loop (pointer arithmetic)
will execute again. ptr
** ptr = 40
First for loop : \ printf ("%d%d", p + r – p, p + r) will print 140
102. (c)
This time there will be no change by the for loop.
103. (a) Clearly the code segment: for
Then the value of done is '1', hence the loop terminate (i = 0; i < size; i + +) Y = Y + E [i]
here, then the final contents of the Array are : Computes the sum of all element in the array E.
Note : As the array E may contain negative elements,
6 5 4 3 2 1 the sum of elements of subarray of E may be greater
than the sum of all elements in the array E.
Hence, the output of the program array [3] is '3'. Hence Now, comes the nested i, j, k loops:
answer is 3. ‘i’ changes the starting of the subway.
95
‘j’ changes the end position of the subarray. maximum one time. Similarly for (2) array element of
‘k’ loop is used to compute sum of elements of the A has updated with array element of new C less than
particular subarray. or equal to one time.
The sum of subarray is stored in z and is compared to d
the earlier max sum Y. Finally the larger sum gets stored A a c b dc d e
in Y.
i = 0, j = 14
i = 0, j = 2 old C b c a
i = 0, j = 1 New C c d a
i = 0, j = 0 For (3)
Now, for (3), when i = O, value of A which old C[2] i.e.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ‘a’ and replace with new C[2] i.e. also ‘a’, So, no changes.
size = 15 when i = 1 value of array. A[1] = ‘b’
i = 2, j = 2 Match with Old C [O] = ‘b’ and replace with new C [O]
= ‘C’.
i = 2, j = 3
Now, A [1] = ‘C’, which equal with ‘d’. Next element of
i = 2, j = 4 old C [1] = ‘C’.
..
.. So, replace again with new C [1] = ‘d’.
i = 2, j = 14 . Now, we can say here in Array A [1] value replace with
This can be easily seen how sum of subarray is new C [O] value, and that new C [O] value replace with
computed with different values of i and j. next new C [1] value.
104. (a) For the given pseudo code, each element of the a
upper triangle is interchanged by the corresponding b a Old C a b c
a b c d e b a c
element in lower triangle and later the lower New C
triangular element is interchanged by the upper Similarly for (4) here 2 times replacement for A[O] with
triangular once. Thus the final output matrix will be element new C [O] and new C [1]. Updating of new C
same as that of input matrix A. value with another new C value is calling flow here,
105. (c) Analyzing the flow in the function. Fix an 'i' in the Hence the option (c) is correct.
outer loop. 107. (b) Dynamic programming can be successfully used, i.e
Suppose A[i] = old c[0], then A[i] is set to new c[0] n rows for n elements &w + 1 columns.
[j = 0]. Each row is filled on the basis of its previous rows
Now suppose that old c[1]= new c[0] & the (j – ai) – th column.
Þ A[i] = old c[1] (after the j = 0th iteration) If any of them is 0 then X[i, j] should be zero.
which means in the next iteration with j = 1; A[i] This require X[i, j] = X[i – 1, j] Ú × [i – 1, j – ai]
again gets changed form new c[0] (or old c[1]) to Hence (b) is correct option.
new c[1] which is incorrect. 108. (c) The algorithm of dynamic programming has n rows
The current loop should "break" after a match is & w columns.These would be filled dynamically
found and A[i] is replaced. depending upon previous rows &columns. So X[n,w]
The inner loop should look like will be filled in the last & this would give the result.
for (int j = 0; j < 3, j + +) If X[n,w] = 1 i.e. TRUE, then we know that there is
if (A [i] = = old c[j]) subset present whose sum = integer w.
A[j] = new c [j]; Otherwise subset not present.
break; Hence (c) is correct option.
The above flow is exposed by the test cases 3 & 4 109. (c) We are given that
only. For test 1 and test 2, the expected output is i = 0; j = 9;
produced by the code. k = (i + j)/2
Test 3 Expected A after replacements I iteration
= "abcde" ® "acdde" Let consider x = 4
result from code = "abcde" ® "addde" Now, k = (0 + 9)/2 = 4
Test 4 Expected "abcde" ® "bacde" Therefore, y[4] < x is true
result form code "abcde" ® "abcde". ® i = 4 and j = 9
\ Answer is 3 and 4 only (c) II iteration
d a b As i = 4
106. (b) A a b c d e Now, k = (4 + 9)/2 = 6
Therefore, y[6] < x is true.
old C a b c ® i = 6 and j = 9
New C d a b III iteration
For (1) As i = 6
Here, when the element of array A and old C match, Now, k = (6 + 9) / 2 = 7
we replace that array element of A with array element Therefore, y[7] < x is true
of new C for every element of A array update occurs ® i = 7 and j = 9
96
IV iteration Here the stack will be fuel if both top 1 & top 2 are
As i = 7 at the adjacent index values i.e., their difference is 1.
Now, k = (7 + 9)/2 = 8 So top 1 = top 2 – 1
Therefore, y[8] < x is true Here (d) is correct option.
® i = 8 and j = 9 117. (a) A is an array of pointers to int, and B is a 2D array
V iteration * A [2] = Can take a pointer
As i = 8 * A [2] [3] = Can take an Int
Now, k = (8 + 9) / 2 = 8 * B [1] = It is the base address of array and it
Therefore, y[8] < x is true cannot be changed, as array in C is a Constant
® i = 8 and j = 9 pointer. We can not make B the point some
As we have proved that 4[8]! = x & i < j, the program other array. So this is false.
will remain forever in loop. * B[2][3] = It can take an integer.
110. (a) The correction in the program is needed as in the So, option (a) is correct.
above solution we can see that the program cannot 118. (b) If (!A[J]) condition was as follows
work properly. (!A[J]) = = 0)
Now, when y[k] < x Then, it prints a non zero value in the array
Increase 1 to k + 1 {m|m £ n, (Ei) [m = i2]}
Otherwise, decrease j to k –1 119. (b) Given is a [40] [50].
This will ensure that in the while condition, element In a [40] [50],
at k position will be checked for equality. Row = 40
111. (b) One possible way to do this is we select first element Column = 50
The address is 4050.
as max & also min. Then we compare it with all others.
120. (a) From the given declaration, it is obtained that s is an
At most this would take 2n comparisons during linear array denoted by s[10], containing 10 elements which
search. But if we use divide & conquer as for merge are pointers indicated by the symbol, *, to structure
sort we have. of type node as defined by struct node statement.
121. (a) From the given conditions it can be clearly concluded
T(n) 2T(n/2) + 2 for n > 2 that, the given sequence rotates s left by k positions.
T(n) = 3n/2 – 2 122. (a) Count = 3
= 1.5n – 2 q[1] = 7 q[2] = 3, q[3] = 9
p[7] = 1 p[3] = 2, p[9] = 3
112. (d) The condition given in the FOR loop is
(b) The first count elements of q contain values (i) such
z[i] = {x[i] Ù ~ y[i]} Ú (~ x[i] Ù y[i]) that set (i) has been called.
Now, we clearly can see that the condition reflect the (c) Suppose p[i] has a value < = 0 or > count, then to
XOR operation and in the XOR operation set set (i) return false.
obtained is (X Ç Y¢) È (X¢ Ç Y) Otherwise consider q[p[i]]. Using (b), q[p[i]] contains
Since, the formula is X Ç Y¢ = X¢ – Y a value (i) such that set (i) has been called. Since
Result obtained is (X – Y) È (Y – X) set (i) has not been called, q[p[i]] cannot be equal
113. (d) Since the matrices are stored in array, there is no to (i) and is set (i) returns false.
dependence of time complexity on row major or 123. (a) (1) f[n] ! = 0
column major. Here only the starting address is or
known & on the basis of indexes the next memory Expression like f(n)3 0 etc.
locations are calculated. (2) f[n]
Hence (d) is correct option. (3) f[n] = t
114. (c) The condition (i) is true if the last inserted element in c[] (b) O(n) or (n + 1)
is from a[] and condition (ii) is true if the last inserted Alternately
element is from b[]. 0 (n3) in the log-cost model.
124. (a) 47
115. (a) We have to store frequencies of scores above 50. That (b) 22210000 1111
is number of students having score 51, number of Arrangements that satisfy one of the following two
students having score 52 and so on. For that an array of constraints are also correct.
size 50 is the best option. (i) 1’s in the first 4 positions and no 2 in the last 3
116. (d) Let take MAXSIZE =10 positions
or
1 2 3 4 5 6 7 8 9 10
1111 010
22020
all ones no two’s
(ii) 1’s in the last 3 positions and no zero in the first 4
Stack 1 Stack 2 positions
or
1212 111
Top 1 Top 2 00020
no zeros all ones
97
Stacks pushed and popped. Plus one more y for the time
interval between the push of ith element and the
125. (b) Viable prefixes appear only at the top of the stack and (i+1)th element. So,
not inside is valid in shift reduce parsing, because we S(i) = y + 2(n–i) . (y + x)
will write the shift reduce parsing program for every = y + 2(n – i) . z
variable such that if any variable contain more than = y + 2nZ – 2iz
one possibility, it will choose the correct production. Where z = (y + x).
126. (c) The queue can be implemated where ENQUEUE takes é s(i) ù
a sequence of three instructions (reverse, push, Average, stack-file will, A= å êë n úû
reverse) and DEQUEUE takes a single instruction
(pop).
(
n A = ny + 2 . n . n . z – 2 . z . å i )
127. (a) The algorithm for evaluating any postfix expression é n(n + 1) ù
n A = ny + 2 . n . n . z – 2 . z . ê
is fairly straightforward: ë 2 úû
1. While there are input tokens left n A = ny + 2 . n.n.z – z (n.n) – n.z
o Read the next token from input. A = y + 2 nz – (n + 1) . z = y + (n–1). z
o If the token is a value = y + (n – 1)(y + x) = n(x + y) – x.
+ Push it onto the stack. 130. (a) A stack machine implements registers with a stack.
o Otherwise, the token is an operator The operands of the arithmetic logic unit (ALU) are
(operator here includes both operators, and functions). always the top two registers of the stack and the
* It is known a priori that the operator takes n arguments. result from the ALU is stored in the top register of
* If there are fewer than n values on the stack the stack. 'Stack machine' commonly refers to
(Error) The user has not input sufficient values in the computers which use a Last-in, First-out stack to
expression. hold short-lived temporary values while executing
* Else, Pop the top n values from the stack. individual program statements. The instruction set
* Evaluate the operator, with the values as arguments. carries out most ALU actions with postfix (Reverse
* Push the returned results, if any, back onto the stack. Polish notation) operations that work only on the
2. If there is only one value in the stack expression stack, not on data registers or main
o That value is the result of the calculation. memory cells. The same opcode that handles the
3. If there are more values in the stack frequent common case of an add, an indexed load,
o (Error) The user input has too many values. or a function call will also handle the general case
Let us run the above algorithm for the given expression. involving complex subexpressions and nested calls
First three tokens are values, so they are simply pushed. 131. (b) 2 stacks . You can even simulate a queue using only
After pushing 8, 2 and 3, the stack is as follows 8, 2, 3 one stack. The second (temporary) stack can be
When ^ is read, top two are popped and power(2^3) is simulated by the call stack of recursive calls to the
calculated 8, 8 insert method.
When / is read, top two are popped and division(8/8) is The principle stays the same when inserting a new
performed 1 element into the queue:
Next two tokens are values, so they are simply pushed. • You need to transfer elements from one stack to
After pushing 2 and 3, the stack is as follows 1, 2, 3 another temporary stack, to reverse their order.
When * comes, top two are popped and multiplication is Then push the new element to be inserted, onto
performed 1, 6 the temporary stack. Then transfer the elements
128. (a) The order in which insert and delete operations are back to the original stack.
performed matters here. • The new element will be on the bottom of the
The best case: Insert and delete operations are performed stack, and the oldest element is on top (first to
alternatively. In every delete operation, 2 pop and 1 push be popped).
operations are performed. So, total m+ n push (n push
for insert() and m push for delete()) operations and 2m
Queues
pop operations are performed. 132. (b) In circular queue next pointer of Rear node pointing to
The worst case: First n elements are inserted and then m the Front Node will lead to insertion in a queue is
elements are deleted. In first delete operation, n + 1 pop always from REAR and deletion is always from FRONT
operations and n push operation are performed. Other node in Q(1) time.
than first, in all delete operations, 1 pop operation is
performed. So, total m + n pop operations and 2n push
Front Rear
operations are performed (n push for insert() and m push Head

for delete()) A B C D
129. (c) Let us represent stack-life, of ith element as s(i). The
ith element will be in stack till (n–i) elements are Hence, option (B) is correct.
98
133. (a) We can use circular array to implement both O(1) time.
134. 256
... n = 16

Therefore, maximum number of iterations will be n2 = 256


135. (a) To compute the worst case time complexity of a
sequence of n queue operations on an initially
empty queue is q (n).
Complexity of a squence of 'n' queue operations =
Total complexity of Enqueue operations (a) + Total
complexity of Dequeue operations (b). Therefore, the level order traversal comes out to be
Total complexity of Dequeue operations (b) £ Total 10, 8, 7, 3, 2, 1, 5
complexity of Enqueue operations Linked Lists
b£a ...(i)
Total complexity of queue operations (a) £ n ...(ii) 138. (c) The time complexity which can put all these operations
Total complexity of n operations = a + b together will be O(N2).
£a+a [From ...(i)] 139. (d) When the while loop ends, q contains address of second
£n+n [From ... (ii)] last node and p contains address of last node. So we
£2 n need to do following things after while loop.
\ Worst Case Time Complexity of 'n' operations is (i) Set next of q as NULL (q->next = NULL).
(a) q (n). (ii) Set next of p as head (p->next = head).
136. (a) FRONT points to the first element that we can remove (iii) Make head as p ( head = p)
and then we increment FRONT REAR points to the first Step (ii) must be performed before step (iii). If we change
empty space in queue so we insert an element and head first, then we lose track of head node in the original
increment REAR so now initially queue is empty F=R=0 linked list.
insert A (AT 0th index) --> increment REAR (REAR =1 140. (b) The function rearrange() exchanges data of every node
FRONT=0) now if we want to delete A we have FRONT with its next node. It starts exchanging data from the first
pointing at that location so we delete A and increment node itself.
FRONT (REAR=1 FRONT=1 queue empty). 141. (a) Answer is not “(b) front node”, as we cannot get rear
137. (d) Initial level order traversal with 10, 8, 5, 3, 2 from front in O(1), but if P is rear we can implement both
enqueue and de-Queue in O(1) because from rear we can
get front in O(1). Below are sample functions. Shows
The pointer points to the rear node:-
Enqueue :- Insert new node after rear, and make rear point
to the newly inserted node:
Struct Node * New Node;
New Node ® Next = Rear ® Next,
Rear ® Next = New Node ;
Rear = New Node;
Dequeue :- Delete the front node, and make the second node
Now, let us insert the values
the front node.
Rear ® Next points to the front node.
Front ® Next points to the second node.
Struct Node * Front;
Front = Rear ® Next;
Rear ® Next = Front ® Next;
Free (front);
142. (d) Membership & cardinality functions takes constt.
time i.e. O(1), but union & intersection require
emparison of 1 element with all the other elements so
10 these two would be slowest.
Hence (d) is correct option.
143. (b) Here the return 1 any 1 of the following should be
correct.
8 5
(a) P == NULL i.e the list is empty (ends)
(b) P ® next = NULL i.e., have one element.
(c) P->data <= p->next ->data i.e., the element is
3 2 1 7 smaller than its next element also. This is true for
whole list. Since &&f(p “next”) is also there.
inserting 7 here So overall it gives that the elements should be in
sorted order.
99
144. (d) Worst case of searching occurs when the element to 151. (c) Let’s take 10 successive key value {1, 2, 3..........10}
be searched is at the end of the list so no. of
comparisons required to search complete list would 1, 2, 3 1 2 3
be n. Hence (d) is correct option.
2
Trees
4
1 3 4
145. (18) A tree with 10 vertices has 9 edges. “1” split
Such that, n = 10, e = (n – 1) = 10 – 1 = 9 2
then, Sdegree (Vi) = 2 [edges (E)] = 2 × 9 = 18. 5
1 3 4 5

6 2 4
5 60 60 10
146. (c) 10 10 15 15 15 15 6 5 6
1 3
10 5 + 60 6 “1” split

8 2 4
150 150 142
* 8 – 7 5 6 7
1 3
147. (a) The in order transversal is as :
left, root, middle, right 2 4 6
\ Nodes are visited in SQPTRWUV order.
148. 6 8
1 3 5 7 8
6 “1” split
9 No (will adjust with 7, 8, 9)
+
3 3
– 10 4
+
2 –1 2
1 2 6 8
+ – – +

1 1 0 1 1 0 1 1
1 3 5 7 9 10
149. (d) R1 ¬ c, R2 ¬ d, R2 ¬ R1 + R2, R1 ¬ e, R2 ¬ R1-R2 Total = “5” split
Now to calculate the rest of the expression we must load 152. (c) Each internal node has n children & so total nodes n
a and b into the registers but we need the content of R2 No. of leaf in them is :
later.
Þ I* (n – 1)
So we must use another Register.
R1 ¬ a, R3 ¬ b, R1 ¬ R1-R3, R1 ¬ R1+R2 Þ I(n – 1)
150. (b) AVL trees are binary trees with the following restrictions. But root can’t produce leaf
(1) the height difference of the children is at most 1. I(n – 1) + 1 = L
(2) both children are AVL trees
n = L – 1/ l + 1
a n=(41 – 1 )/10 +1 =5
/ \
/ \ 153. (c) No. of internal nodes = n
b c Each node has k children
/ \ / So total nk
/ \ / Leaf nodes = nk – n
d e g = n(k – 1)
/ So considering not node also.
/
No. of leaf nodes = n(k – 1) + 1
h
Hence (c) is correct option.
100
154. (a) a + b × c – d ^ e ^ f
Height - 0
a+b×c–d^e f^
a+b×c–def^ ^ Height - 1
Min. Max.
a+bc×–d e f ^ ^ Height - 2
Height Height

abc× –d e f ^ ^
Height - 3
abc× +d e f ^ ^
Total Nodes = 15
a b c × + d e f ^ ^–
the result is obtained. Min. height = floor (log2N)
155. (d) Value initialized by 0. If any root node has left child = floor (log215) = 3
then it adds 1 to the value & move to left child & The maximum height will be when the tree is skew tree, which
if any mode has right child also then also calculated will give rise to height 14 either it will be left or right skewed
the value using recursion & take maximum of both
tree. Hence option (b) is correct.
left & right value is taken.
So we know that height is the largest distance 159. (b) For the given pre-order traversal of a binary search
between root node & leaf. Therefore, this program tree, Pre order is : (Root, left, Right)
calculates heights. Pre order : 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20
Hence (d) is correct option.
In order is : (Left, Root, Right),
156. (d) Consider following rooted trees
a So in order of given sequence is : 2, 6, 7, 8, 9, 10, 12, 15,
16, 17, 19, 20.

c
Then tree will be :
b d

e g 12
f
n=4 8 16
(2n + 1)/3 = 3
No. of leaf nodes = 3 6 9 18 19
Hence (d) is correct option.

4 2 7 10 17 20
157. (a)
Now, post order is : (Left, Right, Root)
1 2 3 4 5 6 Post order will be, 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12
Hence option (b) is correct.
(b)
160. (52) As given that : Search key = 8 bytes
Block size = 512 bytes.
4 7
Block pointer (BP) = 2 bytes.
Then maximum order of B+ tree is
1 2 3 4 5 6 7 8 9 10 Let K is the order then,
K * Bp + ( K - 1)* Key £ Block size
5
K *2 + ( K - 1) *8 £ 512

10 K £ (512 + 8)
1 2 3 4 5 6 7 8 9
10 K £ 520

émù m æ 520 ö
(c) (i) ê ú or K =ç ÷ = 52
ë2û 2 è 10 ø
(ii) m Hence, 52 is correct answer.
161. (a) In B and B+ trees, all the leaf nodes will be at same
Binary Tree and Binary Search Tress
level.
158. (b) Since there are 15 nodes, hence the minimum height of 162. (c) The expression tree for the given post-fix expression
the tree will be 3 (when tree will be balanced). is as follows:
101
168. 19 Let the number of vertices of a binary tree with ‘p’
leaves be n then the tree has–
– (i) p vertices (i.e., leaves) of degree 1
(ii) one vertex (i.e.., root of T) of degree 2
(iii) 'n - p -1' (i.e., interval) vertices of degree 3
+ (iv) n -1edges
\ By Handshaking theorem,
p × 1 + 1 × 2 + (n – p – 1) × 3 = 2 (n – 1)
– 1
* Þ n = 2p – 1
2
= 39 as p = 20
* Þ n – p =19 vertices have exactly two children
5
2 7
6
3 4 169. (b) 71

65 84
New-order of shown expression tree is
–+ 1*7 6^ 2–5*4 3
83
163. 64 69

To fill 7 levels with 7 elements, at each level we have exactly


2 possible options like 1 and 7 for root one corresponding
to making it left skewed and other right skewed. This is 67
valid for all levels upto 6.
Hence, 26 = 64. 170. 199
164. (a) We know that the maximum no. of nodes in a binary Let the number of leaf nodes of a binary tree with ‘n’
tree with (height) h = 2h+1–1. vertices be ‘p’ then the tree has
Here h = 5, then, we easily calculate the h as: (i) ‘p’ vertices of degree ‘1’
(ii) one vertex (i.e. root of T) of degree ‘2’.
h = 25 + 1–1= 64 – 1 = 63 (iii) 'n – p – 1' vertices of degree ‘3’
And the minimum no. of nodes with height h is h + 1. (iv) 'n –1' edges
\h = 5 \ By Handshaking theorem,
p × 1 + 1 × 2 + (n – p –1)×3 = 2(n –1)
L=0 Þn=2p–1
L=1 = 399 as p = 200
\ Number of nodes having exactly two children are
L=2
n – p i.e., 199
L=3 171. (c) For non-trivial solution, we have | A | = 0
L=4
p q r
L=5
i.e., q r p = 0
165. (a) In-order traversal of binary search tree gives ascending r p q
orders and in BST, at every node root element is greater (C1 ® C1 + C2 + C3)
than and equal to all element present in left sub-tree
and less than or equal to all the elements in right sub- 1 q r
tree. 1 r p
(p + q + r) =0
166. 6 1 p q
[R2 ® R2 – R1; R3 ® R3 – R1]
9 p+ q+r=0
1 q r
5 0 r-q p-r
13 17 (or) =0
0 p-q q-r
Þ (r – q)2 – (p – q) (p – r) = 0
1 3 5 7 9 11 13 15 17 Þ p2 + q2 + r2 – pq – qr – pr = 0
Þ (p – q)2 + (q – r)2 + (r – p)2 = 0
Þ p – q = 0; q – r = 0, r – p = 0
167. (a) Þ p = q = r.
102
172. 50 180. (b) To construct a BST from post order we also require
Suppose that ‘k’ is order of the non-leaf node in-order traversal since given the elements are 1 2.......n.
k(8) + (k – 1)12 £ 1024 So their sorted order would be in order. Using both BST
20k £ 1036
can be constructed in a linear scan. So it will take only 4n
é1036 ù
k£ê Þ k £ 51 time.
ë 20 úû 181. (d) For a right skewed binary tree, number of nodes will be
As the order is 51, maximum we can store 50 keys.
173. 1 to 1 2^ (n – 1). For example, in below binary tree, node 'A' will
Value of a + 10b = 1. be stored at index 1, 'B' at index 3, 'C' at index 7 and 'D' at
We can find the subtree with 4 nodes in O(n) time, we can index 15.
use following Algorithm: A
(1) Transverse the tree in bottom up manner and find
size of subtree rooted with current node. \
(2) If size becomes 4, then print the current node. \
int print 4 subtree (struct Node *root) B
{ \
if (root = = NULL)
return 0; \
int l = print 4 subtree (root ® left); C
int r = print 4 subtree (root ® right); \
if ((l + r + 1) = = 4) \
printf (“%d”, root ® data);
return (l + r + 1) D
} 182. (b) It is given that n = 3
174. 110
It takes (log n) time to determine numbers n1 and n2 in n +1
( )
1 2n
Cn
balanced binary search tree T such that
1. n1 is the smallest number greater than or equal to
L and there is no predecessor n¢1 of n1 such that
1 6
3 +1
( C3 )
n¢1 is equal to n1. 1.6!
2. n2 is the largest number less than or equal to H and =5
4 ´ 3!3!
there is no successor of n¢2 of n2 such that is equal
Therefore, there are 5 trees that can be formed with
to n2.
three unlabelled node.
Since there are m elements between n 1 and n2, it takes ‘m’
time to add elements between n1 and n2.
So time complexity is O (log n + m)
So the given expression becomes O (n 0log¢ n + m¢ log0 n)
And a + 10b + 100c + 1000d = 0 + 10 × 1 + 100 × 1 + 1000 × 1
= 10 + 100 = 110
Because a = 0, b = 1, c = 1 and d = 0
175. (c) The tightest upper bound that represents the time 1
complexity of inserting an object into a binary 2
search tree with n nodes is O (n).
176. (d)
177. (a) The box B1 gets executed when left subtree of n is NULL
and right subtree is not NULL. In this case, height of n
will be height of right subtree plus one.
The box B2 gets executed when both left and right
subtrees of n are not NULL. In this case, height of n will 3 4
be max of heights of left and right sbtrees of n plus 1.
178. (b) It is stated that there is a binary tree and we have populate
the tree with n elements. Sorting the n elements in the
increasing order,and placing them in the inorder traversal
nodes of the binary tree makes it only BST possible.
179. (a) Such a binary tree is full binary tree (a binary tree where
every node has 0 or 2 children). 5
103
183. (c) This is a formula to calculate the total number of nodes. 187. (a) Inorder traversal of a BST always gives elements in
It is 2n + 1 – 1. increasing order. Among all four options, (a) is the only
Let consider some examples to prove this. increasing order sequence.
1. Simplest could be taking the binary tree of h (height) = 0. 188. (b) The number of keys as per given are 4
Now, the binary tree of height h will have only 1 node. Applying the direct formula
Bn = 1/(n + 1) × (2n! / n!n!)
where, Bn is number of binary trees and n is the
Using formula 2 ^ (0 + 1) – 1 = 1. Hence, the formula is number of keys.
correct. ® Bn = 1/(4 + 1) × (8! / 4!4!)
2. Binary tree of h (height) = 2 ® Bn = 1/5 × (8 × 7 × 6 × 5 × 4!) / 4!4!
® Bn = 8 × 7 × 6/(4 × 3 × 2)
1 ® Bn = 56/4
® Bn = 14
The total number of binary trees with n = 4 is 14.
3
2 189. (b) At the root node (first level) the cost would be Q(n/2)
as the tree is balanced.
7 At next level, we have 2 nodes and for each of them
4 5 6
cost of computing g(x) will be Q(n/4). So, total cost
Using formula 2 ^ (2 + 1) – 1 = 7. Hence, the formula is at second level = Q(n/2). Similarly at each level (total
correct. cost per level and not the cost per node in a level)
184. (a) In order d b e a f c g the cost would be Q(n/2)and so for logn levels it
preorder a b d e c f g would be Q(nlogn).
190. (b) For a tree we not only require in order & preorder
1st element of pre order is root but also postorder traversal.
Preorder & postorder help to determine the roots of
binary subtrees, inorder arranges those roots in order.
Hence (b) is correct option.
191. (b) Given are 10, 1, 3, 5, 15, 12, 16
10
in preorder b is before d e. & c is before f g. 10
10
1 3 5 1
10
1
1 3
debfgca
3
5
185. (c) A node is a leaf node if both left and right child nodes of
it are NULL.
1) If node is NULL then return 0. 15
2) Else If left and right child nodes are NULL return 1.
3) Else recursively calculate leaf count of the tree using 10 10
below formula.
Leaf count of a tree = Leaf count of left subtree + Leaf 1 15 1 15
count of right subtree 12

3 12 3

5 5

16
Tree
Leaf count for the above tree is 3.
186. (a) The inorder traversal sequence is dbeafcg and the 10
preorder traversal sequence is abdecfg so, the tree is 1
a 1 15
2

c 3 12 16
b 3
g 5
d e f
The height of the leaf node (5) is high 3.
In the postorder traversal, the sequence is debfgca. Hence (b) is correct option.
104
192. (c) We can solve it in shortcut that the first given For option (b), see the following counter example.
element in 7, so we need to choose that particular
1
option in which 7 is at the right place i.e. all the
/ \
elements on its left should be smaller than it & all
2 3
the elements on the right should be equal & greater
/ \ /
than it.
4 5 6
So this rule is followed in option (c) only.
Inorder traversal is 4 2 5 1 6 3
To make in order of a binary search tree.
Preorder traversal is 1 2 4 5 3 6
(i) Start with the root node.
So, lastpost = 1, lastpre = 6, lastin = 3.
(ii) Scan its left subtree,
Hence option (d) is correct.
(iii) If the node in subtree has any left child then
197. (c) (XYZ) indicates that Y is left sub-tree and Z is right
store the node in stack & repeat this step for
subtree. Node is X
its left child unit no. left child of any node.
(iv) If leaf reached then print the node & pop the
stack, print the poped value.
(v) Check its right subtree & repeat step (III) for it.
(vi) When stack empty then stop
So here inorder is 0 1 2 3 4 5 6 7 8 9. Actually a fact
can be remembered that inorder traversal of a BST
leads to a sorted sequence of elements.
As per given in the questions:
Hence (c) is correct option.
(1 (234) (567))
193. (b) The summation is for each node, if that node
We get, the following tree
happens to be the root. When a node is root, it will
have (k – 1) nodes on the left sub tree (k being any
number) and correspondingly (n – k) elements on
the right sub tree. So, we can write recurrence T
(k – 1) * T(n–k) for the number of distinct binary
search trees, as the numbers on left and right sub
tree from BSTs independent of each other and only
a difference in one of the sub trees produces a
difference in the tree. Hence answer is B.
194. (b) Since the given B tree is 2–3–4 tree, there can be at 1 is the root node
most 4 children or 3 keys. In B tree insertion, we 2 and 3 are the non-leaf node
start from root and traverse till the leaf node where 4, 5, 6, 7 are the leaf node which may be null or
key is to be inserted. While traversing. If we find a further nested because in a binary tree every node
node which full, we split it. When we inseart G, we has 0 or children and not just 1.
find root itself is full. So, we split it when we come
down to left most leaf, we find that the leaf is also
Binary Heap
full, thus we split the leaf also. So, the percent node 198. (b)
becomes H. L, P, U and select P as for splitting. 199. 8 Within ‘n’ levels of min heap, nth smallest element will
Hence option (b) is correct. be present.
195. (d) Let the maximum possible height of a tree with n
nodes is represented by H(n). 200. (b)
The maximum possible value of H(n) can be 40
approximately written using following recursion
H(n) = H(2n/3) + 1
The solution of above recurrence is log3/2 n. We
30
can simply get it by drawing a recursion tree. 20

196. (d) It is given that the given tree is complete binary tree.
For a complete binary tree, the last visited node will
always be same for inorder and preorder traversal. None 10 15
16 17

of the above is true even for a complete binary tree.


The option (a) is incorrect because the last node visited
in Inorder traversal is right child and last node visited in 8 4
Postorder traversal is root.
The option (c) is incorrect because the last node visited Insert 35 Þ according to CBT
in Preorder traversal is right child and last node visited
in Postorder traversal is root.
105

201. 3 89
40

30 20
19
50

10 16 17
15
17 12 15 2

8 4 35

5 7 11 6 9 100
Heapification
1st swap is : 100 and 15
40 2nd swap is : 100 and 50
3rd swap is : 100 and 89
202. (a) 10
30 20
7
8

17 2 1 5
16
10 15 3
After insertion of elements,
level - order tansvasal is :
4 35
8
10, 8, 7, 3, 2, 1, 5
203. (c) The number of elements that can be sorted in Q
æ log n ö
40 (log n) time using heap sort is Q ç log log n ÷
è ø
Consider the number of elements is "k", which can
be sorted in q (k log k) time.
30 20 Analyzing the options in decreasing order of
complexity since we need a tight bound i.e., q.
æ log n ö
i.e., q (log n), Q ç , Q( log n ), Q(1)
10 35
16 17
è loglog n ø÷
So if k Î Q (log n) time required for heap sort is O
(k log k) i.e.,
8 4 15 q (log n × log log n), But this is not in Q (log n)
æ log n ö
If k Î Q ç log log n ÷ time required for Heap Sort
è ø
40
æ log n æ log n ö ö
Qç ´ log ç
è log log n è log log n ÷ø ø÷

35 20 æ é æ log n ö ùö
ç ê log ç ú÷
è log log n ÷ø ú÷
i.e., Q ç log n ´ ê
ç ê log log n ú÷
10 16 17 ç ê 1 424 3 ú÷
30
è êë £1 úûø
So, this is in Q (log n)
4 15
8
æ log n ö
Hence, answer is (c) Q ç log log n ÷
è ø
106
204. (c) linked data structures allow more flexibility in and the 208. (c) 12 mod 10 = 2
implementation of these linked data structure is through 18 mod 10 = 8
dynamic data structures 13 mod 10 = 3
205. (b) A binary tree is max-heap if it is a complete binary tree (A 2 mod 10 = 2 collision
complete binary tree is a binary tree in which every level, (2 + 1) mod 10 = 3 again collision
except possibly the last, is completely filled, and all nodes (using linear probing)
are as far left as possible) and it follows the max-heap (3 + 1) mod 10 = 4
property (value of each parent is greater than or equal to 3 mod 10 = 3 collision
the values of its children). (3 + 1) mod 10 = 4 again collision
(a) is not a max-heap because it is not a complete binary (using linear probing)
(4 + 1) mod 10 = 5
tree
23 mod 10 = 3 collision
(b) is a max-heap because it is complete binary tree
(3 + 1) mod 10 = 4 collision
and follows max-heap property.
(4 + 1) mod 10 = 5 again collision
(c) is not a max-heap because 8 is a child of 5 in this (using linear probing)
tree, so violates the max-heap property. (5 + 1) mod 10 = 6
(d) is not a max-heap because 8 is a child of 5 in this 5 mod 10 = 5 collision
tree, so violates the max-heap property. There are (5 + 1) mod 10 = 6 again collision
many other nodes in this tree which violate max- (6 + 1) mod 10 = 7
heap property in this tree. 15 mod 10 = 5 collision
206. (c) Suppose that we have a node x, then for the condition 1 (5 + 1) mod 10 = 6 collision
< = x < = n/2 and A[x] > = A[2x + 1] where 2x and 2x + 1 are (6 + 1) mod 10 = 7 collision
left child and right child of the node x respectively of a (7 + 1) mod 10 = 8 collision
binary max-heap. (8 + 1) mod 10 = 9 collision
Thus, under given cases the tree obtained is. So, resulting hash table
25 0
1
14 16 2 12
3 13
13 10 4 2
8 12
207. (d) Always a greater element is deleted from the binary heap 5 3
first. The answer of previous question gave the array as 6 23
[25, 14, 16, 13, 10, 12, 8] 7 5
So, when the greatest element, i.e., 25 is deleted the array 8 18
becomes [14, 16, 13, 10, 12, 8] 9 15
And next after second deletion the array becomes [14, 209. (b) Heaps are implemented as simple arrays, to insert n
13, 10, 12, 8] more elements each element take Q(1) time. So total
Thus, the procedure for the obtaining the final tree is as time would be Q(n).
follows. 210. (a) Time taken by binary max heap to identify the max
Replacing 25 with After heapifying element is O(1). Therefore, the time taken by binary
max heap to identify the smallest element is O(n).
12 16
211. (b) Binary search takes Q(log2 n), as the search space is
continuously divided by two. In inserting in the heap
14 14 12
16
we only have to move up the path from the leaf to the
root, the height of a heap cannot be more than Q(log n).
13 10 8 13 10 8
So the time complexity for the insertion is Q(log log n).
Replacing 16 by 8 After heapifying 212. (d) Create max heap for options.
8 14 9
1
13 6 3 1
14 12 12
(a) 3 5 6 (b) 8
13 10 8 10 Fail Fail
107
But the balance binary search tree is efficient data
9 structure since at every decision it selects one of its
1 subtree to no. of elements to be checked are reduced
6 3 1 by a factor of 1/2 every time.
3 5 6 n /2!= x
(c) (d) 8 x = logn
Fail Hence (b) is correct option.
Fail 216. (b) This is a basic question. We know that to reach a
Here in options (a), (b) and (c), value present at node on level i, the distance to the root is i–1. This
node is not greater than all its children. Hence (d) is implies that if an element is stored at index i of the
array, then index of the parent is n.
correct option.
213. (a) Given heap is as follows Graph
217. (16) If every vertex has degree at least 3 then,
the maximum possible value of n
where (n is the vertices), given edge (E) = 25
then, 2( E ) ³ S degree of vertices
(\ According to undirected graph)
2( E ) ³ 3n
To add 7, 2, 10, 4 we add the node at the end of array
2( E )
9 n£
3
2 ´ 25
5 6 8 n£
3
3 7 2 4 n £ 16.66
1 10 So, n is at Most 16.
We keep if at right place in the heap tree. Hence 16 is correct answer.
Compare elements with its parent node. 218. 6
The different topological orderings will be as follows:
Since 10 > 6 and 7 > 5, we interchange.
(1) a b c d e f
(2) a d e b c f
(3) a b d c e f
(4) a d b c e f
(5) a b d e c f
(6) a d b e c f
219. (a) The shortest path may change. The reason is, there
Since 10 > 9, we interchange and we get may be different number of edges in different paths
from s to t. For example, let shortest path be of weight
10 10 and has 5 edges. Let there be another path with 2
edges and total weight 25. The weight of the shortest
path is increased by 5*10 and becomes 10 + 50. Weight
7 9 8 of the other path is increased by 2*10 and becomes 25
+ 20. So the shortest path changes to the other path
3 5 2 4 with weight as 45.
1 6 The Minimum Spanning Tree doesn't change.
Remember the Kruskal's algorithm where we sort the
n / 2 = 10/2 = 5 edges first. If we increase all weights, then order of
3 is at right position edges won't change.
4 is at right position 220. 12 8
Order d b
10 7 9 8 3 1 5 2 6 4
Hence (a) is correct option. 2
5 x 5
214. (a)
215. (b) Both the tasks can be performed by both the data
structures but heap is a data structure where to a c
8
perform these function every element has to be The shortest path (excluding x) from 2 to 3 is of weight 12
checked so O(n) complexity. (2–1–0–3).
108
221. (d) An n vertex self complementary graph has exactly half 224. 506 edges
The vertices of Graphs are ordered pair (i, j) where two
n(n - 1) vertices (a, b) and (c, d) are connected by an edge only
number of edges of the complete graph i.e.,
4 if |a – c| < 1 and |b – d| < 1. Also given that 1 < i < 12,
i < j < 12.
edges. Since n (n –1) must be divisible by 4, n must be
So, for example (1, 2) is connected to (1, 1), (2, 1), (2, 2),
congruent to 0 or 1 module 4. (2, 3) and (1, 3)
222. (b) Since, every edge in a tree is bridge Similarly (5, 5) is connected to (4, 4), (4, 5), (4, 6), (5, 4),
\ (a) is false (5, 6), (6, 4), (6, 5) and (6, 6) and (1, 12) is connected only
Since, every edge in a complete graph kn (n ³ 3) is not to (1, 11), (2, 11) and (2, 12).
a bridge Þ (c) is false So, different vertices are connected to different number of
vertices. However we can visualize this easily using
Let us consider the following graph G:
following diagram:–
1 2 3 4 5 6 7 8 9 10 11 12
e
1
2
This graph has a bridge i.e., edge ‘e’ and a cycle of 3
length ‘3’
4
\ (d) is false
5
Since, in a cycle every edge is not a bridge
\ (b) is true 6
223. (c) An ordered n type is graphic if it is a degree sequence 7
of some simple undirected graph. options with
corresponding graphs are shown below:– 8
(a) is Graphic seq. (1, 1, 1, 1, 1, 1) 9 A
A B
10 B
C D 11

12
E F
(b) is Graphic seq. (2, 2, 2, 2, 2, 2) Each cell of the table represents corresponding vertex
of the graph. For example the cell ‘A’ represents vertex
A B C
(9, 0) of the graph, similarly ‘B’ represents vertex (10,
2) of the graph.
Now, we easily see that there are three kind of vertices
in the graph (or table):
F E D (i) Corner vertices which are connected to ‘3’ neighbours.
(d) is Graphic seq. (3, 2, 1, 1, 1, 0) No. of such vertices = 4
C Total no. of edges for such vertices = 4 × 3 = 12
(ii) Vertices in first/ last row of first/last column except
A D F corner vertices, which are connected to ‘5’ neighbours
each.
B E No. of such vertices = 40
(c) is not graphic seq. (3, 3, 3, 1, 0, 0) Total no. of edges for such vertices = 40 × 5 = 200
Clearly we can safely remove isolated vertices (i.e. vertices
(iii) Internal cells (vertices) that are connected to ‘8’
with zero degree) to prepare the graph with this degree
sequence. Remaining sequence is (3, 3, 3, 1). Let us name neighbours each.
these as A, B, C, D Now vertex with degree 1 (is D) No. of such vertices = 100
Must be connect only one A, B or C. If we remove this also Total no. of edges for these vertices = 100 × 8 = 800
then 3 vertices remains A, B and C. One of them will have Edge total of all above cases = 12 + 200 + 800 = 1012
degree = 2 and other's degree = 3. Now remove vertex with However in above calculation each edge is counted
degree 2, finally two vertices remains that must have degree twice, so, finally total no. of edges in the graph =
= 2 which is not possible in a simple undirected graph.
Hence the seq. (3, 3, 3, 1, 0, 0) is not graphic. 1012
= 506.
2
109
225. (d) DFS traversal takes the path to the end & then move 226. (b) Total no. of nodes = n
to other branch. For an edge of n nodes we select any 2 which make
a graph.
a So, nc2 ® n(n – 1)/2 edges
n = 4 ; ( 4 ´ 3)/2 = 6
e b f 227. (d) Here, we need to find the number of undirected
h graphs to be constructed
g Now, S = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 ... n – 1
= n (n – 1)/2 ® constructed graphs
= 2S
n ( n –1)
=2 2

Hence option (d) is correct.

You might also like