GATE 2016 (Session 1)
65 questions
MCQ 39 · NAT 261-mark 30 · 2-mark 35
Questions 51–55 of 65
Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns the element at the head of the queue Q without removing it from Q. Similarly Top(S) returns the element at the top of S without removing it from S. Consider the algorithm given below.
The maximum possible number of iterations of the while loop in the algorithm is_________.
while Q is not Empty do
if S is Empty OR Top(S) ≤ Head(Q) then
x := Dequeue(Q);
Push(S,x);
else
x := Pop(S);
Enqueue(Q,x);
end
endThe maximum possible number of iterations of the while loop in the algorithm is_________.
Answer: 256
Each iteration is a push (Dequeue then Push) or a pop (Pop then Enqueue). The stack always holds its elements in increasing order from bottom to top. Assume the numbers are distinct; equal numbers only reduce the count. Let be the largest number of iterations for n numbers.
Look at the smallest number m. Before m reaches the head of the queue, only the k elements ahead of it are pushed, . A popped element goes to the tail, behind m, so each of these k elements is pushed once and popped at most once. That is at most iterations. When m reaches the head, it pops everything still on the stack (already counted above) and is pushed: one more iteration. So this phase takes at most iterations.
After that, m stays at the bottom of the stack, because no head is smaller than it, and the test always lets the next element through. So the other elements behave like a fresh run. This gives with , so .
A decreasing queue reaches this bound. The head n is pushed and then popped by n-1, which is pushed and popped by n-2, and so on, and m is pushed last. That is exactly iterations and leaves the other n-1 elements in decreasing order. Check: n = 2 with queue [2, 1] takes 4 iterations, and n = 3 with [3, 2, 1] takes 5 + 4 = 9.
For : .
Answer: 256
Look at the smallest number m. Before m reaches the head of the queue, only the k elements ahead of it are pushed, . A popped element goes to the tail, behind m, so each of these k elements is pushed once and popped at most once. That is at most iterations. When m reaches the head, it pops everything still on the stack (already counted above) and is pushed: one more iteration. So this phase takes at most iterations.
After that, m stays at the bottom of the stack, because no head is smaller than it, and the test always lets the next element through. So the other elements behave like a fresh run. This gives with , so .
A decreasing queue reaches this bound. The head n is pushed and then popped by n-1, which is pushed and popped by n-2, and so on, and m is pushed last. That is exactly iterations and leaves the other n-1 elements in decreasing order. Check: n = 2 with queue [2, 1] takes 4 iterations, and n = 3 with [3, 2, 1] takes 5 + 4 = 9.
For : .
Answer: 256
Consider the following context-free grammars:
G1: SaS|B,
Bb|bB
G2: SaA|bB,
AaA|B|,
BbB|
Which one of the following pair of languages is generated by G1 and G2, respectively?
G1: SaS|B,
Bb|bB
G2: SaA|bB,
AaA|B|,
BbB|
Which one of the following pair of languages is generated by G1 and G2, respectively?
Answer: (D)
Let us check each grammar:
- G1: gives any number of a's, then one B. gives at least one b. So G1 gives .
- G2: gives , and gives . Starting with aA we get . Starting with bB we get . The union is , every string of a's then b's except the empty string.
Option (A) is wrong because G1 cannot give "a" (no b). Option (B) is wrong for both: its first language needs and , so it misses "b", which G1 gives, and its second language ( or ) also contains the empty string, which G2 cannot give. Option (C) is wrong for G2: it needs both and , so it misses "a" and "b".
Answer: (D)
- G1: gives any number of a's, then one B. gives at least one b. So G1 gives .
- G2: gives , and gives . Starting with aA we get . Starting with bB we get . The union is , every string of a's then b's except the empty string.
Option (A) is wrong because G1 cannot give "a" (no b). Option (B) is wrong for both: its first language needs and , so it misses "b", which G1 gives, and its second language ( or ) also contains the empty string, which G2 cannot give. Option (C) is wrong for G2: it needs both and , so it misses "a" and "b".
Answer: (D)
Consider the transition diagram of a PDA given below with input alphabet ={a,b} and stack alphabet ={X,Z}. Z is the initial stack symbol. Let L denote the language accepted by the PDA.
Which one of the following is TRUE?
Which one of the following is TRUE?
Answer: (D)
Read the diagram. In the start state (also final), each pushes an on top of . Reading with on top pops it and moves to the middle state, where each further pops one . When is on top, an move goes to the last (final) state. So the PDA accepts for (stopping in the start state), and for . Thus .
The PDA is deterministic: no state has a choice for the same input and stack top. In the start state, is used with or and only with . In the middle state, needs on top and the move needs on top, so they never compete. So is a deterministic CFL.
Other options: (A) names the language , which is not , and the claim is not about . (B) says has no DPDA, but the PDA above is one. (C) is false because every CFL is accepted by a Turing machine that halts on every input.
Answer: (D)
The PDA is deterministic: no state has a choice for the same input and stack top. In the start state, is used with or and only with . In the middle state, needs on top and the move needs on top, so they never compete. So is a deterministic CFL.
Other options: (A) names the language , which is not , and the claim is not about . (B) says has no DPDA, but the PDA above is one. (C) is false because every CFL is accepted by a Turing machine that halts on every input.
Answer: (D)
Let X be a recursive language and Y be a recursively enumerable but not recursive language. Let W and Z be two languages such that reduces to W, and Z reduces to (reduction means the standard many-one-reduction). Which one of the following statements is TRUE?
Answer: (C)
Let us check each statement:
- Y is RE but not recursive, so is not RE. reduces to W. If W were RE, then would be RE too. So W cannot be RE.
- X is recursive, so is also recursive. Z reduces to , so Z is recursive.
Answer: W is not recursively enumerable and Z is recursive
- Y is RE but not recursive, so is not RE. reduces to W. If W were RE, then would be RE too. So W cannot be RE.
- X is recursive, so is also recursive. Z reduces to , so Z is recursive.
Answer: W is not recursively enumerable and Z is recursive
The attributes of three arithmetic operators in some programming language are given below.
The value of the expression 2-5+1-7*3 in this language is_______ .
The value of the expression 2-5+1-7*3 in this language is_______ .
Answer: 9
Order of precedence: + is highest * is lowest. So * is done last:
Inside the bracket, + goes first, 5 + 1 = 6 2 6 7. is right associative: .
Answer: 9
Inside the bracket, + goes first, 5 + 1 = 6 2 6 7. is right associative: .
Answer: 9