Programming

Question 1
   
A
4
B
5
C
6
D
7
       Programming       Programming       Gate 2018
Question 1 Explanation: 
Question 2
 
A
0, c
B
0, a+2
C
‘0’, ‘a+2’
D
‘0’, ‘c’
       Programming       Programming       Gate 2018
Question 2 Explanation: 
char x = ‘a’+2;
The x variable here stores a character ‘c’ in it. Because + 2 will increment ascii value of a from 92 to 95. Hence the structure p contains 3 character values and they are ‘1’, ‘0’, and ‘c’. q is a pointer pointing to structure p. Hence q is pointing to ‘1’, q+1 pointing to ‘0’ and q+2 pointing to ‘c’. Option d cannot be correct, as though they are characters, printf will not print them in single quotes.
Question 3
 
A
Hi Bye Bye Hi
B
Hi Bye Hi Bye
C
Bye Hi Hi Bye
D
Bye Hi Bye Hi
       Programming       Programming       Gate 2018
Question 3 Explanation: 
The first call to the function ‘func1(str1, str2);’ is call by value. Hence, any change in the formal parameters are NOT reflected in actual parameters. Hence, str1 points at “hi” and str2 points at “bye”.
The second call to the function ‘func2(&str1, &str2);’ is call by reference. Hence, any change in formal parameters are reflected in actual parameters. Hence, str1 now points at “bye” and str2 points at “hi”.
Hence answer is “hi bye bye hi”.
Question 4
 
A
4
B
5
C
6
D
40
       Programming       Programming       Gate 2018
Question 4 Explanation: 
Since 240 is the input, so first loop will make j=40.
Next for loop will divide j value (which is 40) by 2, each time until j>1.
j loop starts:
j=40 & sum=1
j=20 & sum=2
j=10 & sum=3
j=5 & sum=4
j=2 & sum=5
j=1 & break
So sum = 5.
Question 5
 
A
10230
B
10231
C
10232
D
10233
       Programming       PRogramming       Gate 2018
Question 5 Explanation: 
#include
int count=0;
Count(x,y){
if(y!=1){
if(x!=1){
printf("*");
count = count +1;
Count(x/2,y);
}
else{
y=y-1;
Count(1024,y);
}
}
}
void main()
{
Count(1024,1024);
printf("\n%d\n",count);
}


Count ( ) is called recursively for every (y = 1023) & for every y, Count ( ) is called (x = 10) times = 1023 × 10 = 10230
Question 6
   
A
compiler error as the return of malloc is not typecast approximately
B
compiler error because the comparison should be made as x==NULL and not as shown
C
compiles successfully but execution may result in dangling pointer
D
compiles successfully but execution may result in memory leak
       Programming       Programming       Gate 2017 set-01
Question 6 Explanation: 
Option A: In C++, we need to perform type casting, but in C Implicit type casting is done automatically, so there is no compile time error, it prints10 as output.
Option B: NULL means address 0, if (a = = 0) or (0 = = a) no problem, though we can neglect this, as it prints 10.
Option C: x points to a valid memory location. Dangling Pointer means if it points to a memory location which is freed/ deleted.
int*ptr=(int*)malloc(sizeof(int));
free(ptr); //ptr becomes a dangling pointer
ptr=NULL; //Removing Dangling pointers condition
Option D: x is assigned to some memory location
int*x=malloc(sizeof(int));
→(int*)malloc(sizeof(int)) again assigns some other location to x, previous memory location is lost because no new reference to that location, resulting in Memory Leak.
Hence, Option D.
Question 7
 
A
53423122233445
B
53423120112233
C
53423122132435
D
53423120213243
       Programming       Programming       Gate 2017 set-01
Question 7 Explanation: 


In fun2, we increment (pre) the value of n, but in fun1, we are not modifying the value. Hence increment in value in recursion (back).
Hence, 5 3 4 2 3 1 2 2 2 3 3 4 4 5.
Question 8
 
A
Return of 6 and 6 respectively.
B
Infinite loop and abnormal termination respectively.
C
Abnormal termination and infinite loop respectively.
D
Both terminating abnormally.
       Programming       Programming       Gate 2017 set-01
Question 8 Explanation: 
while(val>0)
{ x=x+foo(val--);
} In this case foo(val--) is same as foo(val) & val--;
Because the recursive function call is made without changing the passing argument and there is no Base condition which can stop it.
It goes on calling with the same value ‘val’ & the system will run out of memory and hits the segmentation fault or will be terminated abnormally. The loop will not make any difference here.
while(val>0)
{
x=x+bar(val-1);
}
bar(3) calls bar(2)
bar(2) calls bar(1)
bar(1) calls bar(0) ⇾ Here bar(0) will return 0.
bar(1) calls bar(0)
bar(1) calls bar(0)……..
This will continue.
Here is a problem of infinite loop but not abrupt termination. Some compilers will forcefully preempt the execution.
Question 9
 
