GATE 2017 (Session 2)
65 questions
MCQ 45 · NAT 201-mark 30 · 2-mark 35
Questions 46–50 of 65
The pre-order transversal of a binary search tree is given by 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20. Then the post-order traversal of this tree is:
Answer: (B)
Make the BST from the preorder: root 12. Left subtree: 8, with left child 6 (children 2, 7) and right child 9 (right child 10). Right subtree: 16, with left child 15 and right child 19 (children 17, 20).
Postorder: 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12
Answer: 2,7,6,10,9,8,15,17,20,19,16,12
Postorder: 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12
Answer: 2,7,6,10,9,8,15,17,20,19,16,12
Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variables x, y, q and r are all unsigned int.
while (r >= y) {
r = r - y;
q = q +1;
} Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x==(y*q+r)?Answer: (C)
Each pass changes r to and q to , so does not change. If is true before the loop, it is true after every pass and when the loop ends.
In (C), and give at the start. Also makes r fall by at least 1 each pass, so r becomes less than y and the loop stops.
Why the others fail:
- (A) gives , but x is not fixed to 0. Also, if then is always true and the loop never ends.
- (B) q is not fixed. For example ends with , and .
- (D) r is not fixed. For example ends with , and .
Answer: (C)
In (C), and give at the start. Also makes r fall by at least 1 each pass, so r becomes less than y and the loop stops.
Why the others fail:
- (A) gives , but x is not fixed to 0. Also, if then is always true and the loop never ends.
- (B) q is not fixed. For example ends with , and .
- (D) r is not fixed. For example ends with , and .
Answer: (C)
Consider the following C function
int fun (int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1 ; j < n ; j+=i) {
printf ("%d %d ", i, j);
}
}
} The time complexity of fun in terms of notation isAnswer: (C)
The inner loop goes up in steps of i, so for each value of i it runs about n/i times. Adding this over all i gives .
Answer:
Answer:
Let denote that transition function and denote the extended transition function of the - NFA whose transition table is given below:
Then is ____
Then is ____
Answer: (C)
Take the -closures. The closure of q2 is {q2, q0}.
- Read a: q0 q1 take the closure to get {q1, q2, q0}
- Read b: q1 q3, and q0 q0 take the closure to get {q3, q0, q2}
- Read a: q0 q1 take the closure to get {q1, q2, q0}
Answer: {q0, q1, q2}
- Read a: q0 q1 take the closure to get {q1, q2, q0}
- Read b: q1 q3, and q0 q0 take the closure to get {q3, q0, q2}
- Read a: q0 q1 take the closure to get {q1, q2, q0}
Answer: {q0, q1, q2}
Consider the following languages
is a prime number}
Which of the following are CORRECT ?
I. is context-free but not regular.
II. is not context-free.
III. is not context-free but recursive.
IV. is deterministic context-free.
is a prime number}
Which of the following are CORRECT ?
I. is context-free but not regular.
II. is not context-free.
III. is not context-free but recursive.
IV. is deterministic context-free.
Answer: (D)
- I: (prime lengths) is not context-free. A context-free language over a one-letter alphabet is always regular, and the primes are not regular (pumping lemma). So I is false.
- II: is context-free: the a's are free, and for push two symbols per b and pop one per c. So II is false.
- III: is not context-free. Pump : the pumped part cannot touch both the a's and the c's. If it misses the a's, they stay at p, so b would have to stay p and c stay 2p, but pumping changes at least one count. If it misses the c's, they stay at 2p, so a and b would have to stay p, again impossible. It is recursive, because a program that checks the form and then tests always halts. So III is true.
- IV: is a deterministic CFL: push a's, pop for b's, accept on an empty stack. So IV is true.
Answer: (D)
- II: is context-free: the a's are free, and for push two symbols per b and pop one per c. So II is false.
- III: is not context-free. Pump : the pumped part cannot touch both the a's and the c's. If it misses the a's, they stay at p, so b would have to stay p and c stay 2p, but pumping changes at least one count. If it misses the c's, they stay at 2p, so a and b would have to stay p, again impossible. It is recursive, because a program that checks the form and then tests always halts. So III is true.
- IV: is a deterministic CFL: push a's, pop for b's, accept on an empty stack. So IV is true.
Answer: (D)