GATE 2006
191 questions
MCQ 1911-mark 191
Questions 141–145 of 191
Consider the following grammar.
S S * E
S E
E F + E
E F
F F id
Consider the following LR(0) items corresponding to the grammar above.
(i) S S * .E
(ii) E F. + E
(iii) E F + .E
Given the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar?
S S * E
S E
E F + E
E F
F F id
Consider the following LR(0) items corresponding to the grammar above.
(i) S S * .E
(ii) E F. + E
(iii) E F + .E
Given the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar?
Answer: (D)
In an LR(0) state, every kernel item (an item whose dot is not at the start) was reached by the same grammar symbol, and that symbol is just before the dot in each of them. Closure only adds items with the dot at the start, so these three items cannot come from a closure; each one is a kernel item of the state it appears in.
- (i) has * before the dot, so its state is entered on *.
- (ii) has F before the dot, so its state is entered on F.
- (iii) has + before the dot, so its state is entered on +.
The three symbols are different, so no two of the items can be in the same set.
Answer: (D)
- (i) has * before the dot, so its state is entered on *.
- (ii) has F before the dot, so its state is entered on F.
- (iii) has + before the dot, so its state is entered on +.
The three symbols are different, so no two of the items can be in the same set.
Answer: (D)
Consider the following translation scheme.
S ER
R * E{print('*');}R|
EF+E{print('+');}|F
F(S)|id{print(id.value);}
Here id is a taken that represents an integer and id.value represents the corresponding integer value. For an input '2*3+4', this translation scheme prints
S ER
R * E{print('*');}R|
EF+E{print('+');}|F
F(S)|id{print(id.value);}
Here id is a taken that represents an integer and id.value represents the corresponding integer value. For an input '2*3+4', this translation scheme prints
Answer: (D)
Build the parse tree for the input.
Walk the tree depth first from left to right. Each print action runs when the walk reaches it:
1. for the first operand: prints 2.
2. The token * is read. Inside the next , the first prints 3.
3. The token + is read. The inner prints 4.
4. The action of now runs: prints +.
5. The action of now runs: prints *.
6. prints nothing.
The output is , that is 234+*.
Answer: (D)
Walk the tree depth first from left to right. Each print action runs when the walk reaches it:
1. for the first operand: prints 2.
2. The token * is read. Inside the next , the first prints 3.
3. The token + is read. The inner prints 4.
4. The action of now runs: prints +.
5. The action of now runs: prints *.
6. prints nothing.
The output is , that is 234+*.
Answer: (D)
Consider the following C code segment.
for (i = 0; i < n; i++)
{
for (j=0; j < n; j++)
{
if (i%2)
{
x += (4*j + 5*i);
y += (7 + 4*j);
}
}
}
Which one of the following is false?Answer: (D)
(A)
(B)
(C) Multiplication can be replaced by addition. In the inner loop,
(D) Every statement can be reached (the
The false statement is (D).
Answer: (D)
i%2 and 5*i do not change inside the inner loop, so they are loop-invariant. True.(B)
4*j appears in two statements, so it can be computed once and reused (common subexpression elimination). True.(C) Multiplication can be replaced by addition. In the inner loop,
4*j can be kept in a variable t that is set to 0 before the inner loop and gets t += 4 in each pass. Similarly, 5*i can be kept in a variable p that is set to 0 before the outer loop and gets p += 5 at the end of each outer pass. True.(D) Every statement can be reached (the
if body runs for odd i), so there is no dead code to remove. False.The false statement is (D).
Answer: (D)
The process state transition diagram of an operating system is as given below. Which of the following must be FALSE about the above operating system?
Answer: (B)
In the diagram, a process goes Start Ready Running Terminated, and Running Blocked Ready. There is no arrow from Running to Ready. So a running process leaves the CPU only when it waits (blocks) or exits, and it is never forced off by a timer or a higher-priority process. That is non-preemptive scheduling.
(A) can be true: when a process blocks, the CPU goes to another ready process, which is how multiprogramming works.
(B) must be false, because preemption needs a Running Ready transition.
(C) is true for the same reason.
(D) can be true or false: the diagram does not say how many users there are, and several processes do not mean several users.
Answer: (B)
(A) can be true: when a process blocks, the CPU goes to another ready process, which is how multiprogramming works.
(B) must be false, because preemption needs a Running Ready transition.
(C) is true for the same reason.
(D) can be true or false: the diagram does not say how many users there are, and several processes do not mean several users.
Answer: (B)
The wait and signal operations of a monitor are implemented using semaphores as follows. In the following,
x is a condition variable,
mutex is a semaphore initialized to 1,
x_sem is a semaphore initialized to 0,
x_count is the number of processes waiting on semaphore x_sem, initially 0,
next is a semaphore initialized to 0,
next_count is the number of processes waiting on semaphore next, initially 0.
The body of each procedure that is visible outside the monitor is replaced with the following:
Each occurrence of x.wait is replaced with the following:
Each occurrence of x.signal is replaced with the following:
For correct implementation of the monitor, statements E1 and E2 are, respectively,
x is a condition variable,
mutex is a semaphore initialized to 1,
x_sem is a semaphore initialized to 0,
x_count is the number of processes waiting on semaphore x_sem, initially 0,
next is a semaphore initialized to 0,
next_count is the number of processes waiting on semaphore next, initially 0.
The body of each procedure that is visible outside the monitor is replaced with the following:
P(mutex);
...
body of procedure
...
if (next_count > 0)
V(next);
else
V(mutex); Each occurrence of x.wait is replaced with the following:
x_count = x_count + 1;
if (next_count > 0)
V(next);
else
V(mutex);
------------------------------------------------------------ E1;
x_count = x_count - 1;
Each occurrence of x.signal is replaced with the following:
if (x_count > 0)
{
next_count = next_count + 1;
------------------- E2;
P(next);
next_count = next_count - 1;
} For correct implementation of the monitor, statements E1 and E2 are, respectively,
Answer: (D)
In x.wait, x_count is increased, then the process gives up the monitor with V(next) or V(mutex), and later x_count is decreased. x_count counts the processes waiting on x_sem, so the process must sleep on x_sem in between. So E1 is P(x_sem).
In x.signal, if x_count > 0, a process is waiting on x_sem. The signaller must wake it with V(x_sem), which is E2. Then P(next) makes the signaller wait until the woken process leaves the monitor or waits again. The signaller counts itself in next_count, and the exit code wakes it with V(next).
The other options are wrong:
(A) E2 = V(next) wakes a process waiting on next, not the one waiting on x_sem.
(B) E1 = V(next) does not make the waiter sleep on x_sem, and E2 = P(x_sem) would block the signaller instead of waking a waiter.
(C) E1 = P(next) makes the waiter sleep on next, not on x_sem.
Answer: (D)
In x.signal, if x_count > 0, a process is waiting on x_sem. The signaller must wake it with V(x_sem), which is E2. Then P(next) makes the signaller wait until the woken process leaves the monitor or waits again. The signaller counts itself in next_count, and the exit code wakes it with V(next).
The other options are wrong:
(A) E2 = V(next) wakes a process waiting on next, not the one waiting on x_sem.
(B) E1 = V(next) does not make the waiter sleep on x_sem, and E2 = P(x_sem) would block the signaller instead of waking a waiter.
(C) E1 = P(next) makes the waiter sleep on next, not on x_sem.
Answer: (D)