GATE 2010
82 questions
MCQ 821-mark 39 · 2-mark 43
Questions 26–30 of 82
A 5-stage pipelined processor has Instruction Fetch (IF), Instruction Decode (ID), Operand Fetch (OF), Perform Operation (PO) and Write Operand (WO) stages. The IF, ID, OF and WO stages take 1 clock cycle each for any instruction. The PO stage takes 1 clock cycle for ADD and SUB instructions, 3 clock cycles for MUL instruction, and 6 clock cycles for DIV instruction respectively. Operand forwarding is used in the pipeline. What is the number of clock cycles needed to execute the following sequence of instructions?
Answer: (B)
Only one instruction can be in the PO stage at a time. With forwarding, a result goes straight from the end of PO to the next instruction, so it does not wait for WO.
- I0 MUL: IF 1, ID 2, OF 3, PO 4 to 6, WO 7.
- I1 DIV: IF 2, ID 3, OF 4. PO is busy with MUL until cycle 6, so DIV waits and does PO in cycles 7 to 12, WO 13.
- I2 ADD: IF 3, ID 4, OF 5 (it waits behind DIV). It needs R5 from DIV, which is forwarded at the end of cycle 12, so PO is in cycle 13, WO 14.
- I3 SUB: IF 4, ID 5, OF 6 (it waits behind ADD). It needs R2 from ADD, forwarded at the end of cycle 13, so PO is in cycle 14, WO 15.
The last instruction finishes in cycle 15, so 15 cycles are needed.
Answer: (B)
- I0 MUL: IF 1, ID 2, OF 3, PO 4 to 6, WO 7.
- I1 DIV: IF 2, ID 3, OF 4. PO is busy with MUL until cycle 6, so DIV waits and does PO in cycles 7 to 12, WO 13.
- I2 ADD: IF 3, ID 4, OF 5 (it waits behind DIV). It needs R5 from DIV, which is forwarded at the end of cycle 12, so PO is in cycle 13, WO 14.
- I3 SUB: IF 4, ID 5, OF 6 (it waits behind ADD). It needs R2 from ADD, forwarded at the end of cycle 13, so PO is in cycle 14, WO 15.
The last instruction finishes in cycle 15, so 15 cycles are needed.
Answer: (B)
What is the value printed by the following C program?
#include <stdio.h>
int f(int * a, int n)
{
if(n<=0)return 0;
else if(*a% 2 ==0) return *a+f(a+1,n-1);
else return *a-f(a+1,n-1);
}
int main ( )
{
int a[ ] = {12, 7, 13, 4, 11, 6};
printf ("%d", f(a,6));
return 0;
} Answer: (C)
The function f(a, n) returns 0 if . If the first element is even, it returns that element plus f of the rest. If it is odd, it returns that element minus f of the rest. The array is . Work from the last call back:
- : 6 is even, so
- : 11 is odd, so
- : 4 is even, so
- : 13 is odd, so
- : 7 is odd, so
- : 12 is even, so
Answer: (C)
What does the following program print?
#include <stdio.h>
void f (int *p, int * q) {
p=q;
*p=2;
}
int i= 0, j= 1;
int main ( ){
f(&i, & j);
printf( "%d%d \n", i,j);
return 0;
} Answer: (D)
In f, the statement p = q makes p point to j. This change to p stays inside the function. Then *p = 2 sets j to 2. The value of i does not change.
Printed output: 0 2
Answer: 0 2
Printed output: 0 2
Answer: 0 2
The following C function takes a simply-linked 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.
typedef struct node {
int value;
struct node *next;
} Node;
Node *move_to_front(Node *head) {
Node *p, *q;
if ((head == NULL) || (head->next == NULL)) return head;
q = NULL; p = head;
while (p-> next !=NULL) {
q=p;
p=p->next;
}
_______________________________
return head;
} Choose the correct alternative to replace the blank line.Answer: (D)
When the loop ends, p points to the last node and q points to the second-last node. To move the last node to the front:
- : q becomes the new last node.
- : p links to the old first node.
- : p becomes the first node.
This is option (D).
Why the others fail:
- (A) only changes the local pointer q. The old second-last node still points to p, and closes a cycle.
- (B) runs before , so p's next becomes p itself, and the rest of the list is lost.
- (C) makes the list and then ends it, so all nodes before q are lost (for a list longer than 2 nodes).
Answer: (D)
- : q becomes the new last node.
- : p links to the old first node.
- : p becomes the first node.
This is option (D).
Why the others fail:
- (A) only changes the local pointer q. The old second-last node still points to p, and closes a cycle.
- (B) runs before , so p's next becomes p itself, and the rest of the list is lost.
- (C) makes the list and then ends it, so all nodes before q are lost (for a list longer than 2 nodes).
Answer: (D)
In a binary tree with n nodes, every node has an odd number of descendants. Every node is considered to be its own descendant. What is the number of nodes in the tree that have exactly one child?
Answer: (A)
Suppose a node has exactly one child. Then its number of descendants is the node itself plus the descendants of that child, which is 1 + odd = even. This goes against the given condition.
So no node in the tree has exactly one child.
Answer: 0
So no node in the tree has exactly one child.
Answer: 0