GATE 2017 (Session 1)
64 questions
MCQ 44 · NAT 201-mark 30 · 2-mark 34
Questions 46–50 of 64
Consider the C functions foo and bar given below:
int foo (int val ) {
int x = 0;
while (val > 0) {
x = x + foo(val--);
}
return val ;
}
int bar (int val ) {
int x = 0;
while (val > 0) {
x = x + bar(val-1);}
return val ;
} Invocations of foo(3) and bar(3) will result in:Answer: (C)
Let us check each function:
- foo(3): the call foo(val--) sends the old value 3. So foo(3) keeps calling foo(3) again and again stack overflow abnormal termination.
- bar(3) bar(2) bar(1) bar(0), which returns 0. Back in bar(1), val never changes, so the condition while (val 0) stays true and the loop runs forever infinite loop.
Answer: Abnormal termination and infinite loop respectively
- foo(3): the call foo(val--) sends the old value 3. So foo(3) keeps calling foo(3) again and again stack overflow abnormal termination.
- bar(3) bar(2) bar(1) bar(0), which returns 0. Back in bar(1), val never changes, so the condition while (val 0) stays true and the loop runs forever infinite loop.
Answer: Abnormal termination and infinite loop respectively
Consider the context-free grammars over the alphabet {a,b,c} given below. S and T are non-terminals
The language is
The language is
Answer: (B)
Here (S makes matching a's and b's around T, and T makes the c's) and .
A string of with starts with a. Every string of is empty or starts with b or c, so no such string is in both. So only is left in , giving , and each is also in (with ). So the intersection is , which is infinite and regular.
Answer: (B)
A string of with starts with a. Every string of is empty or starts with b or c, so no such string is in both. So only is left in , giving , and each is also in (with ). So the intersection is , which is infinite and regular.
Answer: (B)
Consider the following languages over the alphabet .
Let and
Which of the following are context-free languages ?
I.
II.
Let and
Which of the following are context-free languages ?
I.
II.
Answer: (A)
- and are each context-free: for and the mirror grammar for . CFLs are closed under union, so is a CFL. I is true.
- A string in has the form with (from ) and (from ). So . This is not context-free: in the pumped part cannot contain both a's and c's, so pumping up raises one letter's count and leaves another at p. II is false.
Answer: (A)
- A string in has the form with (from ) and (from ). So . This is not context-free: in the pumped part cannot contain both a's and c's, so pumping up raises one letter's count and leaves another at p. II is false.
Answer: (A)
Let A and B be finite alphabets and let # be a symbol outside both A and B. Let f be a total function from A* to B*. We say f is computable if there exists a Turing machine M which given an input x in A*, always halts with f(x) on its tape. Let denote the language {x#f(x)|xA*}. Which of the following statements is true:
Answer: (A)
The alphabets are finite, so the strings of can be listed one by one in order.
Suppose f is computable. Given the input , run the machine for f on x and compare the output with y. It always halts, so is recursive.
Suppose is recursive, with a decider D. Given x, go through the strings y of in order and run D on . Since f is total, the string is found after finitely many tries, and the search stops. So f is computable.
So f is computable if and only if is recursive.
(C) and (D) say that the converse fails, but the converse holds, so both are false. (B) can also be proved by running a recogniser on all in parallel (dovetailing), but (A) is the stronger and direct statement.
Answer: (A)
Suppose f is computable. Given the input , run the machine for f on x and compare the output with y. It always halts, so is recursive.
Suppose is recursive, with a decider D. Given x, go through the strings y of in order and run D on . Since f is total, the string is found after finitely many tries, and the search stops. So f is computable.
So f is computable if and only if is recursive.
(C) and (D) say that the converse fails, but the converse holds, so both are false. (B) can also be proved by running a recogniser on all in parallel (dovetailing), but (A) is the stronger and direct statement.
Answer: (A)
Recall that Belady's anomaly is that the pages-fault rate may increase as the number of allocated frames increases. Now consider the following statements:
S1: Random page replacement algorithm (where a page chosen at random is replaced) suffers from Belady's anomaly
S2: LRU page replacement algorithm suffers from Belady's anomaly
Which of the following is CORRECT ?
S1: Random page replacement algorithm (where a page chosen at random is replaced) suffers from Belady's anomaly
S2: LRU page replacement algorithm suffers from Belady's anomaly
Which of the following is CORRECT ?
Answer: (B)
- S1: Random replacement is not a stack algorithm. The pages held with k frames need not be a subset of the pages held with k + 1 frames, because the random choices differ. So adding a frame can lead to more faults, and Belady's anomaly can occur. S1 is TRUE.
- S2: LRU is a stack algorithm. With k frames it holds the k most recently used pages, and these are always a subset of the k + 1 most recently used pages. So more frames never add faults, and Belady's anomaly does not occur. S2 is FALSE.
Answer: (B)
- S2: LRU is a stack algorithm. With k frames it holds the k most recently used pages, and these are always a subset of the k + 1 most recently used pages. So more frames never add faults, and Belady's anomaly does not occur. S2 is FALSE.
Answer: (B)