GATE 2023
65 questions
MCQ 34 · MSQ 15 · NAT 161-mark 30 · 2-mark 35
Questions 36–40 of 65
Consider the following program:
Which one of the following options represents the activation tree corresponding to the main function?
int main()
{
f1();
f2(2);
f3();
return(0);
}
int f1()
{
return(1);
}
int f2(int X)
{
f3();
if (X==1)
return f1();
else
return (X*f2(X-1));
}
int f3()
{
return(5);
}
Which one of the following options represents the activation tree corresponding to the main function?
Answer: (A)
Follow the function calls:
- main makes three calls: f1(), f2(2) and f3() so it has 3 children.
- f2(2) first calls f3(). Because X 1, it then calls f2(1).
- f2(1) first calls f3(). Because X == 1, it then calls f1().
The tree is: main {f1, f2, f3}; f2 {f3, f2}; the inner f2 {f3, f1}. This is the tree in (A).
Answer: A
- main makes three calls: f1(), f2(2) and f3() so it has 3 children.
- f2(2) first calls f3(). Because X 1, it then calls f2(1).
- f2(1) first calls f3(). Because X == 1, it then calls f1().
The tree is: main {f1, f2, f3}; f2 {f3, f2}; the inner f2 {f3, f1}. This is the tree in (A).
Answer: A
Consider the control flow graph shown.
Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?
Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?
Answer: (D)
Uses and definitions: B1 defines i, j, a. B2 uses and defines i, j. B3 defines a. B4 uses a and defines i. Flow: B1 → B2; B2 → B3 and B4; B3 → B4; B4 → B2 or exit.
A variable is live at a point if it may be used later before being redefined. For each block, , and OUT is the union of IN of the next blocks:
-
- ,
Start with all sets empty and repeat until nothing changes.
Round 1 (nothing known yet about OUT(B4)):
- IN(B4) = {a}; OUT(B3) = IN(B4) = {a}; IN(B3) = {}
- OUT(B2) = IN(B3) IN(B4) = {a}; IN(B2) = {i, j, a}
Round 2 (use the new IN(B2)):
- OUT(B4) = IN(B2) = {a, i, j}; IN(B4) = {a, j}
- OUT(B3) = IN(B4) = {a, j}; IN(B3) = {j}
- OUT(B2) = IN(B3) IN(B4) = {a, j}; IN(B2) = {a, i, j}, which is unchanged
- OUT(B1) = IN(B2) = {a, i, j}
Nothing changes in round 3, so these are the final sets.
Answer: (D)
A variable is live at a point if it may be used later before being redefined. For each block, , and OUT is the union of IN of the next blocks:
-
- ,
Start with all sets empty and repeat until nothing changes.
Round 1 (nothing known yet about OUT(B4)):
- IN(B4) = {a}; OUT(B3) = IN(B4) = {a}; IN(B3) = {}
- OUT(B2) = IN(B3) IN(B4) = {a}; IN(B2) = {i, j, a}
Round 2 (use the new IN(B2)):
- OUT(B4) = IN(B2) = {a, i, j}; IN(B4) = {a, j}
- OUT(B3) = IN(B4) = {a, j}; IN(B3) = {j}
- OUT(B2) = IN(B3) IN(B4) = {a, j}; IN(B2) = {a, i, j}, which is unchanged
- OUT(B1) = IN(B2) = {a, i, j}
Nothing changes in round 3, so these are the final sets.
Answer: (D)
Consider the two functions incr and decr shown below.
Suppose there are two implementations of the semaphore , as follows:
I-1: s is a binary semaphore initialized to 1.
I-2: s is a counting semaphore initialized to 2.
Let V1, V2 be the values of X at the end of execution of all the threads with implementations I-1, I-2, respectively.
Which one of the following choices corresponds to the minimum possible values of V1, V2, respectively?
incr(){
wait(s);
X = X+1;
signal(s);
}
decr(){
wait(s);
X = X-1;
signal(s);
}
There are 5 threads each invoking incr once, and 3 threads each invoking decr once, on the same shared variable X. The initial value of X is 10.Suppose there are two implementations of the semaphore , as follows:
I-1: s is a binary semaphore initialized to 1.
I-2: s is a counting semaphore initialized to 2.
Let V1, V2 be the values of X at the end of execution of all the threads with implementations I-1, I-2, respectively.
Which one of the following choices corresponds to the minimum possible values of V1, V2, respectively?
Answer: (C)
I-1 (binary semaphore = 1): Only one thread can be inside at a time, so every update is applied. This is the only possible result: .
I-2 (counting semaphore = 2): Two threads can be inside together, so some updates can be lost.
Lower limit: every value written to X is the value that thread read, plus 1 (incr) or minus 1 (decr). That value was itself written by an earlier thread (or is the initial 10). Following this chain back to 10, each thread appears at most once, so the chain has at most 3 decrements. So X can never end below .
7 can be reached by losing all 5 increments:
- One decr thread reads X = 10 and is then preempted.
- All 5 incr threads run one after another (using the second slot). X becomes 15.
- The first decr now writes . This wipes out all 5 increments.
- The other 2 decr threads run normally. X becomes 7.
So , and the pair is 12, 7.
Answer: (C)
I-2 (counting semaphore = 2): Two threads can be inside together, so some updates can be lost.
Lower limit: every value written to X is the value that thread read, plus 1 (incr) or minus 1 (decr). That value was itself written by an earlier thread (or is the initial 10). Following this chain back to 10, each thread appears at most once, so the chain has at most 3 decrements. So X can never end below .
7 can be reached by losing all 5 increments:
- One decr thread reads X = 10 and is then preempted.
- All 5 incr threads run one after another (using the second slot). X becomes 15.
- The first decr now writes . This wipes out all 5 increments.
- The other 2 decr threads run normally. X becomes 7.
So , and the pair is 12, 7.
Answer: (C)
Consider the context-free grammar below
where and are non-terminals, and and are terminal symbols. The starting non-terminal is .
Which one of the following statements is CORRECT?
where and are non-terminals, and and are terminal symbols. The starting non-terminal is .
Which one of the following statements is CORRECT?
Answer: (B)
Look at X first: the rule puts a's on the left, and the rule puts b's on the right. The string ends with a single a or b in the middle. So .
The rule only puts more a's on the left and more b's on the right of X. So the strings stay inside , and they cover all of it (take directly). So .
(A) is wrong: "ba" is in but not in , and the empty string is not in either.
(C) is wrong: contains "ba" (take and the last symbol a), but "ba" cannot be derived, since an a can never come after a b in .
(D) is wrong: is regular, as it is given by the regular expression above.
Answer: (B)
The rule only puts more a's on the left and more b's on the right of X. So the strings stay inside , and they cover all of it (take directly). So .
(A) is wrong: "ba" is in but not in , and the empty string is not in either.
(C) is wrong: contains "ba" (take and the last symbol a), but "ba" cannot be derived, since an a can never come after a b in .
(D) is wrong: is regular, as it is given by the regular expression above.
Answer: (B)
Consider the pushdown automaton (PDA) below, which runs on the input alphabet , has stack alphabet , and has three states , with being the start state. A transition from state to state , labelled , where is an input symbol or , is a stack symbol, and is a string of stack symbols, represents the fact that in state , the PDA can read from the input, with on the top of its stack, pop from the stack, push in the string on the stack, and go to state . In the initial configuration, the stack has only the symbol in it. The PDA accepts by empty stack.
Which one of the following options correctly describes the language accepted by ?
Which one of the following options correctly describes the language accepted by ?
Answer: (A)
Look at the PDA transitions:
- In s, each a pushes one A, so m a's put m A's on the stack.
- The first b takes the PDA to p and pops an A. Each next b pops one more A.
- The stack can only be emptied by going to q. Both moves into q need an A on top. In q, the A's that are left and get popped.
So at least one A must remain after the b's are read. This gives , and also (with no a's, there is no A to go to q).
Answer:
- In s, each a pushes one A, so m a's put m A's on the stack.
- The first b takes the PDA to p and pops an A. Each next b pops one more A.
- The stack can only be emptied by going to q. Both moves into q need an A on top. In q, the A's that are left and get popped.
So at least one A must remain after the b's are read. This gives , and also (with no a's, there is no A to go to q).
Answer: