GATE 2009
66 questions
MCQ 661-mark 24 · 2-mark 42
Questions 26–30 of 66
Consider a 4 stage pipeline processor. The number of cycles needed by the four instructions I1, I2, I3, I4 in stages S1, S2, S3, S4 is shown below:
What is the number of cycles needed to execute the following loop?
For (i=1 to 2) {I1; I2; I3; I4;}
What is the number of cycles needed to execute the following loop?
For (i=1 to 2) {I1; I2; I3; I4;}
Answer: (B)
Each stage handles one instruction at a time. We assume the buffers between stages can hold waiting results like a queue, so an instruction leaves a stage as soon as it is done. A stage of an instruction can start only when (1) the instruction has finished the previous stage and (2) the previous instruction has finished this stage. So
The loop runs I1 to I4 twice, so 8 instructions in all. The table shows the cycles each stage is busy.
For example, I3 finishes S1 in cycle 5, but S2 is busy with I2 until cycle 6, so I3 uses S2 in cycle 7. The last instruction leaves S4 at the end of cycle 23, so 23 cycles are needed.
Answer: (B)
The loop runs I1 to I4 twice, so 8 instructions in all. The table shows the cycles each stage is busy.
For example, I3 finishes S1 in cycle 5, but S2 is busy with I2 until cycle 6, so I3 uses S2 in cycle 7. The last instruction leaves S4 at the end of cycle 23, so 23 cycles are needed.
Answer: (B)
Consider the program below:
#include <stdio.h>
int fun(int n, int * f_p) {
int t, f;
if (n <=1) {
*f_p =1;
return 1;
}
t = fun (n-1, f_p);
f = t+*f_p;
*f_p = t;
return f;
}
int main() {
int x = 15;
printf ("%d\n", fun(5,& x));
return 0;
} The value printed isAnswer: (B)
x is passed by address, so is x. The call fun(5, &x) goes down to fun(1), then each call finishes in order from n = 1 upward. Each call sets to the result of the call below it, , then .
fun(1): n <= 1, so x = 1 and it returns 1. (The starting value 15 is overwritten.)
fun(2): t = 1, f = 1 + 1 = 2, x = 1, returns 2.
fun(3): t = 2, f = 2 + 1 = 3, x = 2, returns 3.
fun(4): t = 3, f = 3 + 2 = 5, x = 3, returns 5.
fun(5): t = 5, f = 5 + 3 = 8, x = 5, returns 8.
This is the Fibonacci sequence 1, 2, 3, 5, 8. The program prints 8.
Answer: (B)
fun(1): n <= 1, so x = 1 and it returns 1. (The starting value 15 is overwritten.)
fun(2): t = 1, f = 1 + 1 = 2, x = 1, returns 2.
fun(3): t = 2, f = 2 + 1 = 3, x = 2, returns 3.
fun(4): t = 3, f = 3 + 2 = 5, x = 3, returns 5.
fun(5): t = 5, f = 5 + 3 = 8, x = 5, returns 8.
This is the Fibonacci sequence 1, 2, 3, 5, 8. The program prints 8.
Answer: (B)
What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a single node is 0.
Answer: (B)
A tree with node has a height of .
The height is largest when every level holds the fewest nodes possible.
Let be the fewest nodes in an AVL tree of height . The root has one subtree of height and one of height at the least, so:
Base values: , .
Then:
With nodes, height is possible because . Height would need at least nodes, and . So the maximum height is .
Answer: (B)
Consider a binary max-heap implemented using an array.
What is the content of the array after two delete operations on {25,14,16,13,10,8,12}
What is the content of the array after two delete operations on {25,14,16,13,10,8,12}
Answer: (D)
In a delete, the root is taken out. The last element is put in its place. Then the root is pushed down until the heap property is correct. For the first delete:
(the element that breaks the max-heap property is swapped with the larger of its children) (now the heap property holds)
Second delete:
(now the heap property holds)
Correct Answer:
(the element that breaks the max-heap property is swapped with the larger of its children) (now the heap property holds)
Second delete:
(now the heap property holds)
Correct Answer:
Consider a binary max-heap implemented using an array.
Which one of the following array represents a binary max-heap?
Which one of the following array represents a binary max-heap?
Answer: (C)
Store the array from index 1. The children of the node at index are at and . For 7 elements, node 1 has children 2 and 3, node 2 has children 4 and 5, and node 3 has children 6 and 7. In a max-heap every parent is at least as large as its children.
(A) 12 at index 2 has children 13 and 10. 12 < 13, so it is not a max-heap.
(B) 14 at index 2 has children 16 and 10. 14 < 16, so it is not a max-heap.
(C) 25 ≥ 14, 16; 14 ≥ 13, 10; 16 ≥ 8, 12. Every parent is at least its children, so it is a max-heap:
(D) 12 at index 3 has children 8 and 16. 12 < 16, so it is not a max-heap.
Answer: (C)
(A) 12 at index 2 has children 13 and 10. 12 < 13, so it is not a max-heap.
(B) 14 at index 2 has children 16 and 10. 14 < 16, so it is not a max-heap.
(C) 25 ≥ 14, 16; 14 ≥ 13, 10; 16 ≥ 8, 12. Every parent is at least its children, so it is a max-heap:
(D) 12 at index 3 has children 8 and 16. 12 < 16, so it is not a max-heap.
Answer: (C)