A
3
B
4
C
5
D
6
       Programming       Programming       Gate 2017 set-01
Question 9 Explanation: 
main( )
{
char*x="abc\"";
char*y="defgh";
printlength(x,y);
}
printlength(char*3,char*t)
{
unsigned int c=0;
int len=((strlen(s)-strlen(t))>c)?
strlen(s):strlen(t);
printf("%d",len);
}
Here strlen(s)-strlen(t)=3-5=-2
But in C programming, when we do operations with two unsigned integers, result is also unsigned. (strlen returns size_t which is unsigned in most of the systems).
So this result '-2' is treated as unsigned and its value is INT_MAX-2. Now the comparison is in between large number & another unsigned number c, which is 0.
So the comparison returns TRUE here.
Hence (strlen(s)-strlen(t))>0 will return TRUE, on executing, the conditional operator will return strlen(s)⇒strlen(abc)=3.
Question 10
 
A
23
B
24
C
25
D
26
       Programming       Programming       Gate 2017 set-01
Question 10 Explanation: 
Arithmetic operators have least priority in this case, as count+=v & 1, we first compute v& 1 and then adds to count variable.

Question 11
 
A
0, 0
B
0, 1
C
1, 0
D
1, 1
       Programming       Programming       GATE 2017(set-02)
Question 11 Explanation: 
printxy (int x, int y)
{
int *ptr;
x = 0;
ptr = &x;
y = *ptr;
*ptr = 1;

}
printxy (1, 1)
Question 12
 
A
(q==r) && (r==0)
B
(x>0) && (r== x) && (y>0)
C
(q==0) && (r==x) && (y>0)
D
(q==0) && (y>0)
       Programming       Programming       GATE 2017(set-02)
Question 12 Explanation: 
Divide x by y.
x, y, q, r are unsigned integers.
while (r >= y)
{
r = r – y;
q = q + 1;
}
Loop terminates in a state satisfying the condition
x = = (y * q + r)
y ⇒ Dividend = Divisor * Quotient + Remainder
So to divide a number with repeated subtractions, the Quotient should be initialized to 0 and it must be incremented for every subtraction.
So initially q=0 which represents
x = 0 + r ⇒ x = r
and y must be a positive value (>0).
Question 13
   
A
3
B
4
C
5
D
6
       Programming       Programming       GATE 2017(set-02)
Question 13 Explanation: 
Swap (&x, &y) exchanges the contents of x & y.

①⇒ 1st for i = 0 <= 4 a[0] < a[1] ≃ 3<5 so perform swapping
done=

①⇒ no swap (3, 1)
②⇾ perform swap (1, 4)

①⇒ perform swap (1, 6)

①⇒ perform swap (1, 2)

①⇒ (done is still 0)
for i = 5 >= 1 a[5] > a[4] ≃ 1>2 – false. So, no swapping to be done
②⇾ no swap (6, 2)
③⇾ swap (4, 6)

①⇒ Swap (3, 6)

①⇒ Swap (5, 6)

⇒ Done is still 0. So while loop executes again.
①⇾ no swap (6, 5)
done = 1
②⇾ No swap (5, 3)
③⇾Swap (3, 4)

So, array [3] = 3
Question 14
A
0
B
1
C
2
D
3
       Programming       Programming       GATE 2017(set-02)
Question 14 Explanation: 

n=++m; (pre)
n=11
n1=m++ (post)
Here, n1=11, but m will be updated to 12.
n-- & --n1 decrements 11 to 10 & subtracting gives us zero.
Question 15
A
1
B
2
C
4
D
6
       Programming       Programming       GATE 2017(set-02)
Question 15 Explanation: 
char * C = “GATECSIT2017”;
char * P = C;
(int) strlen (C + 2[P] – 6[P] – 1)

C + 2[P] - 6[P] – 1 ∵2[P] ≃ P[2]
100 + P[2] – P[6] – 1
100 + T – I – 1
100 + 84 – 73 – 1 ASCII values T – 84, I – 73
100 + 11 – 1
= 110
(int) strlen (110)
strlen (17) ≃ 2
Question 16
A
f(s,*s)
B
i= f(i,s)
C
f(i,*s)
D
f(i,*p)
       Programming       Programming       2016 set-01
