CDS S14
CDS S14
CDS S14
SOLUTIONS
struct
{
short P[10];
union
{
short a;
float b;
long z;
} u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes
respectively. The memory requirements for variable ‘t’ and ‘u’ ignoring alignment
considerations respectively are _______.
Explanation:
(c)
Here structure creates the memory for array and union, but union creates the memory for only
‘long z’ which is maximum among all data types in union.
u = max (2, 4, 8) = 8
∴ t = 20 + 8 = 28
2. The function delete (head, element) is used to delete a node from the linked list by finding the
node value with a given element. The parameter head is the first node of the list. Find the
missing statements A and B in the following “delete” function to delete the node? (Assume all
1
elements are distinct in the list and the function returns pointers that point to the first node of the
list).
Explanation:
𝐴.𝐴𝐴𝐴𝐴.𝐴𝐴𝐴𝐴 == 10
𝐴𝐴 ( )
𝐴
{
𝐴.𝐴𝐴𝐴𝐴 = 𝐴.𝐴𝐴𝐴𝐴.𝐴𝐴𝐴𝐴;
𝐴
return head;
}
2
3. Consider the following code
Assume Node is the structure type with two members: ‘value’ and ‘next’. Identify the node value
printed by the above code if non-empty linked list header is passed to the function find?
(a) First element of the list [i.e., value of the first node]
(b) Second element of the list
(c) Middle element of the list
(d) Last element of the list
Explanation:
(c)
4. In delete operation of binary search tree, we need inorder successor (or predecessor) of a node
when a node to be deleted where it has both left and right child. Which of the following is true
about inorder successor needed in delete operation?
(a) Inorder successor is always either leaf node or a node with empty right child.
(b) Inorder successor maybe an ancestor of the node.
(c) Inorder successor is always a leaf node.
(d) Inorder successor is always either a leaf node or a node with empty left child.
3
Solution: Option (d)
Explanation:
(d)
Successor of Root element is always the smallest element of the Right subtree. Because it will be
the next largest element after the element to be deleted.
void main ( )
{
int x = 1, i, y = 2;
for (i = 0; i < 5; i++)
{
x << 1;
y = x + i;
}
printf(“%d, %d”, x, y);
}
Explanation:
(a)
4
x = 1, y = 5
[Note: x << 1 will not change the value of x, but x = x << 1 will changes the value of x]
(a) f is a function which takes integer pointer as argument and returns integer.
(b) f is a function which takes integer pointer as an argument and returns address of an integer.
(c) f is a pointer to a function which takes integer pointer as an argument and returns integer.
(d) f is a pointer to a function which takes integer pointer as an argument and returns address of
an integer.
Explanation:
(b)
7. Which of the following is NOT true about linked list implementation of queue?
(a) In enqueue operation, if new nodes are inserted at the beginning of linked list, then in
dequeue operation nodes must be removed from end.
(b) In enqueue operation, if new nodes are inserted at the end, then in dequeue nodes must be
removed from the beginning.
5
(c) Both (a) and (b)
(d) None of the above
Explanation:
(d)
Both (a) and (b) are true to keep the first in first out order, a queue can be implemented using
linked list in any of the given two ways.
Which of the following order of elements are inserted into an empty AVL tree, so that it is
possible to get the above AVL tree?
(a) 94, 71, 86, 25, 98, 83, 27, 90 (b) 98, 94, 90, 83, 86, 25, 71, 94
(c) 86, 25, 98, 83, 27, 90, 71, 94 (d) None of these
Explanation:
6
The order: 86, 25, 98, 83, 27, 90, 71, 94 will result the given AVL
[Note: Option (a) and Option (b) will generate different AVL trees]
main ( )
{
extern int i ;
i = 20;
printf (“%d”, i);
}
Explanation:
(a)
7
Linked error: Undefined symbol-i Extern int i; Specifies to the compiler that the memory for i is
allocated in some other program and that address will be given to the current program at the time
of linking. But linker finds that no other variable of name ‘i’ is available in any other program
with memory space allocated for it. Hence linker error occurred.
gate (root)
{
if (root = = null) return 0;
if (root → leftchild = = null && root → rightchild = = null) return 1;
else return (maximum (gate(root → leftchild), gate(root → rightchild)) + 1);
}
Explanation:
(c)
11. A 3-ary tree is a tree in which every internal node has exactly 3 children. The number of leaf
nodes in such a tree with 19 internal nodes will be ____________.
Solution: 39
Explanation:
39
8
12. Consider the following code.
void main( )
{
static int i = 5;
if ( – – i)
{
main ( );
printf(“%d”, i);
}
}
Solution: 4
Explanation:
The variable ‘i’ is declared as static, hence memory for ‘i’ will be allocated for only once, as it
encounters the statement. The function main ( ) will be called recursively unless i becomes equal
9
to zero and since main ( ) is recursively called, so the value of static i, i.e. 0 will be printed every
time the control is returned. So total 4 times zero is printed.
13. Consider a two-dimensional array with elements stored in the form of lower triangular
matrix. The elements must be crossed to read A[4, 2] from the array A[–6, …, + 8, – 6, …, + 8]
whose base address 1000 is _________. (Assume elements are stored in row major order).
Solution: 1063
Explanation:
1063
10
14. Consider the following program
int main ( )
{
char *str = “Gate2015”
printf (“%d”, ravindra (str));
return 0;
}
Solution: 8
Explanation:
The function counts number of characters in the input string. Gate2015 has 8 characters. P1
points to null character and P2 points to first character, at the end of while loop.
15. The sum of the minimum and maximum number of nodes in the AVL tree of height 5 is
___________. (Assume root node is present at height zero)
Solution: 83
Explanation:
83
11
16. Consider the following program.
int main ( )
{
int x = 016;
printf (“%d”, x);
return 0;
}
Solution: 14
Explanation:
14
If the root of following tree is passed to the above function, what is the level order traversal of
output tree produced by above function? (newNode is a function which creates new node)
12
(a) 2 2 3 3 1 1 (b) 2 2 3 1 3 1
(c) 2 3 2 3 1 1 (d) 2 3 2 3 2 1
Explanation:
(b)
18. Which of the following is correct output for the program code given below?
main ( )
{
void pr( );
pr ( );
pr ( );
pr ( );
}
void pr ( )
{
static int i = 1;
printf (“%c”, (65 + i++));
}
13
(c) 67, 68, 69 (d) None of these
Explanation:
(d)
The correct output is “BCD” when the function pr ( ) is first called the value of i is initialized to
1. After the pr ( ) completes its execution i = 2 is retained for it’s next call as “i” is static
variable.
∴ 65 + 1 = 66 (B)
65 + 2 = 67 (C)
65 + 3 = 68 (D)
Explanation:
(a)
<< and >> are bitwise operators used to multiply and divide by power of 2 respectively (shift
operators)
∴ i << 3 ⇒ i * 8
j >> 2 ⇒ j / 4
20. Consider the following foo function and identify the return value of foo function.
14
int c, x = 0;
while (n! = 0)
{
if (n & 01) x++;
n >>= 1;
}
return c;
}
Explanation:
(a)
while (n! = 0)
{
if (n & 01) x++; /* performs bitwise AND operator and if condition is
satisfied if result contains atleast one 1.
n >> 1
}
21. Which of the following are the number of assignments, number of additions and number of
subtractions respectively required for swapping 2 variables without the help of 3rd variable?
(a) 3, 2, 2 (b) 3, 1, 2
(c) 3, 3, 2 (d) 2, 2, 2
Explanation:
15
(b)
The following is the process for swapping two variables i and j without 3rd variable.
i=i+j
j=i–j
i = i – j;
22. Consider the AVL tree T in which left subtree contains half of the maximum number of
nodes possible in the balanced AVL tree of height h and right subtree consists of one 3rd of the
maximum number of nodes possible in AVL tree of height ‘h’.
Assume that tree T may or may not be height balanced at present. What is the total maximum
possible number of nodes in T?
5 5
(a) 6 (2𝐴+1 − 1) − 1 (b) 6 (2𝐴+1 − 1) + 1
3 3
(c) 2 (2𝐴+1 + 1) (d) 2 (2𝐴+1 + 1) − 1
Explanation:
(b)
2𝐴+1 − 1 1
∴ 𝐴𝐴𝐴𝐴𝐴 = ( ) + (2𝐴+1 − 1) + 1
2 3
5
= 6 (2𝐴+1 − 1) + 1
16
23. The minimum size that an array may require to store a binary tree with ‘n’ nodes is
____________.
Explanation:
(a)
In case of full or complete binary tree minimum height ⇒ hmin = ⌈𝐴𝐴𝐴2 (𝐴 + 1)⌉
Hence, last element will be stored at 2𝐴𝐴𝐴𝐴 − 1
variable l;
procedure My (K: integer)
begin
K = K + 1;
Print (K);
end
procedure R ( )
var l;
begin
l = 5;
My (l);
print (l);
end;
begin
l = 3;
My (l);
R ( );
print (l);
17
end
Find the output produced by above program using dynamic scoping, and all functions uses call
by value.
(a) 4, 6, 6, 4 (b) 4, 6, 5, 3
(c) 4, 5, 6, 4 (d) 3, 6, 6, 3
Explanation:
(b)
The program uses dynamic scoping. So if the variable is not available in the present function,
then it searches back in the previous function scope from where it was called.
struct listnode
{
int data;
struct listnode *next;
};
18
if (head = = NULL || head → next = = NULL) return;
struct listnode *tmp = head → next;
head → next = tmp → next;
free (tmp);
fun (head → next);
}
Explanation:
(c)
The above program deletes every alternate node in the linked list (In particular second, fourth,
sixth… soon nodes will be deleted)
void f (int n)
{
if (n ≤ 0) return;
else
{
print (n);
f (n – 2);
print (n);
f (n – 1);
}
}
Let f(n) be the number of values printed. What is the number of values printed by above
function?
19
(a) f(n – 1) + f(n – 2) (b) f(n – 1) + f(n – 2) + 1
(c) f (n – 1) + f(n – 2) + 2 (d) f (n – 2) + f(n – 3)
Explanation:
(c)
f(n – 1) + f(n – 2) + 2 values printed by f(n), where 2 indicate number of print statements.
27. What is the number of additions in the fibonacci series of n which uses following recursive
function.
fib (n) = fib (n – 1) + fib (n – 2)
(a) f (n – 1) + f (n – 2) (b) f (n – 1) + f (n – 2) + n
(c) f (n – 1) + f (n – 2) + 1 (d) f (n – 1) + f (n – 2) + 2n
Explanation:
(c)
int x = 0, i;
for (i = 0; i < 10, i++)
if (i%2 && x++)
x += 2;
(a) 11 (b) 13
(c) 15 (d) 17
Explanation:
20
(c)
(15)
i=1⇒x=3
i=3⇒x=6
i=5⇒x=9
i = 7 ⇒ x = 12
i = 9 ⇒ x = 15
29. Consider the following infix expression which is to be converted to postfix expression using
stack.
(((P + Q) * (R + S)) /T) + (A * (B + C))
The sum of all unique possible heights of stack when converting from infix to postfix is ______.
Solution: 15
Explanation:
15
21
Again ‘)’ comes pop till first ‘(‘ comes
Now ‘)’ comes pop until ‘(‘ comes maximum possible stack height is 5
22
30. The sum of outputs printed by running the following program is ________.
int main ( )
{
int i;
for (i = 3; i <= 6; i++)
printf(“%d”, f1(i));
}
int f1 (int n)
{
if (n < 2) return n;
else return (f1(n – 1) + f2(n – 2));
}
int f2 (int n)
{
if (n <= 1) return n;
else return (2 * f1 (n – 2) + 1);
}
Solution: 20
Explanation:
20
f1 (3) = 2
f1 (4) = 3
f1 (5) = 6
1 (6) = 9
2 + 3 + 6 + 9 = 20
31. The key 14, 4, 6, 16, 32, 50 in the order are inserted into an initially empty AVL tree. The
total numbers of rotations to make AVL with the given keys are __________. Assume “single
rotation = 1 rotation” and “double rotation = 1 rotation”.
Solution: 3
Explanation:
23
3
One double and two single rotations are required. So total 3 rotations.
32. Consider the following postorder and Inorder traversals of binary tree.
The total number of nodes that have height greater than the height of node 6 are ___________.
Assume root is at lowest height.
Solution: 4
Explanation:
24
1, 4, 5 and 7 have height greater than node 6.
int g(int e)
{
if (e > 4)
return (2 + g(e – 5) + g(e – 2));
return 1;
}
Solution: 40
Explanation:
40
∴ g(15) = 2 + 13 + 25 = 40.
Also for this kind of question , instead of computing recursively, by seeing the recurrence we can go
bottom up, like in this case g(0) =g(1)=g(2)=g(3)= g(4)=0.
Now g(5)=2+g(0)+ g(3) =2 ; g(6)= 2+g(1)+g(4) =2… and so on we can build till g(15)
34. A priority queue can efficiently implemented using which of the following data structures?
Assume that the number of insert and peek (operation to see the current highest priority item)
25
and extraction (remove the highest priority item) operations are almost same.
(a) Array
(b) Linked List
(c) Heap Data Structures like Binary Heap, Fibonacci Heap
(d) None of the above
Solution: Option (c )
26