GATE 2015 (Session 3)
65 questions
MCQ 45 · NAT 201-mark 30 · 2-mark 35
Questions 41–45 of 65
Language is polynomial time reducible to language . Language is polynomial time reducible to , which in turn is polynomial time reducible to language . Which of the following is/are true?
I. if , then
II. if or , then
III. , if and only if
IV. if , then and
I. if , then
II. if or , then
III. , if and only if
IV. if , then and
Answer: (C)
Write for "A is polynomial time reducible to B". If and , then : convert the input with the reduction, then run the polynomial algorithm for B. Membership in P moves only backward along a reduction.
Given: and .
I: and , so . True.
IV: if , then (by I), and since and both reduce to , both are in P. True.
II: this goes forward, from or to . An easy problem can reduce to a hard one, so it does not follow. False.
III: and each reduce to , but not to each other, so one being in P says nothing about the other. False.
Answer: (C)
Given: and .
I: and , so . True.
IV: if , then (by I), and since and both reduce to , both are in P. True.
II: this goes forward, from or to . An easy problem can reduce to a hard one, so it does not follow. False.
III: and each reduce to , but not to each other, so one being in P says nothing about the other. False.
Answer: (C)
Consider the following C program.
#include <stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main( )
{
int x = 1;
x += f1( ) + f2( ) + f3( ) + f2( );
printf("%d", x);
return 0;
}
int f1() { int x = 25; x++; return x;}
int f2() { static int x = 50; x++; return x;}
int f3() { x *= 10; return x;} The output of the program is ________.Answer: 230
Local x in main starts at 1.
- f1() returns 26, because it uses its own local x.
- f2() returns 51, because the static x goes up to 51.
- f3() works on the global x: 10 10 = 100
- The second f2() call returns 52.
So x = 1 + 26 + 51 + 100 + 52 = 230
Answer: 230
- f1() returns 26, because it uses its own local x.
- f2() returns 51, because the static x goes up to 51.
- f3() works on the global x: 10 10 = 100
- The second f2() call returns 52.
So x = 1 + 26 + 51 + 100 + 52 = 230
Answer: 230
Consider the following C program.
#include <stdio.h>
int main( )
{
static int a[ ] = {10, 20, 30, 40, 50};
static int *p[ ] = {a, a+3, a+4, a+1, a+2};
int **ptr = p;
ptr++;
printf("%d%d", ptr-p,**ptr);
}The output of the program is ______________.Answer: 140
The statement ptr++ moves ptr forward, so it now points to p[1].
- The difference ptr p = 1
- **ptr = *p[1] = *(a + 3) = 40
The format "%d%d" prints the two values one after the other: 140
Answer: 140
- The difference ptr p = 1
- **ptr = *p[1] = *(a + 3) = 40
The format "%d%d" prints the two values one after the other: 140
Answer: 140
Which of the following languages are context-free?
Answer: (B)
- : the pairs are nested, with m outside and n inside. Push the a's, push the b's, pop one b for each a, then pop one a for each b. Grammar: . It is a CFL.
- : the pairs cross (the first a's go with the second a's, the first b's with the second b's), which a stack cannot do. Pump : the pumped part touches at most two neighbouring blocks, and the partner of a block (1st with 3rd, 2nd with 4th) is never a neighbour. So pumping changes a block without its partner, or breaks the form . It is not a CFL.
- : push every a, pop two a's for each b, and accept if exactly one a is left. Grammar: . It is a CFL.
Answer: (B)
- : the pairs cross (the first a's go with the second a's, the first b's with the second b's), which a stack cannot do. Pump : the pumped part touches at most two neighbouring blocks, and the partner of a block (1st with 3rd, 2nd with 4th) is never a neighbour. So pumping changes a block without its partner, or breaks the form . It is not a CFL.
- : push every a, pop two a's for each b, and accept if exactly one a is left. Grammar: . It is a CFL.
Answer: (B)
Consider the following policies for preventing deadlock in a system with mutually exclusive resources.
I. Processes should acquire all their resources at the beginning of execution. If any resource is not available, all resources acquired so far are released
II. The resources are numbered uniquely, and processes are allowed to request for resources only in increasing resource numbers
III. The resources are numbered uniquely, and processes are allowed to request for resources only in decreasing resource numbers
IV. The resources are numbered uniquely. A process is allowed to request only for a resource with resource number larger than its currently held resources
Which of the above policies can be used for preventing deadlock?
I. Processes should acquire all their resources at the beginning of execution. If any resource is not available, all resources acquired so far are released
II. The resources are numbered uniquely, and processes are allowed to request for resources only in increasing resource numbers
III. The resources are numbered uniquely, and processes are allowed to request for resources only in decreasing resource numbers
IV. The resources are numbered uniquely. A process is allowed to request only for a resource with resource number larger than its currently held resources
Which of the above policies can be used for preventing deadlock?
Answer: (D)
Let us look at each policy:
- Policy I breaks hold-and-wait, because a process either gets all its resources or gets none.
- Policies II, III and IV each make requests follow one fixed order (increasing or decreasing). This breaks circular wait.
So all four policies prevent deadlock.
Answer: Any one of I, II, III, and IV
- Policy I breaks hold-and-wait, because a process either gets all its resources or gets none.
- Policies II, III and IV each make requests follow one fixed order (increasing or decreasing). This breaks circular wait.
So all four policies prevent deadlock.
Answer: Any one of I, II, III, and IV