Question 16 Explanation: 
int i = 100;
short s = 12;
short *p = &s;
_______ // call to f ( ) :: (void f(int,short);)
It is clearly mentioned the return type of f is void.
By doing option elimination
(A) & (C) can be eliminated as s is short variable and not a pointer variable.
(B) i = f(i, s) is false because f’s return type is void, but here shown as int.
(D) f(i, *p)
i = 100
*p = 12
Hence TRUE
Question 17
A
2016
B
2017
C
2018
D
2019
       Programming       Programming       2016 set-01
Question 17 Explanation: 

For the first mystery (&a, &b);

temp = ptr b
ptr b = ptr a
ptr a = temp
If (a The function mystery (int *ptra, int *ptrb) does not change the value of variables pointed by pointers, instead it only changes the addresses inside the pointers, which is in no way going to modify the values of a, b, c, d.
Hence, a = 2016 will be printed.
Question 18
 
A
a != n
B
b != 0
C
b > (a + 1)
D
b != a
       Programming       Programming       2016 set-01
Question 18 Explanation: 
main ( )
{
int arr [ ] = {3, 2, 1, 5, 4};
int n = sizeof(arr) / sizeof (arr[0]);
printf (max(arr, 5));
}
int max (int *p, int n)
{
int a = 0, b = n – 1;
(while (a!=b))
{
if (p[a] <= p[b])
{
a = a + 1;
}
else
{
b =b – 1;
}
}
return p[a];
}
The function computes the maximum value contained in an integer array p [ ] of size n (n >= 1).
If a = = b, means both are at same location & comparison ends.
Question 19
A
3 1 2 2 1 3 4 4 4
B
3 1 2 1 1 1 2 2 2
C
3 1 2 2 1 3 4
D
3 1 2 1 1 1 2
       Programming       Programming       2016 set-01
Question 19 Explanation: 

Count (3)
static int d = 1
It prints 3, 1
d++; //d = 2
n>1, count(2)
prints 2, 2
d++; // d = 3
n>1, count(1)
prints 1, 3 → Here n = 1, so condition failed & printf (last statement) executes thrice & prints d
d++; //d=4 value as 4. For three function calls, static value retains. ∴ 312213444
Question 20
A
6, 2
B
6, 6
C
4, 2
D
4, 4
       Programming       Programming       2016 set-01
Question 20 Explanation: 

First m(a) is implemented, as there are no local variables in main ( ), it takes global a = 3;
m(3) is passed to m(y).
a = 1
a = 3 – 1 = 2
n(2) is passed to n(x).
Since it is dynamic scoping
x = 2 * 2 = 4 (a takes the value of its calling function not the global one).
The local x is now replaced in m(y) also.
Hence, it prints 4,4.
And we know it prints 6, 2 if static scoping is used. It is by default in C programming.
Question 21
 
A
30
B
31
C
32
D
33
       Programming       Programming       GATE 2016 set-2
Question 21 Explanation: 

P is a pointer stores the address of i, & m is the formal parameter of j.
Now, m = m + 5;
*p = *p + m;

Hence, i + j will be 20 + 10 = 30.
Question 22
   
A
XY =ab
B
(res*a)Y = (res*X)b
C
XY =res*ab
D
XY = (res*a)b
       Programming       Programming       GATE 2016 set-2
Question 22 Explanation: 
int exp (int X, int Y)
{
int res = 1, a = X, b = Y;
while (b != 0)
{
if (b%2 = = 0)
{
a = a*a;
b = b/2;
}
else
{
res = res*a;
b = b – 1;
}
}
return res;
}
From that explanation part you can understand the exponent operation, but to check the conditions, first while iteration is enough.
x = 2, y = 3, res = 2, a = 2, b = 2.
Only (C) satisfies these values.
xy=res*ab
23=2*22=8
Explanation:
Will compute for smaller values.
Let X = 2, Y = 3, res = 1
while (3 !=0)
{
if(3%2 = = 0) - False
else
{
res = 1*2 = 2;
b = 3 – 1 = 2;
}
For options elimination, consider
return res = 2 (but it is out of while loop so repeat while)
__________
while (2 != 0)
{
if (2%2 = = 0) - True
{
a = 2*2 = 4
b = 2/2 = 1
}
__________
repeat while
while (1 != 0)
{
if (1%2 = = 0) - False
else
{
res = 2*4 = 8
b = 1 – 1 = 0
}
__________
while (0 != 0) - False
return res = 8 (23)
Question 23
 
A
3
B
4
C
5
D
6
       Programming       Programming       GATE 2016 set-2
Question 23 Explanation: 
Given

f(a, 5) ⇒ f(100, 5)
Question 24
A
-5
B
6
C
7
D
8
       Programming       Programming       GATE 2015 (Set-01)
Question 24 Explanation: 
Function f1 will not swap the value of 'a' and 'b' because f1 is call by value.
But f2 will swap the value of 'b' and 'c' because f2 is call by reference. So finally the value of
a=4
b=6
c=5
So, answer will be
c - a - b
5 - 4 - 6 = -5
Question 25
 
A
2036, 2036, 2036
B
2012, 4, 2204
C
2036, 10, 10
D
2012, 4, 6
       Programming       Programming       GATE 2015 (Set-01)
Question 25 Explanation: 
⇒ Address of x = 2000
⇒ x [4] [3] can represents that x is a 2-dimensional array.
⇒ x+3 = (Address of x) + 3 * 4 * 3 [3×4×3 is inner dimention]
= 2000 + 36
= 2036
⇒ *(x+3) also returns the address i.e., 2036.
The '*' represents 1 - D but x is starting at 2036.
⇒ *(x+3)+3 = *(Address of x + 2 * 3 * 4) + 3
= *(2000 + 24) +3
= *(2024) + 3 ['*' will change from 2D to 1D]
= 2024 + 3 * 4
= 2024 + 12
= 2036
Question 26
 
A
{r = qx+y ∧ r
B
{x = qy+r ∧ r
C
{y = qx+r ∧ 0
D
{q+10}
       Programming       Programming       GATE 2015 (Set-01)
Question 26 Explanation: 
The loop terminates when r In each iteration q is incremented by 1 and y is subtracted from 'r'. Initial value of 'r' is 'x'. So, loop iterates x/y times and q will be equal to x/y and r = x%y.
⇒ x = qy + r
Question 27
A
5
B
6
C
7
D
8
       Programming       Programming       GATE 2015 (Set-01)
Question 27 Explanation: 
Note: Out of syllabus.
Question 28
 
A
n3
B
n(log n) 2
C
nlog n
D
nlog (log n)
       Programming       Programming       GATE 2015 (Set-01)
Question 28 Explanation: 
int fun1 (int n) {
int i, j, k, p, q = 0;
for (i=1; i
return q;
}
∴ nlog(logn)
Question 29
 
A
ABCD EFGH
B
ABCD
C
HGFE DCBA
D
DCBA
       Programming       Programming       GATE 2015 -(Set-2)
Question 29 Explanation: 

if condition fails
& returns controls
∴ DCBA will be pointed
Question 30
 
A
51
B
52
C
53
D
54
       Programming       Programming       GATE 2015 -(Set-2)
Question 30 Explanation: 
Recurrence Relation is
f(n) = 1; if n = 1
Question 31
A
-2
B
2
C
-1
D
15
       Programming       Programming       GATE 2015 -(Set-2)
Question 31 Explanation: 
When first time stkFunc (-1,10) will be called then, inside Switch(opcode) the control will go to Case-I, where size=10.
When next time stkFunc (0,5) is called then, inside Switch(opcode), the control will go to Case-0, where A[0]=5 and stkTop = 0+1 =1.
When next time stkFunc (0,10) is called then, inside Switch (opcode), the control will go to Case '0', where A[1]=10 and stkTop=1+1=2.
When next time stkFunc(1,0) is called from inside the printf statement, then inside Switch(opcode), the control will go to default and stkTop = 2-1 = 1 and value of A[1] will get returned, i.e., 10.
When next time stkFunc(1,0) is called from inside the printf statement, then inside Switch(opcode), the control will go to default and stkTop = 1-1 = 0 and value of A[0] will get returned, i.e., 5.
Finally the two values 10 & 5 will be added and printed.
Question 32
A
12
B
120400
C
1204
D
1034
       Programming       Programming       GATE 2015(Set-03)
Question 32 Explanation: 
p = s1+2;
p now points to third element in s1, i.e., '3'.
*p = '0', will make value of '3' as '0' in s1. And finally s1 will become 1204.
Question 33
 
A
15
B
25
C
35
D
45
       Programming       Programming       GATE 2015(Set-03)
Question 33 Explanation: 
Question 34
 
A
140
B
150
C
160
D
170
       Programming       Programming       GATE 2015(Set-03)
Question 34 Explanation: 

**ptr = 40
∴ printf (“%d%d”, p + r – p, p + r) will print 140.
Question 35
   
A
10
B
11
C
12
D
13
       Programming       Programming       GATE 2015(Set-03)
Question 35 Explanation: 
j = 2*3 / 4+2.0 / 5+8 / 5;
= 6 / 4+2.0 / 5+1;
= 1 + 0.4 + 1
= 2.4
But since j is integer,
j=2
Now,
k = k - (--j)
k = 0 - (1) = -1
When i=0, i+k = -1,
printf executed 1 time
When i=1, i+k = 0,
printf executed 1 time
When i=2, i+k = 1,
printf executed 3 times
When i=3, i+k = 2,
printf executed 3 times
When i=4, i+k = 3,
printf executed 2 times
∴ Total no. of times printf executed is,
1 + 1 + 3 + 3 + 2 = 10
Question 36
A
230
B
240
C
250
D
260
       Programming       Programming       GATE 2015(Set-03)
Question 36 Explanation: 
x = x + f1( ) + f2( ) + f3( ) + f4( )
f1( ) = 25 + 1 = 26
f2( ) = 50 + 1 = 51
f3( ) = 10 × 10 = 100
f2( ) = 51 × 1 = 52 (Since here x is static variable so old value retains)
∴ x = 1+26+51+100+52 = 230
Question 37
 
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.
       Programming       Programming       GATE 2014(Set-01)
Question 37 Explanation: 
int i; // Initially i takes the Garbage value
int *pi = &i; // pi is a pointer which stores the address of i.
scanf (pi); // pi = &i (we rewrite the garbage value with our values) say x = 2
printf (i+5); // i+5 = x+5 = 2+5 = 7
Hence on execution, the value printed is 5 more than the integer value entered.
Question 38
A
9
B
10
C
11
D
12
       Programming       Programming       Gate 2014 Set -02
Question 38 Explanation: 
func (435)
count = 0 Shift right of 1, which means the number gets half.
while (num)
{ Shift left of 1, which means, the number gets doubled.
count++;
num>>=1;
}
return (count); 435/2 = 217/2 = 108/2 = 54/2 = 27/2 = 13/2 = 6/2 = 3/2 = 1/2 = 0
Count: 1, 2, 3, 4, 5, 6, 7, 8, 9. Χ
(or)
(435)10 = (110110011)2
The given program counts total number of bits in binary representation and fails when while (num) becomes all zeroes.
Question 39
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;
       Programming       Programming       Gate 2014 Set -02
Question 39 Explanation: 
n & p are unsigned int variable.
From the options n*(n-1)*(n-2) will go out of range. So eliminate A & D.
n*(n-1) is always an even number. So subexpression n(n-1)/2 also an even number.
n*(n-1)/ 2*(n-2), gives a number which is a multiple of 3. So dividing with 3 will not have any loss. Hence B is option.
Question 40
A
1.73
B
1.74
C
1.75
D
1.76
       Programming       Programming       Gate 2014 Set -02
Question 40 Explanation: 
double f(double x){
if( abs(x*x – 3) < 0.01) return x;
else return f(x/2 + 1.5/x);
}
We know that for
1) abs(x*x–3) < 0.01
x2 – 3 < 0.01 & – (x2 – 3) < 0.01
x2 < 3.01 & –x2 < 2.99
x < 1.73 & x < 1.72
2) x = x/2 + 1.5/x
x=(x2+(1.5)2)/2x
x=(x2+3)/2x⇒2x2-x2=3
x2=3
x=√3=1.732
Whichever the case we get around 1.72, 1.73 & max 1.74.
Question 41
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.
       Programming       Programming       Gate 2014 Set -02
Question 41 Explanation: 
int f(int j) // if j=50,
{ i is also 50,
static int i = 50; if (i == j) // True
int k; prints something
if (i == j) and called f(50) again,
{ there is no base condition
printf(“something”); to get out of recursion.
k = f(i); So it goes into infinite loop
return 0; when j=50.
}
else return 0;
}
Question 42
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
       Programming       Programming       Gate 2014 Set -03
Question 42 Explanation: 
Let be a small matrix.
For first row iteration, it get swapped and becomes
For second row iteration, it comes to the original position
=A
So, it is the same matrix A.
Question 43
A
Θ(n2)
B
Θ(n2 log n)
C
Θ(n3)
D
Θ(n3 logn)
       Programming       Programming       Gate 2013
Question 43 Explanation: 
Outer loop runs for (n/2) times and inner loop runs for (logn) times.
So, the total number of times loop runs is (n/2 logn).
So, the final k value will be n/2*(n/2 logn) = O(n2logn)
= (n/2+1).n/2 ∙log n
= (n2log n)
Question 44

A
None
B
2 only
C
3 and 4 only
D
4 only
       Programming       Programming       Gate 2013
Question 44 Explanation: 
The test cases 3 & 4 captures the flaw. The code does not works fine when an old character is replaced by a new character & new character is again replaced by another new character.
1, 2 works fine, 3, 4 carries flaw.
Question 45
 
A
(i) and (iii)
B
(i) and (iv)
C
(ii) and (iii)
D
(ii) and (iv)
       Programming       Programming       Gate 2007-IT
Question 45 Explanation: 
The function can be terminated for all the values which can have factor of 2{(2-x)2 == 0}, so (i) is false and (ii) is true.
→ Let n=3, then it is terminated in 2nd iteration.
→ Let n=5, then sequence is 5→14→7→20→10 and it will repeat.
→ Any number with factor 5 and 2 leads to infinite recursion.
So, (iv) is True and (iii) is False.
Question 46
   
A
jungle, n, 8, ncestor
B
etter, u, 6, ungle
C
cetter, k, 6, jungle
D
etter, u, 8, ncestor
       Programming       Programming       Gate 2006-IT
Question 46 Explanation: 
Lets take the part of program,
Line 1 - main ( )
Line 2 - {
Line 3 - struct test *p = st;
Line 4 - p += 1;
Line 5 - ++p → c;
Line 6 - printf("%s", p++→ c);
Line 7 - printf("%c", +++p → c);
Line 8 - printf("%d", p[0].i);
Line 9 - printf("%s\n", p → c);
Line 10 - }
Now,
Line 3: Initially p is pointing to st, i.e., first element of st which is {5, "become"}
Line 4: Now p is pointing to {4, "better"}
Line 5: ++(p → c), since → has higher precedence, so p → c points to 'e' of "better".
Line 6: prints 'enter' and p now points to {6, "jungle"}
Line 7: ***(p → c), since → has higher precedence. So, prints 'u'.
Line 8: p → i, which is 6 so prints '6'.
Line 9: prints 'ungle' since p is pointing to 'u'.
So, output is "enter, u, 6, ungle".
Question 47
 
A
a = 0, b = 3
a = 0, b = 3
B
a = 3, b = 0
a = 12, b = 9
C
a = 3, b = 6
a = 3, b = 6
D
a = 6, b = 3
a = 15, b = 12
       Programming       Programming       Gate 2006-IT
Question 47 Explanation: 
First of all, the swap function just swaps the pointers inside the function and has no effect on the variable being passed.
Inside print 'a' and 'b' are added to odd integers from 1 to 5, i.e., 1+3+5=9. So, in first call to print ab,
a = -3+9 = 6
b = -6+9 = 3
Static variable have one memory throughout the program run (initialized during program start) and they keep their values across function calls. So during second call to print ab,
a = 6+9 = 15
b = 3+9 = 12
Question 48
 
A
8, -12, 7, 23, 8
B
8, 8, 7, 23, 7
C
-12, -12, 27, -31, 23
D
-12, -12, 27, -31, 56
       Programming       Programming       Gate 2006-IT
Question 48 Explanation: 
1) a[0][2] = *(*(a+0)+2)
It returns the value of 3rd element in a1.
First printf print 8.
2) *a[2] = *(*(a+2))
It returns the value of 1st element in a3.
Second printf print -12.
3) *++a[0] = *(++(*(a+0)))
a[0] is pointing to 1st element in a1.
++a[0] - after preincrement performed, now a[0] is pointing to 2nd element in a1.
*++a[0] return the value of 2nd element in a1.
Third printf print 7.
4) *(++a)[0]
++a - after preincrement is performed 'a' is pointing to a2.
(++a)[0] is pointing to 1st element in a2.
*(++a)[0] returns the value of 1st element in a2.
Fourth printf print 23.
5) a[-1][+1] = *(*(a-1)+1)
(a-1) is pointing to a1.
*(a-1) is pointing to the 2nd element in a1, because in 3rd printf already a1 was incremented by 1.
*(a-1)+1 is pointing 3rd element in a1.
*(*(a-1)+1) returns the value of 3rd element in a1, i.e., 8.
Question 49
   
A
(n == 0) || (m == 1)
B
(n == 0) && (m == 1)
C
(n == 0) || (m == n)
D
(n == 0) && (m == n)
       Programming       Programming       Gate 2006-IT
Question 49 Explanation: 
We know that,
mC0 = 1
nCn = 1
Question 50
A
A : count [a[j]]++ and B : count[b[j]]–
B
A : count [a[j]]++ and B : count[b[j]]++
C
A : count [a[j++]]++ and B : count[b[j]]–
D
A : count [a[j]]++and B : count[b[j++]]–
       Programming       Programming       Gate 2005-IT
Question 50 Explanation: 
A: Increments the count by 1 at each index that is equal to the ASCII value of the alphabet, it is pointing at.
B: Decrements the count by 1 at each index that is equal to the ASCII value of the alphabet it is pointing at. Also it increments the loop counter for next iteration.
If one string is permutation of other, there would have been equal increments and decrements at each index of array, and so count should contain zero at each index, that is what the loop checks at last and if any non-zero elements is found, it returns 0 indicating that strings are not anagram to each other.
Question 51
A
1, 2, 3, 4, 5, 6, 7
B
2, 1, 4, 3, 6, 5, 7
C
1, 3, 2, 5, 4, 7, 6
D
2, 3, 4, 5, 6, 7, 1
       Programming       Programming       Gate 2005-IT
Question 51 Explanation: 
It is nothing but a pairwise swapping of the linked list.
Question 52
A
a[j] – a[i] > S
B
a[j] – a[i] < S
C
a[i] – a[j] < S
D
a[i] – a[j] > S
       Programming       Programming       Gate 2005-IT
Question 52 Explanation: 
For some 'i' if we find that difference of (A[j] - A[i] < S) we increment 'j' to make this difference wider so that it becomes equal to S.
If at times difference becomes greater than S we know that it won't reduce further for same 'i' and so we increment the 'i'.
Question 53
   
A
5
B
8
C
9
D
20
       Programming       Programming       Gate 2005-IT
Question 53 Explanation: 

Hence, 9 is the answer.
Question 54
 
A
x = 1 + x;
B
x = 1 - x;
C
x = x - 1;
D
x = 1 % x;
       Programming       Programming       Gate 2004-IT
Question 54 Explanation: 
x = 1 - x
For x = 0, it gives 1.
For x = 1, it gives 0.
Question 55

A program attempts to generate as many permutations as possible of the string, 'abcd' by pushing the characters a, b, c, d in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program?

A
abcd
B
dcba
C
abad
D
cabd
       Programming       Programming       Gate 2004-IT
Question 55 Explanation: 
A) push 'a' and pop 'a', push 'b' and pop 'b', push 'c' and pop 'c', and finally push 'd' and pop 'd'. Sequence of popped elements will come to abcd.
B) First push abcd, and after that pop one by one. Sequence of popped elements will come to dcba.
C) push abc, and after that pop one by one. Sequence of popped elements will come to cba. Now push 'd' and pop 'd', final sequence comes to cbad.
D) This sequence is not possible because 'a' cannot be popped before 'b' anyhow.
Question 56
A
B
C
D
       Programming       Programming       Gate 2004-IT
