Disha Publication Computer Past Questions Programming - Data Structures. CB512871741
Disha Publication Computer Past Questions Programming - Data Structures. CB512871741
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)
Ü
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
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
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
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
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