GATE 2004
199 questions
MCQ 1991-mark 199
Questions 111–115 of 199
The elements 32, 15, 20, 30, 12, 25, 16, are inserted one by one in the given order into a MaxHeap. The resultant MaxHeap is
Answer: (A)
Insert each key at the next free place (so the tree stays complete), then swap it with its parent while it is larger than the parent.
32: root.
15: left child of 32. No swap.
20: right child of 32. No swap.
30: left child of 15. 30 > 15, so swap. Then 30 < 32, stop. Tree: 32; 30, 20; 15.
12: right child of 30. No swap.
25: left child of 20. 25 > 20, so swap. Then 25 < 32, stop. Tree: 32; 30, 25; 15, 12, 20.
16: right child of 25. 16 < 25, no swap.
The final heap is shown below: 32 at the root, 30 and 25 below it, then 15, 12 under 30 and 20, 16 under 25. This is figure (a). In (c) the leaves 20 and 16 under 25 are in the other order, which the insertion of 16 does not produce, and (b) and (d) have 25 on the left, which does not happen here.
Answer: (A)
32: root.
15: left child of 32. No swap.
20: right child of 32. No swap.
30: left child of 15. 30 > 15, so swap. Then 30 < 32, stop. Tree: 32; 30, 20; 15.
12: right child of 30. No swap.
25: left child of 20. 25 > 20, so swap. Then 25 < 32, stop. Tree: 32; 30, 25; 15, 12, 20.
16: right child of 25. 16 < 25, no swap.
The final heap is shown below: 32 at the root, 30 and 25 below it, then 15, 12 under 30 and 20, 16 under 25. This is figure (a). In (c) the leaves 20 and 16 under 25 are in the other order, which the insertion of 16 does not produce, and (b) and (d) have 25 on the left, which does not happen here.
Answer: (A)
Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod 10, which of the following statements are true?
i) 9679, 1989, 4199 hash to the same value
ii) 1471, 6171 has to the same value
iii) All elements hash to the same value
iv) Each element hashes to a different value
i) 9679, 1989, 4199 hash to the same value
ii) 1471, 6171 has to the same value
iii) All elements hash to the same value
iv) Each element hashes to a different value
Answer: (C)
The hash function gives the last digit of .
4322 2, 1334 4, 1471 1, 9679 9, 1989 9, 6171 1, 6173 3, 4199 9.
(i) 9679, 1989 and 4199 all hash to 9, so it is true.
(ii) 1471 and 6171 both hash to 1, so it is true.
(iii) False: 4322 hashes to 2 but 1334 hashes to 4.
(iv) False: 9679 and 1989 hash to the same value 9.
Only (i) and (ii) are true.
Answer: (C)
4322 2, 1334 4, 1471 1, 9679 9, 1989 9, 6171 1, 6173 3, 4199 9.
(i) 9679, 1989 and 4199 all hash to 9, so it is true.
(ii) 1471 and 6171 both hash to 1, so it is true.
(iii) False: 4322 hashes to 2 but 1334 hashes to 4.
(iv) False: 9679 and 1989 hash to the same value 9.
Only (i) and (ii) are true.
Answer: (C)
The tightest lower bound on the number of comparisons, in the worst case, for comparison-based sorting is of the order of
Answer: (C)
A comparison sort can be drawn as a binary decision tree. Each internal node is one comparison, and each leaf is one possible sorted order. There are orders, so the tree needs at least leaves.
A binary tree of height has at most leaves, so and . The height is the number of comparisons in the worst case.
Also (the largest factors of are each at least ), so .
So every comparison sort needs comparisons in the worst case. Merge sort and heap sort reach , so this bound is tight. (A) is too small, is not tight since is achievable, and is larger than needed.
Answer: (C)
A binary tree of height has at most leaves, so and . The height is the number of comparisons in the worst case.
Also (the largest factors of are each at least ), so .
So every comparison sort needs comparisons in the worst case. Merge sort and heap sort reach , so this bound is tight. (A) is too small, is not tight since is achievable, and is larger than needed.
Answer: (C)
Let f(n), g(n) and h(n) be functions defined for positive integers such that
Which one of the following statements is FALSE?
Which one of the following statements is FALSE?
Answer: (D)
All functions are taken as positive. The given facts: , , and with , so and grow at the same rate.
(A) . True.
(B) and , and is transitive, so . True.
(C) If , then , which contradicts . So . True.
(D) From , multiply both sides by : . So , and the statement "" is false.
Answer: (D)
(A) . True.
(B) and , and is transitive, so . True.
(C) If , then , which contradicts . So . True.
(D) From , multiply both sides by : . So , and the statement "" is false.
Answer: (D)
The time complexity of the following C function is (assume n 0)
int recursive (int n) {
if (n == 1)
return (1);
else
return (recursive (n - 1) + recursive (n - 1));
} Answer: (D)
int recursive (int n) {
if(n == 1) // constant time, say 'a'
return (1); // constant time, say 'a'
else
// takes T(n-1) + T(n-1) time
return (recursive (n-1) + recursive (n-1));
}
The recurrence from the code is . Here is the constant cost of the part of the function that is not recursive (the comparison and the addition).
Solve it by back substitution:
The base case is , so put , that is :
Answer: (D)