Question 56 Explanation: 
To compute transpose 'j' needs to be started with 'i'. So, A and B are wrong.
In (D) , given statements is wrong as temporary variable needs to be assigned some value and not vice-versa.
Question 57
A
43 80
B
42 74
C
33 37
D
32 32
       Programming       Programming       Gate 2004-IT
Question 57 Explanation: 
In first iteration:
In first case of funcf, which in turn calls funcg, y becomes 11 and it returns 5+11=16.
In second call of funcg, y becomes 12 and it returns 5+12=17.
So, in main y is incremented by 16+17=33 to become 10+33 =43.
In second iteration:
y will be incremented by 18+19=37 to give 43+37=80.
Question 58
A
?1 is getchar() ! = ‘\n’
?2 is getchar(c);
B
?1 is (c = getchar()); ! = ‘\n’
?2 is getchar(c);
C
?1 is c! = ‘\n’
?2 is putchar(c);
D
?1 is (c = getchar()) ! = ‘\n’
?2 is putchar(c);
       Programming       Programming       Gate 2004-IT
Question 58 Explanation: 
getchar( ) = reads a single character at a time from the stdin.
putchar( ) = writes a character specified by the argument to stdout.
As getchar( ) and putchar( ), both are needed to read the string and prints its reverse and only option (D) contains both the function. (D) is the answer.
Now coming to the code, wrt_id(void) is calling itself recursively. When \n is encountered, putchat( ) gets executed and prints the last character and then the function returns to its previous call and prints last 2nd character and so on.
Question 59
 
