GATE 2003
107 questions
MCQ 1071-mark 107
Questions 51–55 of 107
For a pipelined CPU with a single ALU, consider the following situations
1. The (j + 1)-th instruction uses the result of j-th instruction as an operand
2. The execution of a conditional jump instruction
3. The j - th and j + 1 - st instructions require the ALU at the same time
Which of the above can cause a hazard?
1. The (j + 1)-th instruction uses the result of j-th instruction as an operand
2. The execution of a conditional jump instruction
3. The j - th and j + 1 - st instructions require the ALU at the same time
Which of the above can cause a hazard?
Answer: (D)
A hazard is a situation that can stop the next instruction from going ahead in the next cycle.
1. The (j + 1)-th instruction uses the result of the j-th: this is a data hazard (RAW), since the result may not be ready yet.
2. A conditional jump: this is a control hazard, since the next instruction to fetch is not known until the jump is resolved.
3. Both instructions need the ALU in the same cycle: this is a structural hazard, since there is only one ALU.
All three can cause a hazard.
Answer: (D)
1. The (j + 1)-th instruction uses the result of the j-th: this is a data hazard (RAW), since the result may not be ready yet.
2. A conditional jump: this is a control hazard, since the next instruction to fetch is not known until the jump is resolved.
3. Both instructions need the ALU in the same cycle: this is a structural hazard, since there is only one ALU.
All three can cause a hazard.
Answer: (D)
Consider the following C function.
float f(float x, int y)
{
float p, s; int i;
for (s=1, p=1, i=1; i < y; i ++)
{
p*= x/i;
s+=p;
}
return s;
} For large values of y, the return value of the function f best approximatesAnswer: (B)
Here is a simpler form of the given program:
float f(float x, int y) {
float p=1, s=1;
int i;
for (i=1; i<y; i++) {
p = p * (x/i);
s = s + p;
}
return s;
}
We can move the initialization out of the for loop, because the loop does not check any condition on .
So, option B is the answer.
Consider the C program shown below.
#include <stdio.h>
#define print(x) printf("%d ", x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y + 2;
Q(x);
*y = x - 1;
print(x);
}
main(void)
{
x = 5;
P(&x);
print(x);
} The output of this program isAnswer: (A)
Set global x in main:
main: x = 5; //Global x becomes 5
Local x in P:
P: int x = *y + 2; //local x in P becomes 5+2 = 7
Local z in Q:
Q: z+=x; //local z in Q becomes 7 + 5 = 12
Output from Q:
Q: print(z); //prints 12
Update through pointer in P:
P: *y = x - 1; //content of address of local variable y (same as global variable x) becomes 7 - 1 = 6
Output from P:
P: print(x); //prints local variable x in P = 7
Output from main:
main: print(x); //prints the global variable x = 6
The program prints 12 7 6.
Answer: (A)
The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.
global int i = 100, j = 5;
void P(x)
{
int i = 10;
print(x + 10);
i = 200;
j = 20;
print(x);
}
main()
{
P(i + j);
} If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program areAnswer: (B)
Call by name replaces x by the expression i + j in the body of P and evaluates it again at every use. With dynamic scoping, a name means the most recent active declaration at that moment. Inside P that is P's local i (= 10) and the global j (= 5), since main has no variables of its own.
First print: x + 10 = i + j + 10 = 10 + 5 + 10 = 25.
Then i = 200 changes P's local i, and j = 20 changes the global j.
Second print: x = i + j = 200 + 20 = 220.
Output: 25, 220. Option (A) 115, 220 needs the global i = 100 in the first print but the local i = 200 in the second, which no single scoping rule gives.
Answer: (B)
First print: x + 10 = i + j + 10 = 10 + 5 + 10 = 25.
Then i = 200 changes P's local i, and j = 20 changes the global j.
Second print: x = i + j = 200 + 20 = 220.
Output: 25, 220. Option (A) 115, 220 needs the global i = 100 in the first print but the local i = 200 in the second, which no single scoping rule gives.
Answer: (B)
The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.
global int i = 100, j = 5;
void P(x)
{
int i = 10;
print(x + 10);
i = 200;
j = 20;
print(x);
}
main()
{
P(i + j);
} If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program areAnswer: (D)
With static scoping, the names in the actual argument are looked up in the caller's environment, which here is the global i and j. P's local i does not hide them. In call by need, the argument is evaluated at the first use of x, and that value is stored and reused for later uses.
First use:
Then
Second use:
The output is 115, 105.
Answer: (D)
First use:
print(x + 10) evaluates , so it prints .Then
i = 200 changes only P's local i, and j = 20 changes the global j.Second use:
print(x) does not evaluate again. It uses the stored value, so it prints . (Call by name would evaluate again and give .)The output is 115, 105.
Answer: (D)