GATE- 2016-17 CS GATE Solutions GATE SOLUTION › postal-pdf › cse › GATE... · 2018-07-07 ·...
Transcript of GATE- 2016-17 CS GATE Solutions GATE SOLUTION › postal-pdf › cse › GATE... · 2018-07-07 ·...
GATE- 2016-17 CS GATE Solutions 1
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
GATE SOLUTION
Computer Science
&
Information Technology (CS)
20 Rank under AIR 100
GATE SOLUTIONGATE Solutions Subject & Topic WiseDetailed Solutions of each question
GATE- 2016-17 CS GATE Solutions 1
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
GATE SOLUTION
Computer Science
&
Information Technology (CS)
20 Rank under AIR 100
GATE SOLUTIONGATE Solutions Subject & Topic WiseDetailed Solutions of each question
GATE- 2016-17 CS GATE Solutions 1
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
GATE SOLUTION
Computer Science
&
Information Technology (CS)
20 Rank under AIR 100
GATE SOLUTIONGATE Solutions Subject & Topic WiseDetailed Solutions of each question
GATE- 2016-17 CS GATE Solutions 2
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
CONTENTS: COMPUTER SCIENCE AND INFORMATION TECHNOLOGY
1. Digital Logic
2. Computer Organization and Architecture
3. C Programming
4. Data Structures
5. Algorithms
6. Theory of Computation
7. Compiler Design
8. Operating System
9. Databases-DBMS
10. Computer Networks
11. Information Systems and Software Engineering
12. Web technologies
13. Graph Theory
14. Discrete Mathematics
New Edition with GATE 2015 Solutions is available.
1. DIGITAL LOGIC
GATE- 2016-17 CS GATE Solutions 3
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
1. Boolean Algebra and K-Maps
2. Combinational Circuits
3. Sequential Circuits
4. Number Systems
1. Boolean Algebra and K-Maps
1. Consider the following Boolean expression for F: GATE-2014: SET-1
( , , , )F P Q R S PQ PQR PQRS
The minimal sum of products form of F is
(a.) PQ + QR + QS (b.) P + Q + R + S (c.) P Q R S (d.) PR PRS P
2. The dual of a Boolean function F(x1, x2, …., xn, +, ., ’) FD, is the same expression as that of F with +
and. Swapped. F is said to be self duel if F = FD. The number of self dual functions with n Boolean
variables is GATE-2014: SET2
(a.) 2n (b.) 2n – 1 (c.) 22n
(d.)122
n
3. Consider the following minterm expression for F: GATE-2014: SET-3
( , , , ) 0,2,5,7,8,10,13,15F P Q R S The minterms 2, 7, 8 and 13 are ‘do not care’ terms. The minimal sum – of – products form for F is
(a.) QS QS (b.) QS QS
(c.) QRS QRS QRS QRS (d.) PQS PQS PQS PQS
4. A binary operation on a set of integers is defined as 2 2x y x y . Which one of the
following statements is TRUE about ?
(a) commutative but not associative (b) both commutative and associative
(c) associative but not commutative (d) neither commutative nor associative
5. What is the minimal form of the K- map shown below? Assume that X denotes a don’t care term.GATE-CSIT: 2012
GATE- 2016-17 CS GATE Solutions 4
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(a) bd (b) bd bc (c) bd abcd (d) bd bc cd
6. The simplified SOP (Sum of Product) form of the Boolean expression GATE-CSIT: 2011
(P + Q + R) . (P + Q + R) . (P + Q + R) is
(a) (P . Q + R) (b) (P Q . R) (c) (P . Q R) (d) (P . Q + R)
7. The minterms expression of f (P, Q, R) = PQ + QR + PR is GATE-CSIT : 2010
(a) 2 4 6 7m m m m (b) 0 1 3 5m m m m
(c) 0 1 6 7m m m m (d) 2 3 4 5m m m m
8. What is the Boolean expression for the output f, for the combinational logic circuit of ‘NOR’ gatesgiven below? GATE-CSIT : 2010
(a) Q + R (b) P + Q (c) P + R (d) P + Q R
9. What is the minimum number of gates required to implement the Boolean function (AB + C) if we
have to use only 2-input NOR gates? GATE-CSIT : 2009(a) 2 (b) 3 (c) 4 (d) 5
10. Given f1, f2, and f in canonical sum of products form (in decimal) for the circuit.
GATE-CSIT : 2008
GATE- 2016-17 CS GATE Solutions 5
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
1 (4, 5, 6, 7, 8)f m 3 (1, 6,15)f m (1, 6, 8,15)f m
Then 2f is
(a) (4, 6)m (b) (4, 8)m (c) (6, 8)m (d) (4, 6, 8)m
DIGITAL LOGIC ANSWER KEY1. Boolean Algebra and K – maps
1 2 3 4 5 6 7 8 9 10
a d b a b b a a b c
DIGITAL LOGIC SOLUTIONS
1. (a)
( , , , )F P Q R S PQ PQR PQRS
2. (d)
For ‘n’ variables.
Total number of Boolean functions = 22n
And number of self dual functions =122
n
3. (b)
F (P, Q, R, S) = (0, 2, 5, 7, 8, 10, 13, 15)
Where 2, 7, and 13 are don’t careK – map
GATE- 2016-17 CS GATE Solutions 6
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
4. ANS: (a)
EXP: Verifying commutative law:
2 2 2 2 Commutativex y x y y x y x
Verifying associative law:
22 2 2 2 2 ... 1x y z x y z x y z
22 2 2 2 ... 2x y z x y z x y z
From(1) and (2)it is clear that,
Not Associative.x y z x y z
5. (b)
6. (b)
Given POS expression is; ( ).( ).( ) (3,2,1)F P Q R P Q R P Q R M
SOP P QR
7. (a)
f (P, Q, R) PQ QR PR
GATE- 2016-17 CS GATE Solutions 7
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
In canonical form:
7 6 2 4
2 4 6 7
( , , ) ( ) ( ) ( )f P Q R PQ R R P P QR P Q Q R
PQR PQR PQR PQR PQR PQR
PQR PQR PQR PQR
m m m m
m m m m
8. (a)
( ).( ) ( ) ( )
( 1) ( 1)
f P Q Q R P R Q R
PQ PR Q QR PQ PR QR R
PQ PR QR Q R
Q P R R P
Q R
9. (b)
f = A.B + C = (A + C). (B + C).
So 3 NOR gates are needed.
10. (c)
f = f1. f2 + f3
Or, 2(1,6,8,15) (4,5,6,7,8). (1,6,15)m m f m
where, f1. f2 = m (only those minterms which are in common to both f1 and f2)
Using venn diagram:
GATE- 2016-17 CS GATE Solutions 8
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
f consist of all minterms in f3 and those in common to f1 and f2. As f does not contain minterm ‘4’, so‘4’ should not be in common to f1 and f2. But f1 have ‘4’, so f2 must not have ‘4’. The only option is(c).
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
2. COMPUTER ORGANIZATION1. Machine Instructions, Addressing Modes
2. Control Unit Design, I/O Interface
3. Instruction Pipelining
4. Memory Organization
Machine Instructions and Addressing Modes
1. A machine has a 32-bit architecture, with 1-word long instructions. It has 64 registers, each of which
is 32 bit long. It needs to support 45 instructions, which have an immediate operand in addition to two
register operands. Assuming that the immediate operand is an unsigned integer, the maximum value
of the immediate operand is __________. GATE : 2014
2. Consider a hypothetical processor with an instruction of type LW R1, 20 (R2), which during execution
reads a 32-bit word from memory and stores it in a 32-bit register R1. The effective address of the
memory location is obtained by the addition of constant 20 and the contents of register R2. Which of the
following best reflects the addressing mode implemented by this instruction for the operand in memory?
GATE : 2011
(a.) Immediate Addressing
(b.) Register Addressing
(c.) Register indirect scaled addressing
(d.) Base indexed Addressing
3. A computer handles several interrupt sources of which ,the following are relevant for this question.
GATE : 2011
Interrupt from CPU temperature sensor ( raises interrupt if CPU temperature is too high).
Interrupt from mouse ( raises interrupt if the mouse is moved or a button is pressed).
Interrupt from keyboard ( raises interrupt when a key is pressed or released).
Interrupt from hard disk ( raises interrupt when a disk read is completed).
Which one of these will be handled at HIGHEST priority?
GATE- 2016-17 CS GATE Solutions 9
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(a.) Interrupt from Hard Disk (b.) Interrupt from mouse
(c.) Interrupt from Keyboard (d.) Interrupt from CPU temperature sensor
4. Consider evaluating the following expression tree on a machine with load-store architecture in which
memory can be accessed only through load and store instructions. The variables a,b,c,d, and e are
initially stored in memory. The binary operators used in the expression tree can be evaluated by the
machine only when the operands are in registers. The instructions produce result only in a Register. If
no intermediate results can be stored in memory, what is the minimum number of registers needed to
evaluate this expression? GATE: 2011
(a.) 2 (b.) 9 (c.) 5 (d.)3
5. A 5- stage pipelined processor has instruction fetch (IF), instruction decode (ID), operand fetch (OF),
perform operation (PO) and write operand (WO) stages. The IF, ID, OF and WO stages take 1 clock
cycle each for any instruction. The PO stage takes 1 clock cycle for ADD and SUB instructions, 3
clock cycles for MUL instruction, and 6 clock cycles for DIV instruction respectively. Operand
forwarding is used in the pipeline. What is the number of clock cycles needed to execute the following
sequence of instructions? GATE : 2010
Instruction Meaning of instruction
I0 : MUL R2, R0, R1: R2R0* R1
I1 : DIV R5, R3, R4 R5 R3/ R4
I2 : ADD R2, R5, R2 R2R5+ R2
I3 : SUB R5, R2, R6 R5R2 – R6
(A.) 13 (B.) 15 (C.) 17 (D.) 19
6. The program below uses six temporary variable a, b, c, d, e, f
a= 1
b= 10
c= 20
d= a+ b
e= c+ d
f= c+ e
b= c+ e
e= b+ f
d= 5+ e
Return d+ f
Assuming that all operation take their operands from registers, what is the minimum number of
registers needed to execute this program without spilling? GATE : 2010
GATE- 2016-17 CS GATE Solutions 10
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(A.) 2 (B.) 3 (C.) 4 (D.) 6
7. A CPU generally handles an interrupt by executing an interrupt service routine GATE : 2009
(a) As soon as an interrupt is raised
(b) By checking the interrupt register at the end of fetch cycle.
(c) By checking the interrupt register after finishing the execution of the current instruction
(d) By checking the interrupt register at fixed time intervals.
8. Which of the following is/are true of the auto-increment addressing mode?
I. It is useful in creating self-relocating code
II. If it is included in an Instruction Set Architecture, then an additional ALU is required for
effective address calculation
III. The amount of increment depends on the size of the data item accessed GATE : 2008
(a.) I only (b.) II only (c.) III only (d.) II and III only
9. Which of the following must be true for the RFE (Return from Exception) instruction on a general
purpose processor? GATE : 2008
I. It must be a trap instruction
II. It must be a privileged instruction
III. An exception cannot be allowed to occur during execution of an RFE instruction
(a.) I only (b.) II only (c.) I and II only (d.) I, II and III only
10. The use of multiple register windows with overlap causes a reduction in the number of memory
accesses for GATE : 2008
I. Function locals and parameters
II. Register saves and restores
III. Instruction fetches
(a.) I only (b.) II only (c.) III only (d.) I, II and III
Answer Key : Computer Organization1 2 3 4 5 6 7 8 9 10
16383 d d d b b c c d b
Computer Organization Solutions
1. ANS: 16383
number of Registers: 64
number of instructions supported by machine: 45
Instruction size: 32 bit
GATE- 2016-17 CS GATE Solutions 11
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
x = 32 – 18 = 14bits
Immediate operand is the operand which is available in the instruction itself.
number of bits for immediate operand = 14 bits
Maximum value of immediate operand = 214 – 1 = 16383
2. ANS: d
1 2, 20( )LW R R
Immediate Addressing:
In this mode, operand is specified in the instruction itself.
Register Addressing:
In this mode, operands are in registers that reside within CPU.
In the above instruction:
EA of memory: 20 + [R2]
1 220 [ ]R M R
In indexed AM, content of index register is added to address part of the instruction, to obtain the
effective address.
3. ANS: d
Interrupt from CPU temperature sensor will be handled at highest priority.
4. ANS: d
1 1
2 2
1 2 1 1 2
2 2
3 3
2 3 2 2 3
3 3
3 2 3 3 2
1 3 1 1 3
//
//
//
//
//
//
//
//
//
LOAD R a R a
LOAD R b R b
SUB R R R R R
LOAD R c R c
LOAD R d R d
ADD R R R R R
LOAD R e R e
SUB R R R R R
ADD R R R R R
Minimum 3 registers are needed to evaluate the expression.
5. ANS: b
GATE- 2016-17 CS GATE Solutions 12
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
EXP:
Number of STAGES: 5
Instruction fetch (IF)
Instruction decode (ID)
Operand Fetch (OF)
Perform operation (PO)
Write operand (WO)
Operand Forwarding:
This technique uses special H/w to detect a conflict and then avoid it by routing the data through
special paths between pipeline segments.
Speed Up Obtained: 27/15 = 1.8
While the maximum speed up which can be obtained is 5 (number of segments). Due to the presence
of stall cycles, pipeline performance degrades.
6. ANS: b
EXP:
IF ID OF PO WO
Io
I1
I2
I3
1 1 1 3 1
1 1 1 6 1
1 1 1 1 1
1 1 1 1 1
GATE- 2016-17 CS GATE Solutions 13
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
1
2
3
1 1 2
2 3 1
1 3 2
3 3 2
2 3 1
2 2
1 2
1
10
20
5 5
R a a
R b b
R c c
R R R d a b
R R R e c d
R R R f c e
R R R b c e
R R R e b f
R R d e
return R R return d f
Minimum 3 registers are required to execute this program without spilling.
7. ANS: c
EXP:
CPU generally handles an interrupt by executing an interrupt service routine (ISR) by checking the
interrupt register after finishing the execution of the current instruction.
8. ANS: c
EXP:
Auto Increment Addressing Mode:
This mode is similar to register indirect mode except that register is incremented (after or before) its
value is used to access memory.
mov (R2) + , - (R3)
Auto decrement mode automatically decrement the register R3, before using its content as the operand
address.
Auto increment addressing mode automatically increments the register R2 after using its content as
the operand address.
Thus the instruction copies the content of location pointed by R3 into the location pointed by register
R2.
Self – Relocating Code:
GATE- 2016-17 CS GATE Solutions 14
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Self relocating program is a program that relocates its instructions and data during execution auto –increment addressing mode do not supports self relocating code.
If this addressing mode is included in instruction set architecture than there is no requirement of an
additional ALU.
The amount of increment depends on the size of data item accessed is true.
Only statement III is correct.
9. ANS: d
Return from Exception:
It is a privileged instruction which is going to be used at the end of exception handler (interrupt
handler or trap handler).
Exception handler is a piece of OS code. This instruction causes the saved processor state to get
restored into the processor then it causes control to transferred to appropriate PC value.
It is a Trap instruction and an exception cannot be allowed to occur during execution of an RFE
instruction.
Note:
Exceptions are exceptional events that occur during the program execution and automatically handled
by processor hardware.
There are 2 different classes of exceptions:
1. Traps
2. Interrupts
Traps are s/w generated exception while interrupt is H/w generated exception.
10. ANS: b
EXP:
Overlapped register windows provide the passing of parameters and avoid need for saving and
restoring register values.
Each procedure call results in the allocation of a new window consisting of set of registers from the
register file
Each procedure call activates a new register window by incrementing current window pointer (CWP),
while return statement decrements the pointer and causes the activation of the previous window.
Windows for addressing procedures have overlapping registers that are shared to provide the passing
of parameters and results.
Common overlapped registers permit parameters to be passed without actual movement of data.
Only one register window is activated at any given time.
GATE- 2016-17 CS GATE Solutions 15
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
3. C Programming
1. Consider the following program in C language:
# include <stdio.h>
main ( )
{
int i;
int *pi = &i;
scanf (“%d”, pi);
printf (“%d\n”, i+5);}
Which one of the following statements is TRUE? GATE-2014(a.) Compilation fails.
(b.) Execution results in a run – time error.
(c.) On execution, the value printed is 5 more than the address of variable i.
(d.) On execution, the value printed is 5 more than the integer value entered.
2. Consider the following C function in which size is the number of elements in the array E:
int MyX (int *E, unsigned int size){int Y = 0;int Z;int i, j, k;
for (i = 0; i < size; i++)Y = Y + E [i];
For (i = 0; i < size; i++)for (j = i; j < size; j++){
Z = 0;for (k = i; k < = j; k++)
Z = Z + E[k];if (Z > Y)Y = Z;
}return Y;
}The value returned by the function MyX is the GATE-2014(a.) Maximum possible sum of elements in any sub-array of array E.
(b.) Maximum element in any sub – array of array E.
(c.) Sum of the maximum elements in all possible sub-arrays of array E.
(d.) The sum of all the elements in the array E.
3. Consider the function func show below:
GATE- 2016-17 CS GATE Solutions 16
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
The value returned by func (435) is ___________. GATE-2014
4. Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which
one of the following statements is most likely to set p correctly?
(a.) p = n * ( n - 1 ) * ( n - 2 ) / 6 ;
(b.) p = n * ( n - 1 ) / 2 * ( n - 2 ) / 3 ;
(c.) p = n * ( n - 1 ) / 3 * ( n - 2 ) / 2 ;
(d.) p = n * ( n - 1 ) * ( n - 2 ) / 6 . 0 ; GATE-2014
5. Consider the following function GATE-2014
double f(double x){if( abs(x*x - 3) < 0.01) return x;else return f(x/2 + 1.5/x);}Give a value q (to 2 decimals) such that f(q) will return q: __________
6. Consider the C function given below.
int f (int j){
Static int i = 50;int k;if (i = = j)
{printf (“something”);k = f(i);return 0;
}else return 0;
}Which one of the following is TRUE? GATE-2014
(a.) The function returns 0 for all values of j.
(b.) The function prints the string something for all values of j.
(c.) The function returns 0 when j = 50.
(d.) The function will exhaust the runtime stack or run into an infinite loop when j = 50.
7. Let A be a square matrix of size n n. Consider the following pseudo-code. What is the expected
output? GATE-2014
GATE- 2016-17 CS GATE Solutions 17
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(a.) The matrix A itself
(b.) Transpose of the matrix A
(c.) Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements of
A
(d.) None of the above
8. Consider the C function given below. Assume that the array listA contains n (> 0) elements,
sorted in ascending order. GATE-2014
int ProcessArray (int * listA, int x, int n)
{
int i,j, k;
i = 0;
j = n-1;
do {
k = (i + j)/2;
if (x < = listA [k])
j = k – 1;
if (listA [k’ < = x)i = k + 1;
} while (i < = j);
if (list A[k] = = x)
return (k);
else
return -1;
}
Which one of the following statements about the function Process Array is Correct?
(a.) It will run into an infinite loop when x is not in list A.
(b.) It is an implementation of binary search.
(c.) It will always find the maximum element in list A.
(d.) It will return –1 even when x is present in list A.
GATE- 2016-17 CS GATE Solutions 17
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(a.) The matrix A itself
(b.) Transpose of the matrix A
(c.) Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements of
A
(d.) None of the above
8. Consider the C function given below. Assume that the array listA contains n (> 0) elements,
sorted in ascending order. GATE-2014
int ProcessArray (int * listA, int x, int n)
{
int i,j, k;
i = 0;
j = n-1;
do {
k = (i + j)/2;
if (x < = listA [k])
j = k – 1;
if (listA [k’ < = x)i = k + 1;
} while (i < = j);
if (list A[k] = = x)
return (k);
else
return -1;
}
Which one of the following statements about the function Process Array is Correct?
(a.) It will run into an infinite loop when x is not in list A.
(b.) It is an implementation of binary search.
(c.) It will always find the maximum element in list A.
(d.) It will return –1 even when x is present in list A.
GATE- 2016-17 CS GATE Solutions 17
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(a.) The matrix A itself
(b.) Transpose of the matrix A
(c.) Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements of
A
(d.) None of the above
8. Consider the C function given below. Assume that the array listA contains n (> 0) elements,
sorted in ascending order. GATE-2014
int ProcessArray (int * listA, int x, int n)
{
int i,j, k;
i = 0;
j = n-1;
do {
k = (i + j)/2;
if (x < = listA [k])
j = k – 1;
if (listA [k’ < = x)i = k + 1;
} while (i < = j);
if (list A[k] = = x)
return (k);
else
return -1;
}
Which one of the following statements about the function Process Array is Correct?
(a.) It will run into an infinite loop when x is not in list A.
(b.) It is an implementation of binary search.
(c.) It will always find the maximum element in list A.
(d.) It will return –1 even when x is present in list A.
GATE- 2016-17 CS GATE Solutions 18
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
9. Consider the following pseudo code. What is the total number of multiplication to be performed?
D = 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3
(a.) Half of the product of the 3 consecutive integers.
(b.) One third of the product of the 3 consecutive integers.
(c.) One – sixth of the product of the 3 consecutive integers.
(d.) None of above. GATE-2014
10. What is the return value of f (p, p) if the value of p is initialized to 5 before the call?
Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
GATE-2013
int f (int &x, int c) {
c = c – 1;
if (c = = 0) return 1:
x = x + 1;
return f (x, c) * x;
}
(a.) 3024 (b.) 6561 (c.) 55440 (d.) 161051
11. What will be the output of the following C program segment? GATE-2012
char inChar = ‘A’
switch (inChar){
case ‘A’: printf(“Choice A\n”);
case ‘B’:
case ‘C’: printf(“Choice B”);
case ‘D’:
case ‘E’:
default: printf(“No Choice”) ;}
(a) No choice
(b) Choice A
(c) Choice A Choice B No choice
(d) Program gives no output as it is erroneous
12. Consider the program given below, in a block-structured pseudo-language with lexical scoping and
nesting of procedures permitted. GATE-2012
GATE- 2016-17 CS GATE Solutions 19
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Program main;
Var ...
Procedure A1;
Var ...
Call A2;
End A1
Procedure A2;
Var ...
Procedure A21;
Var ...
Call A1;
End A21
Call A21;
End A2
Call A1;
End main.
Consider the calling chain: MainA1A2A21 A1The correct set of activation records along with their access links is given by
(a) (b)
(c) (d)
GATE- 2016-17 CS GATE Solutions 20
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Programming Solutions1 2 3 4 5 6 7 8 9 10d a 9 b 1.73 d a b c b11 12 13 14 15 16 17 18 19 20c d
Programming Solutions
1. (d)
EXP: Execution of program starts from main ( )
1000 1001
2000 2001
1000
i
pi
scan f ( ) function takes a memory address (&) as argument, where the run time entered value will be
stored. In this program, address of variable i is passed as argument, thus the value entered at run time
will be stored in variable i. Thus on execution, value printed is 5 more than the integer value entered.
2. (a)
EXP: Value returned by function MyX is the maximum possible sum of elements in any subarray of
array E.
Consider on array E to understand the behaviour of the program:
First “for” loop adds all the array elements and store the result in variable Y
Remaining code first takes 0 as starting index of sub – array and check for all the combinations i.e
E[0], E[0] + E[1], E[0] + E[1] + E[2], E[0] + E[1] + E[2] + E[3] (sum of all the possible sub-arrays
with staring sub-array index as (0)
Also for every such combination, sum of sub-array is compared with Y and if sum of sub-array is
more than Y, Y will be updated accordingly.
Similarly in the next iteration, all the possible combinations of sub-arrays with starting index 1 will be
considered i.e. E[1], E[1] + E[2], E[1] + E[2] + E[3] and there sums are compared with current value
of Y. If sum of any sub-array is more than Y, Y will be updated.
GATE- 2016-17 CS GATE Solutions 21
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Thus all the possible combinations of sub-arrays will be considered and at the end Y will be having
maximum possible sum of elements in any sub-array of array E. For the above taken array E,
4 1 3 is the sub-array whose sum of elements is maximum i.e. 6
3. (9)
EXP:
int func (int num)
{
int count = 0;
While (num) /* If num is non – zero, It is evaluated as true */
{
count ++;
num >> = 1; / *Right shift bits of num by 1 */
}
return (count);
}
Initially num = 11011 0011, count = 0
Count =1 num = 011011001
Count =2 num = 001101100
Count =3 num = 000110110
Count = 4 num = 000011011
. .
. .
. .
Count = 9 num = 000000000
After nine right shift operations, num = 0 and while loop terminates , count value which is 9 will be
returned.
4. (b)
EXP:3
( )( 1)( 2)
6Cn n n
p n
Multiplying n, (n – 1), (n – 2) at once might result in overflow, if the multiplicated values goes
beyond the range of unsigned integer. Thus option (a), (d) can be eliminated.
Whether n is Even or odd, (n) (n – 1)/2 will always result an integer, thus no possibility of
truncation, so more accuracy. Where as in case of n*(n – 1)/3 , value resulted might not be integer,
leads to possibility of truncation, so less accuracy. So option (b) is correct.
GATE- 2016-17 CS GATE Solutions 22
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
5. 1.73
EXP: Function will return q i.e., the value which is passed as the parameter if
(abs (x2 – 3) < 0.01) i.e. x2 must be close to 3, x must be close to square root of 3.
2(1.73) 3 2.9929 3.0000 0.0071 3 1.73
When 1.73 is passed as parameter, 1.73 will be returned as the output.
6. (d)
EXP:
Value of variable i is 50. Function will return 0 for any value of j other than 50.
Call itself recursively till runtime stack exhausts or run into an infinite loop.
For j = 50, condition (i = = j) will be true, function will print the string “something” and call itselfrecursively till run time stack exhaust or run into an infinite loop.
7. (a)
EXP: Inner statements of first for loop swaps A[i] [j] with A[j] [i] for all values of i and j.
Since the loop runs for all values of i and j, every matrix element A [i] [j] is swapped with A[j] [i]
twice, resulting in no change in the matrix A.
Second for loop prints all the elements of the matrix.
8. (b)
EXP: It is clear that it is implementation of Binary search.
For binary search, the array must be sorted. In each step, algorithm compares the search key value
with the key value of middle element of array if the keys match, then the matching element has been
found and its index is returned.
If the search key is less than middle element’s key, then the algorithm repeats its action on thesub-array to the left of the middle element or if the search key is greater, then on the sub-array to the
right.
So the above C program is an implementation of binary search which returns the position of search
key value in the array, and if element is not in the array, returns – 1.
9. (c)
EXP: Total # of multiplications performed will be
( 1)( )( 1)
6
n n n
For i = 1, multiplication statements is executed (n – 1) + (n – 2) + (n – 3) + ..... + 2 + 1 times. For i =
2, multiplication statement is executed (n -2 ) + (n – 3) +.... + 2 + 1 times.
GATE- 2016-17 CS GATE Solutions 23
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
: : : : : :
For i (n – 1), statement is executed once.
For i = n, statement is not executed at all.
Total # of times, statement D = D * 3 is executed is
S= [1+2+3.... (n -1)] + [1+2+3 +....(n-2)] + [1+2+3+...(n-3)] + ..... +1+0
2 2 2 2
( 1)( ) ( 2)( 1).............. 1
2 22 ( 1) ( 2)( 1) ............. 2
2 ( 1) (1 ) ( 2) (2 ) ........ 2 ( 2 )
n n n nS
S n n n n
S n n n n n n n n
2 2 2 22 [ ( 1) ( 2) ........2 ] [ ( 1) ( 2) ...... 2]S n n n n n n
Adding and subtracting 1
2 2 2 2 22 [ ( 1) ( 2) ..........2 1 ] [ ( 1) ( 2) ....... 2 1]
( )( 1)(2 1) ( )( 1)2
6 2( )( 1)(2 1) ( )( 1)
12 4( )( 1)(2 1) 3( )( 1)
12( )( 1)[2 1 3]
12( )( 1)(2 2)
12( 1) *( ) * (
S n n n n n n
n n n n nS
n n n n nS
n n n n nS
n n nS
n n nS
n nS
1)
6
n
10. (b)
In function f (int & n, int c), parameter ‘n’ is passed by reference and parameter ‘C’ passed by value.By definition parameter “pass by value”, passes only the copy of actual parameter, but parameter“pass by reference”, passes the memory address of actual parameter.Any operation on parameter, passed by value only changes the value of parameter in the local copy
but on parameter passed by reference changes the value of actual parameter
So every function call will have different copy of C but some copy (address) of n.
f(n, 5) = f (n, 4) *n = f(n, 3) * n * n
= f (n, 2) *n *n *n
= f(n, 1) * n * n * n = 1 * n * n * n * n
Every function call increments the value of n by 1,
The value of n after f (n, 2) = 9,since initially x = 5
but passed by reference
So the output = n * n * n * n = 9 * 9 * 9 * 9 * 9 = 6561
GATE- 2016-17 CS GATE Solutions 24
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Note: There is 1 mistake in the question , 1st parameter of function f () should be int * x.
11. (c)
EXP: Switch causes the control to branch to one of a list of possible statements.
A match is found with case ‘A’, execution continuous after the matching case statement and continuousuntil a break statement is encountered.
Since there is no break statement in the code, every statement will be executed.
Output: choice A
Choice B no choice
Note: In the printf statement of case A, there is new line operator which causes the remaining output
to come in the next line.
12. (d)
EXP:
C-programming
Every function is defined inside main. Thus every function can access variables of main. Function A21
is defined inside A2. Thus A21 can access variables of A2 beside main. So there will be direct access
link from A1 and A2 to main, but for A21 direct access link to A2 and to main through A2.
For chain: MainA1A2A21A1, the activation record and access links is as
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::;
4. DATA STRUCTURES1. Arrays
2. Stacks and Queues
3. Linked List
4. Trees
5. Graphs
6. Hashing
Arrays
1. A program P reads in 500 integers in the range [0, 100] representing the scores of 500 students. It then
prints the frequency of each score above 50. What would be the best way for P to store the
frequencies? GATE-2005(a) An array of 50 numbers (b) An array of 100 numbers
GATE- 2016-17 CS GATE Solutions 25
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(c) An array of 500 numbers (d) A dynamically allocated array of 550 numbers
2. A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow from opposite
ends of the array. Variables top1 and top2 (top1 < top2) point to the location of the topmost element in
each of the stacks. If the space is to be used efficiently, the condition for “stack full” isGATE-2004
(a) (top 1 = MAXSIZE/2) AND (top 2 = MAXSIZE/2 + 1)
(b) top 1 + top 2 = MAXSIZE
(c) (top 1 = MAXSIZE/2) or (top 2 = MAXSIZE)
(d) top 1 = top 2 – 1
3. Two matrices 1 2M and M are to be stored in arrays A and B respectively. Each array can be stored
either in row-major or column-major order in contiguous memory locations. The time complexity ofan algorithm to compute 1 2M M will be GATE-2004
(a) Best if A is in row-major, and B is in column-major order
(b) Best if both are in row-major order
(c) Best if both are in column-major order
(d) Independent of the storage scheme
Stacks and Queues4. Suppose a stack implementation supports an instruction REVERSE, which reverses the order of
elements on the stack, in addition to the PUSH and POP instructions. Which one of the followingstatements is TRUE with respect to this modified stack?
(a) A queue cannot be implemented using this stack.
(b) A queue can be implemented where ENQUEUE takes a single instruction and DEQUEUE takes a
sequence of two instructions.
(c) A queue can be implemented where ENQUEUE takes a sequence of three instructions and
DEQUEUE takes a single instruction.
(d) A queue can be implemented where both ENQUEUE and DEQUEUE take a single instruction
each. GATE-2014
5. Consider the following operation along with Enqueue and Dequeue operations on queues, where k is
a global parameter GATE-2013MultiDequeue (Q) {
m = k
while (Q is not empty) and (m > 0) {
Dequeue (Q)
m = m – 1
}
}
What is the worst case time complexity of a sequence of n queue operations on an initially empty
queue?
GATE- 2016-17 CS GATE Solutions 26
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(a) (n) (b) (n + k) (c) (nk) (d) (n2)
6. Suppose a circular queue of capacity (n – 1) elements is implemented with an array of n elements.
Assume that the insertion and deletion operations are carried out using REAR and FRONT as array
index variables, respectively, Initially, REAR = FRONT = 0. The conditions to detect queue full and
queue empty are ` GATE-2012(a) full: (REAR) + 1 mod n = = FRONT (b) full: (REAR + 1) mod n = = FRONT
empty : REAR = = FRONT empty: (FRONT + 1) mod n = = REAR
(c) full REAR = = FRONT (d) full: (FRONT + 1) mod n = = REAR
empty: (REAR + 1) mod n = = FRONT empty: REAR = = FRONT
7. The following postfix expression with single digit operands is evaluated using a stack:
8 2 3 ^ / 2 3 * + 5 1 * –Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is
evaluated are: GATE-2007(a) 6, 1 (b) 5, 7 (c) 3, 2 (d) 1, 5
8. Suppose you are given an implementation of a queue of integers. The operations that can be
performed on the queue are: GATE-IT : 2007(i) isEmpty (Q) – returns true if the queue is empty, false otherwise
(ii) delete (Q) – deletes the element at the front of the queue and returns its value.
(iii) insert (Q, i) – inserts the integer i at the rear of the queue.
Consider the following function:
void f (queue Q)
{
int i ;
if (! isEmpty (Q))
{
i = delete (Q);
f(Q);
insert (Q, i);
}
}
What operation is performed by the above function f?
(a) Leave the queue Q unchanged
(b) Reverse the order of the elements in the queue Q
(c) Deletes the element at the front of the queue Q and insert it at the rear keeping the other elements
in the same order
(d) Empties the queue Q
GATE- 2016-17 CS GATE Solutions 27
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Arrays Answer Key
1 2 3 4 5 6 7 8 9 10
a d d c c a a b b a
Arrays Solutions1. (a)
An array of size 50 looks the best option to store number of students for each score. We need to store
frequencies of scores above 50. We can ignore scores below 50 and to index the scores above 50.
2. (d)A single array
As both stack grow from the opposite end of the array and grow in the opposite direction so stack will
be full only if both Top1 and Top2 are adjacent to each other. Only option (d) satisfies the condition.
3. (d) Since matrices are stored in array, complexity is independent of how they are stored. If the
starting address of array is known, with indexes of elements their address can be calculated.
Stacks and Queues
4. (c)We can implement queue with the help of two stacks but if we have one stack with REVERSEoperation then we can implement the queue operation.For de-queue operation
(i) Pop the top of the stack
For Enqueue operation
(i) REVERSE (ii) PUSH (iii) REVERSE
So de-queue can be implemented by 1-stack operation and enqueue can be implemented by 3-stack
operation option (c) is correct.
5. In the stack and queue every operation takes 0(1) time. Here there are total n operations.
Let a be the en-queue operations
b be the de-queue operations
c be the multi-queue operation
GATE- 2016-17 CS GATE Solutions 28
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
a + b + c = n
All en-queue, de-queue, and multi-queue operations takes constant time so total time complexity is
0(n).
6. (a)full: (REAR+1) mod n == FRONT
empty: REAR == FRONT
The question says initially FRONT = REAR = 0. So it's given that empty queue condition is FRONT ==
REAR.
We have options (A) and (D) to choose from.
Let's see an e.g.: Let array size n = 4. The queue capacity is given as n-1, so capacity is 3.
FRONT=REAR=0 (initially)
Insertion : q[REAR] = 'a'; REAR=REAR+1 %N;
Insert a: q[0] = 'a'; REAR=1;
Insert b: q[1] = 'b'; REAR=2;
Insert c: q[2] = 'c'; REAR=3;
Now if we insert one more element, REAR will become 0 (3+1%mod 4), which will make it equal to
FRONT. We need to distinguish between EMPTY and FULL queue conditions. Plus it is given that
queue capacity is (n-1)! so we leave a space between REAR and FRONT when the queue is FULL. So
the condition is (REAR+1)mod n == FRONT.
Option (A).
7. (a)In postfix evaluation operand stack is used
8. (b)In this, we keep on executing the code segment delete (Q) until the queue get empty and after that we
starts insertions.
Consider the following queue
GATE- 2016-17 CS GATE Solutions 29
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
F R
a b c d d
Deletion sequence of the element a, b, c, d, e
When queue get empty we will starts insertion in the queue (Q)
Insertion sequence is e, d, c, b, a
Content of queue is
F R
e d c b a
Option (b) is correct
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::;
5. ALGORITHMS (DAA)
1. Algorithm Analysis and Asymptotic Notations
2. Divide and Conquer
3. Greedy Method
4. Dynamic Programming
5. P and NP Concepts
6. OBST
7. Miscellaneous Topics
Algorithm Analysis and Asymptotic Notations
1. Consider a rooted n node binary tree represented using pointers. The best upper bound on the time
required to determine the number of sub-trees having exactly 4 nodes is O (na logb n). Then the value
of a + 10b is __________. GATE-2014 SET-1
2. Consider the following pseudo code. What is the total number of multiplication to be performed?D = 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3
(e.) Half of the product of the 3 consecutive integers.
(f.) One third of the product of the 3 consecutive integers.
(g.) One – sixth of the product of the 3 consecutive integers.
(h.) None of above. GATE-2014 SET-1
3. Which one of the following correctly determines the solution of the recurrence relation with T(1) =
1?
GATE- 2016-17 CS GATE Solutions 30
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
( ) 2 log2
nT n T n
GATE – 2014 SET-2(a.) (n) (b.) (n log n) (c.) (n2) (d.) (log n)
4. Suppose we have a balanced binary search tree T holding n numbers. We are given two numbers L
and H and wish to sum up all the numbers in T that lie between L and H . Suppose there are m
such numbers in T . If the tightest upper bound on the time to compute the sum is 0(na logfc n + mc
logd n), the value of a+ 10b+ 100c + 1000d is __________. GATE – 2014 SET-3
5. The worst case running time to search for an element in a balanced binary search tree with 2nn
elements is GATE - 2012
(a) ( log )n n (b) ( 2 )nn (c) ( )n (d) (log )n
6. Let W(n) and A(n) denote respectively, the worst case and average case running time of an
algorithm executed on an input of size n. Which of the following is ALWAYS TRUE?
GATE - 2012(a) A( ) (W( ))n n (b) A( ) (W( ))n n (c) A(n) = O(W(n)) (d) A(n) = 0(W(n))
7. Which of the given options provides the increasing order of asymptotic Complexity of functions
1 2 3 4, , and ?f f f f GATE - 2011
1( ) 2nf n 3
22 ( )f n n 3 2( ) logf n n n 2log
4 ( ) nf n n
(a) 3 2 4 1, , ,f f f f (b) 3 2 1 4, , ,f f f f (c) 2 3 1 4, , ,f f f f (d) 2 3 4 1, , ,f f f f
8. Two alternative packages A and B are available for processing a database having 10k records.
Package A requires 20.0001n time units and package B requires 1010 . logn n time units to
process n process n records. What is the smallest value of k for which package B will be preferred
over A? GATE - 2010(a) 12 (b) 10 (c) 6 (d) 5
9. Consider the following functions:
( ) 2nf n
g(n) = n!
log( ) nh n n
Which of the following statements about the asymptotic behaviour of f(n), g(n) and h(n) is true?
GATE - 2008(a) f (n) = O(g(n)); g(n) = O(h(n)) (b) ( ) ( ( )); ( ) O( ( ))f n g n f n h n (c) g (n) = O(f (n)); h(n) = O(f (n)) (d) ( ) O( ( )); ( ) ( ( ))h n f n g n f n
GATE- 2016-17 CS GATE Solutions 31
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
10 The minimum number of comparisons required to determine if an integer appears more than n/2
times in a sorted array of n integers is GATE - 2008(a) ( )n (b) (log )n (c) (log * )n (d) (1)
11. You are given the postorder traversal, P, of a binary search tree on the n element, 2, ….., n. You
have to determine the unique binary search tree that has P as its post order traversal. What is the time
complexity of the most efficient algorithm for doing this? GATE-2008(a) (log )n (b) ( )n (c) ( log )n n
(d) None of the above, as the tree cannot be uniquely determined
Algorithm ALGORITHMS (DAA) SOLUTIONDAA)
Answer Key
1 2 3 4 5 6 7 8 9 10
1 c a 110 c c a c d a
11 12 13 14 15 16 17 18 19 20
c
SOLUTION
Algorithm Analysis and Asymptotic Notations
1. Gate – 2014 set 1
(a + 10b) = 1
As give in the question rooted binary tree is represented using pointers.
To find the sub-tree of “4” nodes in a given tree we have to traverse in bottom – up fashion, and find the
size of sub-tree rooted with current node. If the size is 4, print the current root node.
Let us see the pseudo code to know the complexity.
int node 4 subtreef (struct node * root
{
if root = = NULL
return 0;
int l = node 4 subtreef (root left);
int r = node 4 subtree (root right);
if (l + r + 1) = = 4
printf ("subtree of size 4");
return (l
+ r + 1);
}
For n node of tree, the complexity to find the sub-tree of node 4 is O(n), because above function will be
called for every node of the tree from leaf to root.
GATE- 2016-17 CS GATE Solutions 32
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
In the question the complexity is O (na logb n)
So O (na logb n) O (n).
So a = 1, b = 0
Thus a + 10b = 1 + 10 0 = 1
2. (c) Gate – 2014 set 1
D = 2
For i = 1 to n do
For J = i to n do
For k = J + 1 to n do
= D * 3
for i = 1; J = 1, k = 2 to n, multiplication (n - 1) times
i = 1; J = 2; k = 3 to n multiplication (n - 2) times
i = 1; J = 3; k = 4 to n multiplication (n - 3) times
i = 1; J = n - 2; k = n - 1 to n mul
( 1)
tiplicatin 2 times
i = 1; J = n - 1; k = n to n multiplication 1 time
n times
Similarity i = 2, the multiplication executed (n – 2) + (n – 3) + - 2 + 1times = (n – 2)
For i = 3, the multiplication executed
(n – 3) + (n – 4) + ..... 2 + 1 times = (n – 3)
For i = n – 1 multiplication executed once = 1
For i = n, the statement is not executed
Thus the overall execution times
[ ( 1) ( 2) ( 3) ..... 2 1]n n n
1
2
( 1) ( 1)( 2) ( 1)...... 1 0
2 2 2
( 1)
21 1
2 21 ( 1)(2 1) ( 1)
2 6 2
( 1)( 1)
6
n
i
n n n n n ns
n n
n n
n n n n n
n n n
3. (a) Gate – 2014 set 2
T(n) = 2T (n/2) + Log n
Using master method
T(n) = aT (n/b) + f(n)
Here a = 2, b = 2, f(n) = log nNow 2log log 2n b na n
Here
2
n 2
( ) ( log ) i.e. log ( log 2 )
Thus T(n) = ( log 2) ( )n b nf n O a n O
n
GATE- 2016-17 CS GATE Solutions 33
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
4. The given tree is balanced binary search tree holding n numbers. Gate – 2014 set 3
Here we want to search the number which is greater than equal to L (suppose k1), which can be done in
O (log n) time.
Again we want to search the number, which is lesser than or equal to H. (suppose k2).It can also be done
in O(log n) time.
As given there are m numbers between k1 and k2
Computation order to add m numbers = O (m).
Total complexity = O (log n) + O (log n) + O (m)
= O (log n + m) [since constant term is meaningless]
The given complexity = ( log log )a b c dO n n m n
So (log ) ( log log )a b c dO n m O n n m n
So a = 0, b = 1, c = 1, d = 0
Now a + 10b + 100c + 1000d
= 0 + 10 1 + 100 1 + 1000 0 = 10 + 100 = 110
5. (c)
In Balance Binary search tree worst case search time= 2log n
where n is the number of nodes in balanced BST. So for 2nn elements (node) worst case
search time= 2log ( 2 )nn 2 2log log (2 ) log log ( )nn n n n n O n
[NB: in asymptotic notation highest degree term is entertained]
6. (c)Average case runtime A(n) is always lesser or equal to the worst case runtime W(n) i.e ( ) ( )A n W nso A(n) = O (W(n))
7. (a)
Take the log both sides.
1 1( ) 2 log ( )nf f n
3/22 2 2( ) log ( ) 3 / 2 lognf n f
2 2log 1/ 2logn n
3 2 2 3 2
2 2 2
( ) log log ( ) log( log )
log log log
n nf n f n
n n
4 2 2 4 2 2() log log () log .logf n n f n n
Comparing all we get 3 2 4 1f f f f
8. (c)
Total number of records =10k
Required time to package “A” to access n recodes
GATE- 2016-17 CS GATE Solutions 34
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
= 2 4 20.0001n 10 n units
Required time to package “B” to a access n recodes =10 n 10logn units
to access 10k recodes
A required time = 4 2 2 410 (10 ) 10k k units
B required time
1010
1
10.10 .log
10 .
k k
k K units
To prefer B over A1 2 410 . 10k KK
2 4 1
5
10 /10
10
k k
k
K
K
This inequality is true for minimum value K=6 .So smallest value of K=6
9. (d)
log
( ) 2
( ) !
( )
n
n
n
f n
g n n n
h n n
Taking the log we can see
log f(n) =n
log g(n) =log (n!) = n log n
log h(n) = log n log n
Here log n < n< log (n1)
h(n) < f(n) < g(n)
So h(n) = O f(n) ; g(n) = (f(n))
10. (a)
The given array is sorted.
If any element appear more than n/2 times in the array, then same element must be at least at the ith and
(i + n/2)th positions.
The value of ith position differs from 0 to n/2
so for (i=0; i<n/2; i++)
if (arr [i] ==e &arr [i+n/2]==e)
return true
}
“for” loop will execute n/2 times i.e the complexity is of order of O(n)So minimum number of comparison = O(n)
11. (c)
Here the given tree is Binary Search Tree” and the order of traversal is “post order”.So the sorted order would be “inorder”.Sorting of “ postorder” to make “inorder” has the complexity O(n log n).Using “postorder” and “inorder” we can create unique Binary Search Tree in linear scan with order
GATE- 2016-17 CS GATE Solutions 35
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
O(n).
So complexity =O(n) + O(n log n)= O(n log).
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Theory of computation
1. Regular Language and F.A
2. CFL and PDA
3. CSL, REC, RE and TM
4. Decidability and NP – Completeness
Regular Languages and Finite Automata
1. Which one of the following is TRUE? (GATE - 2014)(a.) The language L = {anbn | n 0} is regular.(b.) The language L = {an | n is prime} is regular.
(c.) The language L = {w|w has 3k + 1b’s for some k N with = {a,b}} is regular.
(d.) The language L = {ww | w * with ={0,1}} is regular.
2. Consider the finite automaton in the following figure.
What is the set of reachable states for the input string 0011? (GATE - 2014)(a.) {q0, q1, q2} (b.) {q0, q1} (c.) {q0, q1, q2, q3} (d.) { q3}
3. Which of the regular expressions given below represent the following DFA?(GATE - 2014)
I) 0 * 1( 1 + 00* 1)* II) 0*1*1+11*0*1 III) (0+1)*1
(a.) I and II only (b.) I and III only (c.) II and III only (d.) I, II and III
4. If 1 | 0nL a n and 2 | 0nL b n , consider
(I) L1.L2 is a regular language
(II) L1.L2 | 0n na b n
Which one of the following is CORRECT? (GATE-2014)(a.) Only (I) (b.) Only (II)
(c.) Both (I) and (II) (d.) Neither (I) nor (II)
GATE- 2016-17 CS GATE Solutions 36
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
5. Let L1 = { w {0,1}* | w has at least as many occurrences of (110)'s as (011)'s}. Let L2 = {w {0,1}*
| w has at least as many occurrences of (000)'s as (111)'s}. Which one of the following is TRUE?
(GATE - 2014)(a.) L1 is regular but not L2 (b.) L2is regular but not L1
(c.) Both L1and L2are regular (d.) Neither L1nor L2 are regular
6. Consider two strings A = "qpqrr" and B = "pqprqrp". Let x be the length of the longest common
subsequence (not necessarily contiguous) between A and B and let y be the number of such longest
common subsequences between A and B . Then x + 10y =____________. (GATE - 2014)
7. The length of the shortest string NOT in the language (over = {a, b}) of the following regular
expression is ___________. (GATE - 2014)a* b* (ba) *a*
8. Consider the languages 1 2L =Φ and L = a . which one of the following representsL1L2* U L1
*?
(A.) ε (B.) (C.) a* (D.) ε,a (GATE - 2013)
9. Consider the DFA A given below.
Which of the following are FALSE? (GATE - 2013)
1. Complement of L(A) is context – free
2. L(A) = L((11*0+0)(0 +1)*0*1*)
3. For the language accepted by A, A is the minimal DFA
4. A accepts all strings over {0, 1} of length at least 2
(A.) 1 and 3 only (B.) 2 and 4 only (C.) 2 and 3 only (D.) 3 and 4only
10. Given the language L= {ab, aa, baa}, which of the following strings are in L*?
(GATE-2012)
(1.) abaabaaabaa (2.) aaaabaaaa (3.) baaaaabaaaab (4.) baaaaabaa
(a) 1,2 and 3 (b) 2,3 and 4 (c) 1,2 and 4 (d) 1,3 and 4
GATE- 2016-17 CS GATE Solutions 36
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
5. Let L1 = { w {0,1}* | w has at least as many occurrences of (110)'s as (011)'s}. Let L2 = {w {0,1}*
| w has at least as many occurrences of (000)'s as (111)'s}. Which one of the following is TRUE?
(GATE - 2014)(a.) L1 is regular but not L2 (b.) L2is regular but not L1
(c.) Both L1and L2are regular (d.) Neither L1nor L2 are regular
6. Consider two strings A = "qpqrr" and B = "pqprqrp". Let x be the length of the longest common
subsequence (not necessarily contiguous) between A and B and let y be the number of such longest
common subsequences between A and B . Then x + 10y =____________. (GATE - 2014)
7. The length of the shortest string NOT in the language (over = {a, b}) of the following regular
expression is ___________. (GATE - 2014)a* b* (ba) *a*
8. Consider the languages 1 2L =Φ and L = a . which one of the following representsL1L2* U L1
*?
(A.) ε (B.) (C.) a* (D.) ε,a (GATE - 2013)
9. Consider the DFA A given below.
Which of the following are FALSE? (GATE - 2013)
1. Complement of L(A) is context – free
2. L(A) = L((11*0+0)(0 +1)*0*1*)
3. For the language accepted by A, A is the minimal DFA
4. A accepts all strings over {0, 1} of length at least 2
(A.) 1 and 3 only (B.) 2 and 4 only (C.) 2 and 3 only (D.) 3 and 4only
10. Given the language L= {ab, aa, baa}, which of the following strings are in L*?
(GATE-2012)
(1.) abaabaaabaa (2.) aaaabaaaa (3.) baaaaabaaaab (4.) baaaaabaa
(a) 1,2 and 3 (b) 2,3 and 4 (c) 1,2 and 4 (d) 1,3 and 4
GATE- 2016-17 CS GATE Solutions 36
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
5. Let L1 = { w {0,1}* | w has at least as many occurrences of (110)'s as (011)'s}. Let L2 = {w {0,1}*
| w has at least as many occurrences of (000)'s as (111)'s}. Which one of the following is TRUE?
(GATE - 2014)(a.) L1 is regular but not L2 (b.) L2is regular but not L1
(c.) Both L1and L2are regular (d.) Neither L1nor L2 are regular
6. Consider two strings A = "qpqrr" and B = "pqprqrp". Let x be the length of the longest common
subsequence (not necessarily contiguous) between A and B and let y be the number of such longest
common subsequences between A and B . Then x + 10y =____________. (GATE - 2014)
7. The length of the shortest string NOT in the language (over = {a, b}) of the following regular
expression is ___________. (GATE - 2014)a* b* (ba) *a*
8. Consider the languages 1 2L =Φ and L = a . which one of the following representsL1L2* U L1
*?
(A.) ε (B.) (C.) a* (D.) ε,a (GATE - 2013)
9. Consider the DFA A given below.
Which of the following are FALSE? (GATE - 2013)
1. Complement of L(A) is context – free
2. L(A) = L((11*0+0)(0 +1)*0*1*)
3. For the language accepted by A, A is the minimal DFA
4. A accepts all strings over {0, 1} of length at least 2
(A.) 1 and 3 only (B.) 2 and 4 only (C.) 2 and 3 only (D.) 3 and 4only
10. Given the language L= {ab, aa, baa}, which of the following strings are in L*?
(GATE-2012)
(1.) abaabaaabaa (2.) aaaabaaaa (3.) baaaaabaaaab (4.) baaaaabaa
(a) 1,2 and 3 (b) 2,3 and 4 (c) 1,2 and 4 (d) 1,3 and 4
GATE- 2016-17 CS GATE Solutions 37
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
ANSWER KEY : TOC
1 2 3 4 5 6 7 8 9 10
c a b a a 34 3 a d c
TOC
1. (c)
(a) | 0n nL a b n is DCFL not regular.
(b) | is primenL a n is CSL not regular.
(c) | has 3k + 1 b's for some k N with ={a, b is regularL w w
Transition diagram for this language is:
(d) *| 0,1L ww w with is CSL not regular.
2. (a)Reachable states are those states which can be reached from starting state by reading the input.
* *
*
*
*
*
*
( ,0011) ( ( ,0),011)
( ,011)
( ( ,0),11)
( ,11)
( ( ,1),1)
( ,1)
o o
o
o
o
o
o o
q q
q
q
q
q
q q
qo is a reachable state.
* *
*
*
*
*
*1
( ,0011) ( ( ,0),011)
( ,011)
( ( ,0),11)
( ,11)
( ( ,1),1)
( ,1)
o o
o
o
o
o
o
q q
q
q
q
q
q q
q1 is a reachable state
GATE- 2016-17 CS GATE Solutions 38
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
* *
*
*
*
*
*2
( ,0011) ( ( ,0),011)
( ,011)
( ( ,0),11)
( ,11)
( ( ,1),1)
( ,1)
o o
o
o
o
o
o
q q
q
q
q
q
q q
q2 is a reachable state
q3 is not a reachable state by reading input 0011.
3. (b)
This DFA accepts all the strings over = {0, 1} which ends with “1”.I. Use the procedure for converting NFA to R.E
0* 1(1 + 00* 1)*
II. 0* 1* 1 + 11* 0*1
This regular expression do not generate the string “10101” which is valid string. Thus thisR.E is not correct.
III. (0 + 1)*1
This R.E will generate all the strings over = {0, 1} which ends with “1”.4. (a)
1
2
2 3 2 31 2
| 0
| 0
. , , , ,...... . , , , ,......
| , 0
n
n
m n
L a n
L b n
L L a a a b b b
a b m n
Note that each string of L1 will be concentrated with every string of L2. L1. L2 is a regular language
with regular expression a* b*
5. (a)
*1 0, 1L w |w has at least as many occurrences of (110)’s as (011)’s}
*2 0, 1L w |w has at least as many occurrences of (000)’s as (111)’s}
L1 is regular language. All the strings of length upto 2 are present in the language.
GATE- 2016-17 CS GATE Solutions 39
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Occurrences of (110)’s in the string occurrences of (011)’s in the stringDFA accepting the language is:
Since DFA for language if possible, language is surely regular.
DFA for L2 cannot be designed, therefore L2 is not regular since 111 occurrence depends on 000
appearance in the string
6. 34
Subsequence is a sequence that can be derived by deleting some elements form the sequence without
changing the order of the remaining elements. Subsequence and substrings are not synonyms.
substrings are consecutive parts of a string, while subsequence need not be. Substring of a string is
always a subsequence of the string, but a subsequence of the string is not always a substring of the
string.
A = “q p q r r”B = “p q p r q r p”x: Length of the longest common subsequence which can be maximum 5 if string A is common
subsequence.
y: Number of such common subsequences between A and B
Common subsequences:
qpqr, pqrr, qprr
x = 4 y = 3
x + 10y = 4 + 30 = 34
7. (3)
RE: a* b* (ba)* a*
{ , }a b
Length Possible strings Strings not in language
0 __
( is in language)
1 a, b __
GATE- 2016-17 CS GATE Solutions 40
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(All are in language)
2 aa, ab, ba, bb __
All are in language
3 aaa, aab, aba, abb, baa, bab, bba, bbb All except “bab” is not in language
Length of “bab” is 3 and this is the shortest string which is not in language
8. (a)
When we concatenate an empty language with another language we will get an empty language. So
* *1 2 1
**1 2
** *1 2 1
. .
L L L
L L a
L L L
*
* *1 2 1
Note L
Thus L L L
9. (d)
1. L(A) is regular, since any language accepted by DFA is always regular.
Regular language all closed under complementation thus complement of L(A) is also regular and
every regular language is context free. Thus 1 is correct statement.
2. The language of this deterministic finite automata is all input strings where at least one 0 is
must, which is same as in the given regular expression.
3. The minimum state deterministic finite automata will be:
In this minimal DFA, we have 2 states.
4. DFA A will accept only 0(string of length 1), so this is false.
10. (c)
* 0 1 2 3 4
**
, ,
......
, ,
L ab aa baa
L L L L L L
L ab aa baa
L* is a set which contain strings which can be broken into ab, aa, baa
1) ab aa baa ab aa
2) aa aa baa aa
3) *( )baaaaabaa aab Not present in L
4) baaaaabaa
1, 2 and 4 must be present in L*
GATE- 2016-17 CS GATE Solutions 41
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
14. DISCRETE MATHEMATICS1. Mathematical Logic
2. Combinatorics
3. Graph Theory
4. Set Theory & Algebra
5. Linear Algebra
1. Mathematical LogicOne Mark Questions
1. Identify the correct translation into logical notation of the following assertion.
Some boys in the class are taller than all the girls
Note: taller (x, y) is true if x is taller than y. (GATE-CS-2004)
(a.) ( ( ) ( )( ( ) ( , )))x boy x y girl y taller x y
(b.) ( ( ) ( )( ( ) ( , )))x boy x y girl y taller x y
(c.) ( ( ) ( )( ( ) ( , )))x boy x y girl y taller x y
(d.) ( ( ) ( )( ( ) ( , )))x boy x y girl y taller x y
2. Let a (x, y) b(x, y) and c(x, y) be three statements with variables x and y chosen from some universe.
Consider the following statement: (GATE-IT-2004)
( ( , ) ( , )) ( , )x y a x y b x y c x y
Which one of the following is its equivalent?
(a.) ( ), ( , ) ( , )) ( , )x y a x y b x y c x y
(b.) ( ), ( , ) ( , )) ( , )x y a x y b x y c x y
(c.) ( , ) ( , ) ( , )x y a x y b x y c x y
(d.) ( , ) ( , ) ( , )x y a x y b x y c x y
3. A set of Boolean connectives is functionally complete if all Boolean function can be synthesized
using those. Which of the following sets of connectives is NOT functionally complete?
(GATE-IT-2008)
(a.) EX – NOR (b.) Implication, negation
(c.) OR, negation (d.) NAND
4. The truth, table (GATE IT – 2012)
GATE- 2016-17 CS GATE Solutions 42
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
X Y f(x, y)
0 0 0
0 1 0
1 0 1
1 1 1
Represents the Boolean function
(a.) X (b.) X + Y (c.) X Y (d.) Y
5. Consider the following logical inferences.
I1: If it rains then the cricket match will not be played
The cricket match was played
Inference: There was no rain
I2: If it rains then the cricket match will not be played
It did not rain
Inference: The cricket match was played.
Which of the following is TRUE? (GATE – IT-2012)
(a.) Both I1 and I2 are correct inferences
(b.) I1 is correct but I2 is not a correct inferences
(c.) I1 is not correct but I2 is a correct inferences
(d.) Both I1 and I2 are not correct inferences
6. What is the correct translation of the following statement into mathematical logic?
“Some real numbers are rotational” (GATE-CS-1990)
(a.) (real (x) rational (x))x (b.) (real (x) rational (x))x
(c.) (real (x) rational (x))x (d.) (rational (x) real (x))x
Two Marks Questions
7. The following resolution rule is used in logic programming.
Derive Clause (P Q) from clauses (P R), (Q R).
Which of the following statements related to this rule is FALSE? (GATE – CS-2003)
(a.) (( ) ( )) ( ) is logically validP R Q R P Q
(b.) ( ) (( )) ( )) is logically validP Q P R Q R
(c.) ( )P Q is satisfiable if and only if ( ) ( )P R Q R is satisfiable
(d.) ( )P Q FALSE if and only if both P and Q are unsatisfiable
8. The following propositional statement is (GATE-CS-2004)
( ( )) (( ) )P Q R P Q R
GATE- 2016-17 CS GATE Solutions 43
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
(a.) Satisfiable but not valid (b.) Valid
(c.) A Contradiction (d.) None of the above
9. Let p, q, r and s be four primitive statements. Consider the following statements:
P: ( ) ( )p q r s p r s q
Q: [ ( )]p q q p r r
R: ( )q r p q p r
S: ( ) ( )p p r q r q
Which of the above arguments are valid? (GATE-IT-2004)
(a.) P and Q only (b.) P and R only (c.) P and S only (d.) P, Q, R and S
10. Let P, Q and R be three atomic prepositional assertions. Let X denote (PQ) R and Y denote (P
R) (Q R). Which one of the following is a tautology? (GATE-CS-2005)
(a.) X Y (b.) X Y (c.) Y X (d.) Y X
Answer Key
1 2 3 4 5 6 7 8 9 10
d c a a b c b a c b
Solution of Discrete
1. Mathematical Logic-Solution1. (d) boy (x): x is a boy in the class
Girl (y): y is a girl
The statement is
There exist some boys in the class which are taller than all the girls
Or
There exist some x, where x is a boy such that x is taller than all y, y is a girl
Or
There exist some x, where x is a boy and if y is a girl then x is taller than all y.( ( ) ( )( ( ) ( , )))x boy x y girl y taller x y
2. (c)( )( )[( ( , ) ( , )) ( , )]x y a x y b x y c x y
Consider option (c)
GATE- 2016-17 CS GATE Solutions 44
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
( )( ) ( ( , ) ( , )) ( , )
( ) ( ) ( ( , ) ( , )) ( , ) ]
( )( ) ( ( , ) ( , )) ( , )
( )( ) { ( ( , ) ( , )) ( , )
( )( ) ( ( , ) ( , )) ( , )
( )( ) ( ( , ) ( , ))
x y a x y b x y c x y
x y a x y b x y c x y
x y a x y b x y c x y
x y a x y b x y c x y
x y a x y b x y c x y
x y a x y b x y
( , )c x y
3. (a) we cannot implement every function using Ex – NOR connective
4. (a) f(x, y) = 1 when (a) x = 1, y = 0 i.e. at xy
(b) x = 1, y = 1 i.e. at xySo, ( , )f x y xy xy
( )
.1
( , )
x y y
x
f x y x
5. (b) I1: let r : it rains
P : cricket match was played
The first statement is (premise)
r PSecond premise is P
Conclusion: rIt is valid if
( )r P P r
r P P r As P q P q
r P P P r
r P F r
r P r
I2: premises are:
r P
Conclusion:r
P
It is valid only if
( ) ( )
( ) ( )
( ) ( )
( ) ( )
r P r P
r P r P
r r r r P
r P r P
which is not true
6. (c) real (x): x is a real number
GATE- 2016-17 CS GATE Solutions 45
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Rational (x): x is a rational number
The statement can be interpreted as
There exist an x such that x is real number and x is also rational numberi.e. ( )( ( ) ( ))x real x rational x
7. (b)
Since we are deriving the conclusion (PQ) from the premise (PQ) and (QR) option (a) and (c)
are correct.
Consider option (b) when both (P Q) and (Q R) are unsatisfiable or FALSE( ) ( )P Q Q R F F F
And hence
PR becomes false
So option (d) is also true
8. (a)
( ( )) (( ) )
( ( ) )
( ( ) )
( ) ( )
( ) ( )
( ) ( )
P Q R P Q R
P Q R
P Q R
P Q R P Q R
P Q R P Q R
P Q R P Q R
which is false when P and Q are true and R is false.
It is true whenever R is true
So, it is not a tautology, not a contradiction and it is not valid.
9. (c) Since in all the options we have P as valid, so we do not need to check for P.
: ( ) ( ( )
( ) ( ( )
( ) ( ( ))
( ) ( ( ))
Q P q q P r r
focuson
P q q P r
P q q P r
P q q P r
( ) ( )
(( ) ) (( ) ) (( ) )
( ) ( ) (( ) )
P q q P r
P q q P q P P q r
F P q P q r
( ) (( ) )
( ) ( )
( )
P q P q r
P q T r
P q
So,
( ) ( ( )P q q P r r is false or not valid
GATE- 2016-17 CS GATE Solutions 46
GATE Solutions © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com
Q is not valid
So, option (a) and (d) are not correct.
: [( )R q r PA q P r
Let us focus on:
So, R is not valid
Only option left is (c)
10. (b)
: ( )
( )
( )
( ) ( )
( ) ( )
X P Q R
P Q R
P Q R
P R Q R
P R Q R
So
( ) ( )
( ) ( )
( ) ( )
X Y
X P R Q R
X P R Q R
P R Q R
( ) ( ) ( ) ( )
( ) ( ) ( ) ( )
So, X is true or valid
X Y P R Q R P R Q R
P R P R Q R Q R
T T
X Y T
New Edition with GATE 2015 Solutions is available.
To Buy Postal Correspondence Package call at 0-9990657855
(( ) ) ( )
( ( ) ) ( )
( ) ( )
( ) ( ) ( )
( ) ( ) ( )
( ) ( ) ( )
( ) ( )
q r P q P
q r P q P
q r P q P
q q q P r q
r P P q P
q P q r q
r P P