A
AB
UV
VW
VW
B
AB
UV
AB
VW
C
AB
UV
UV
VW
D
AB
UV
VW
UV
       Programming       Programming       Gate 2004-IT
Question 59 Explanation: 
→ First print AB.
→ f1 is call by value. The changes applicable only for local from f1. UV is printed.
→ Back in main( ), AB is printed.
→ Then in f2, VW is printed.
Hence, answer is (B).
Question 60
 
A
89
B
90
C
91
D
92
       Programming       Programming       Gate-1998
Question 60 Explanation: 
Value returned by
fun(95) = fun(fun(106))
= fun(96)
= fun(fun(107))
= fun(97)
= fun(fun(108))
= fun(98)
= fun(fun(109))
= fun(99)
= fun(110)
= fun(100)
= fun(fun(111))
= fun(101)
= 91
Question 61
 
A
5
B
25
C
36
D
42
       Programming       Programming       Gate-1998
Question 61 Explanation: 
If it is call by reference then answer is 42.
If it is call by value then answer is 36.
Question 62
 
A
2
B
C
Run time error
D
None of the above
       Programming       Programming       Gate-1995
Question 62 Explanation: 
Since nothing is said in question. So we will assume by default call by value.
X in the procedure FIND is a local variable. No change will be reflected in global variable X.
Question 63
An unrestricted use of the “goto” statement is harmful because
A
it makes it more difficult to verify programs
B
it increases the running time of the programs
C
it increases the memory required for the programs
D
it results in the compiler generating longer machine code
       Data-Structures       Programming       Gate-1994
Question 63 Explanation: 
If we use "goto" statements then it leads to structural decomposition of code then it is difficult to verify the programs.
Question 64
 
A
1, because m is a local variable in P
B
0, because m is the actual parameter that corresponds to the formal parameter in p
C
0, because both x and y are just reference to m, and y has the value 0
D
1, because both x and y are just references to m which gets modified in procedure P
E
none of the above
       Programming       Programming       Gate-1993
Question 64 Explanation: 
0, because global m is not modified, m is just passed to formal argument of P.
Question 65
   
A
0, because n is the actual parameter corresponding to x in procedure Q.
B
0, because n is the actual parameter to y in procedure Q.
C
1, because n is the actual parameter corresponding to x in procedure Q.
D
1, because n is the actual parameter corresponding to y in procedure Q.
E
none of the above
       Programming       Programming       Gate-1993
Question 65 Explanation: 
0, because n is just passed to formal parameters of Q and no modification in global n.
Question 66
   
A
PARAM, P, Q
B
PARAM, P
C
PARAM, Q
D
P, Q
E
none of the above
       Programming       Programming       Gate-1993
Question 66 Explanation: 
Since m is defined global it is visible inside all the procedures.
Question 67
 
A
exchanges a and b
B
doubles a and stores in b
C
doubles b and stores in a
D
leaves a and b unchanged
E
none of the above
       Programming       Programming       Gate-1993
Question 67 Explanation: 
Exchanges a and b.
Let us consider a=5; b=2
a := a+b = 5+2 = 7
b := a-b = 7-2 = 5
a := a-b = 7-5 = 2
O/P: a=2; b=5
Question 68
 
A
10
B
11
C
3
D
None of the above
       Programming       Programming       Gate-2001
Question 68 Explanation: 
n=3
W(n)=W(3)
Procedure W(var x; int)
begin
x = x+1 = 3+1 = 4
Print x → Print x=4
end
Question 69
An unrestricted use of the "go to" statement is harmful because of which of the following reason (s):
A
It makes it more difficult to verify programs.
B
It makes programs more inefficient.
C
It makes it more difficult to modify existing programs.
D
It results in the compiler generating longer machine code.
       Programming       Programming       Gate-1989
Question 69 Explanation: 
Dijkstra's argued that unrestricted goto statements should abolished from the higher-level languages because they complicated the task of analyzing and verifying the correctness of programs.
Question 70
 
A
3, 6
B
6, 7
C
3, 7
D
None of the above.
       Programming       Programming       GATE-1987
Question 70 Explanation: 
First procedure Q is called from the main procedure. Q has local variables x and y with values 3 and 4 respectively. This local variable y (value 4) is being parsed to procedure P during call, and received in local variable n inside procedure P. Now as P does not have any local definition for variable x, it will assign the evaluated value of (n+2)/(n-3), i.e., (4+2)/(4-3)=6 to the global variable x, which was previously 7. After the call of procedure P, procedure Q writes the value of local variable x which is still 3. Lastly, the main procedure writes the value of global variable x, which has been changed to 6 inside procedure P. So, the output will be 3, 6.
Question 71
 
A
3, 6
B
6, 7
C
3, 7
D
None of the above
       Programming       Programming       GATE-1987
Question 71 Explanation: 
The same sequence of statements will be executed using dynamic scoping. However, as there is no local definition of variable x in procedure P, it will consider the recent definition in the calling sequence, as P is being called from procedure Q, definition of x from Q will be changed to 6 from 3. Now, when Q writes local variables x, 6 will be printed. The write global variable x from main procedure will print 7 (as value of the global variable x has not been changed). So, the output will be 6, 7.
There are 71 questions to complete.