## Programming

 Question 1

Let x be an integer which can take a value of 0 or 1. The statement if(x = =0) x = 1; else x = 0; is equivalent to which one of the following?

 A x = 1 + x; B x = 1 – x; C x = x – 1; D x = 1 % x;
Programming       Programming       GATE 2004-IT
Question 1 Explanation:
x = 1 – x
For x = 0, it gives 1.
For x = 1, it gives 0.
 Question 2

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 2 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 3

Consider the following C program which is supposed to compute the transpose of a given 4 x 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program.

```#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (1 = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
}```
 A for(j = 0; j < 4; ++j){ t = M[i][j]; M[i][j] = M[j][i]; M[j][i] = t; } B for(j = 0; j < 4; ++j){ M[i][j] = t; t = M[j][i]; M[j][i] = M[i][j]; } C for(j = i; j < 4; ++j){ t = M[i][j]; M[i][j] = M[j][i]; M[j][i] = t; } D for(j = i; j < 4; ++j){ M[i][j] = t; t = M[j][i]; M[j][i] = M[i][j]; }
Programming       Programming       GATE 2004-IT
Question 3 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 4

What is the output of the following program?

```#include <stdio.h>
int funcf (int x);
int funcg (int y);

main()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count)
{
y += funcf(x) + funcg(x);
printf ("%d ", y);
}
}

funcf(int x)
{
int y;
y = funcg(x);
return (y);
}

funcg(int x)
{
static int y = 10;
y += 1;
return (y+x);
}```
 A 43 80 B 42 74 C 33 37 D 32 32
Programming       Programming       GATE 2004-IT
Question 4 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 5

Choose the correct option to fill the ?1 and ?2 so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.

```#include
void wrt_it (void);
int main (void)
{
printf("Enter Text");
printf ("n");
wrt_ it();
printf ("n");
return 0;
}
void wrt_it (void)
{
int c;
if (?1)
wrt_it();
?2
} ```
 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 5 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, putchar( ) 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 6

Consider the following C program:

```#include <stdio.h>
typedef struct
{
char *a;
char *b;
} t;
void f1(t s);
void f2(t *p);
main()
{
static t s = {"A", "B"};
printf ("%s %sn", s.a, s.b);
f1(s);
printf ("%s %sn", s.a, s.b);
f2(&s);
}
void f1(t s)
{
s.a = "U";
s.b = "V";
printf ("%s %sn", s.a, s.b);
return;
}
void f2(t *p)
{
p -> a  = "V";
p -> b = "W";
printf("%s %sn", p -> a, p -> b);
return;
} ```
What is the output generated by the program?

 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 6 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.
 Question 7

Consider the following C functions.

```  int fun1 (int n)  {                              int fun2 (int n)  {
static int i = 0;                                static int i = 0;
if (n > 0)  {                                    if (n > 0)  {
++i;                                              i = i + fun1 (n);
fun1 (n-1);                                       fun2 (n-1);
}                                                 }
return (i);                                           return (i);
}                                                 } ```

The return value of fun2 (5) is _______.

 A 55
Programming       Functions       GATE 2020       Video-Explanation
Question 7 Explanation:
#include
int fun1(int n) {
printf(“–fun1 call–\n”);
static int i = 0;
if(n>0){
++i;
printf(“fun1(%d-1)\n”,n);
fun1(n-1);
}
printf(“fun1(%d)= %d\n”,n, i);
return(i);
}
int fun2(int n) {
printf(“\n******* fun2 call ********\n”);
static int i = 0;
if(n>0){
printf(“%d + fun1(%d)\n”, i,n);
i=i+fun1(n);
fun2(n-1);
}
printf(“fun2(%d)= %d\n”,n, i);
return(i);
}
void main()
{
printf(“final = %d\n”, fun2(5));
}
Check step by step hand run of the code to understand the recursion:
******* fun2 call ********
0 + fun1(5)
–fun1 call–
fun1(5-1)
–fun1 call–
fun1(4-1)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 5
fun1(1)= 5
fun1(2)= 5
fun1(3)= 5
fun1(4)= 5
fun1(5)= 5
******* fun2 call ********
5 + fun1(4)
–fun1 call–
fun1(4-1)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 9
fun1(1)= 9
fun1(2)= 9
fun1(3)= 9
fun1(4)= 9
******* fun2 call ********
14 + fun1(3)
–fun1 call–
fun1(3-1)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 12
fun1(1)= 12
fun1(2)= 12
fun1(3)= 12
******* fun2 call ********
26 + fun1(2)
–fun1 call–
fun1(2-1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 14
fun1(1)= 14
fun1(2)= 14
******* fun2 call ********
40 + fun1(1)
–fun1 call–
fun1(1-1)
–fun1 call–
fun1(0)= 15
fun1(1)= 15
******* fun2 call ********
fun2(0)= 55
fun2(1)= 55
fun2(2)= 55
fun2(3)= 55
fun2(4)= 55
fun2(5)= 55
final = 55
 Question 8
Consider the following C functions.
int tob (int b, int* arr) {
int i;
for (i=0; b>0; i++) {
if (b%2) arr [i] = 1;
else arr [i] = 0;
b = b/2;
}
return (i);
}

int pp (int a, int b) {
int arr ;
int i, tot = 1, ex, len;
ex = a;
len = tob (b,arr);
for (i=0; i if (arr[i] == 1)
tot = tot * ex;
ex = ex * ex;
}
return (tot);
}
The value returned by pp(3,4) is ________.
 A 81
Programming       Functions       GATE 2020       Video-Explanation
Question 8 Explanation:
pp(3,4) ⇒
a=3,b=4
tot=1
ex=a=3
len=tob(b,arr) which is 3
[
tob(4,arr)==>
b=4
b%2 =4%2=0 Condition is false then arr=0
=> b=b/2 =4/2 =2
b=2
b%2 =2%2=0 condition is false then arr=0
=>b=b/2=2/2=1
b=1
then b%2=1%2 condition is true then arr=1
=>b=b/2=1/2=0
The i value is 3 [length is 3]
]
i=0,
arr ==1 condition is false
ex=3*3=9
i=1
arr==1 condition is false
then
ex=9*9=81
i=2
then arr==1 condition is true
tot=tot*ex=1*81=81
ex=81*81
Finally it returns tot value which 81.
 Question 9
What is printed by the following ANSI C program?
#include
{
int a =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for( i = 0; i < 3; i++ ){
for(k = 0; k < 3; k++ )
printf(“%d “, a[i][j][k]);
printf(“\n”);
}
return 0;
}
 A 1 2 3 10 11 12 19 20 21 B 1 4 7 10 13 16 19 22 25 C 1 2 3 4 5 6 7 8 9 D 1 2 3 13 14 15 25 26 27
Programming       Arrays       GATE 2022       Video-Explanation
Question 9 Explanation:
Array dimension sizes are 3,3,3 for all 3 dimensions hence, the value assigned to a   will be assigned from only first 3 indices i.e. 0^th, 1^st, & 2^nd and 3 dimensions shown by { }
Hence, 1, 2, 3 will be 1^st row
10 , 11, 12 will be 2^nd row
19, 20, 21 will be 3^rd row
 Question 10
What is printed by the following ANSI C program?
#include
int main(int argc, char *argv[]){
char a = ‘P’;
char b = ‘x’;
char c = (a & b) + ‘*’;
char d = (a | b) – ‘-‘;
char e = (a ^ b) + ‘+’;
printf(“%c %c %c\n”, c, d, e);
return 0;
} A z K S B 122 75 83 C * – + D P x +
Programming       Strings       GATE 2022       Video-Explanation
Question 10 Explanation: Question 11

Given the following Pascal-like program segment

```Procedure A;
x,y: integer;
Procedure B;
x,z: real
S1
end B;
Procedure C;
i: integer;
S2
end C;
end A; ```

The variables accessible in S1 and S2 are

 A x or A, y, x of B and z in S1 and x of B, y and i in S2 B x or B, y and z in S1 and x of B, i and z in S2 C x or B, z and y in S1 and x of A, i and y in S2 D None of the above
Programming       PASCAL-Programming       GATE 1997
Question 11 Explanation:
Note: Out of syllabus.
 Question 12

What value would the following function return for the input x=95?

```                 Function fun (x:integer):integer;
Begin
If x > 100 then fun = x - 10
Else fun = fun(fun(x + 11))
End;  ```
 A 89 B 90 C 91 D 92
Programming       Programming       GATE 1998
Question 12 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 13

What is the result of the following program?

```program side-effect (input, output);
var x, result: integer;
function f (var x:integer):integer;
begin
x:x+1;f:=x;
end;
begin
x:=5;
result:=f(x)*f(x);
writeln(result);
end; ```
 A 5 B 25 C 36 D 42
Programming       Programming       GATE 1998
Question 13 Explanation:
If it is call by reference then answer is 42.
If it is call by value then answer is 36.
 Question 14

Given the programming constructs (i) assignment (ii) for loops where the loop parameter cannot be changed within the loop (iii) if-then-else (iv) forward go to (v) arbitrary go to (vi) non-recursive procedure call (vii) recursive procedure/function call (viii) repeat loop, which constructs will you not include in a programming language such that it should be possible to program the terminates (i.e., halting) function in the same programming language.

 A (ii), (iii), (iv) B (v), (vii), (viii) C (vi), (vii), (viii) D (iii), (vii), (viii)
Programming       Programming-Constructs       GATE 1999
Question 14 Explanation:
Arbitrary goto, recursive call and repeat loop may enter infinite loop and the program might never terminate.
 Question 15

Consider the following C function definition.

```int Trial (int a, int b, int c)
{
if ((a >= b) && (c < b) return b;
else if (a >= b) return Trial(a, c, b);
else return Trial(b, a, c);
} ```

The function Trial:

 A Finds the maximum of a, b, and c B Finds the minimum of a, b and c C Finds the middle number of a, b, c D None of the above
Programming       C-Programming       GATE 1999
Question 15 Explanation:
Try for (3,2,2), it will go for infinite loop.
 Question 16

Suppose we have a function HALTS which when applied to any arbitrary function f and its arguments will say TRUE if function f terminates for those arguments and FALSE otherwise. Example, Given the following function definition.
FACTORIAL (N) = IF(N=0) THEN 1 ELSE N*FACTORIAL (N-1)
Then HALTS(FACTORIAL 4) = TRUE and HATS(FACTORIAL – 5) = FALSE

Let us define the function FUNNY(f) = IF HALTS(ff) THEN not(ff) ELSE TRUE
(a) Show that FUNNY terminates for all functions f.
(b) Use (a) to prove (by contradiction) that it is not possible to have a function like HALTS which for arbitrary functions and inputs says whether it will terminate on that input or not.

 A Theory Explanation.
Programming       Problem-Solving       GATE 1999
 Question 17

What will be the output of the following program assuming that parameter passing is

(i) call by value
(ii) call by reference
(iii) call by copy restore
```      procedure P{x, y, z};
begin y:y+1; z: x+x end;
begin
a:= b:= 3;
P(a+b, a, a);
Print(a)
end. ```
 A Theory Explanation.
Programming       Prameters-passing-mechanisms       GATE 1999
 Question 18

Consider the following pascal program skeleton:

program sort(…);
```                        var a,x,...;
var i,....;
begin
...:=a...
end;
procedure exchange(...);
begin
...:=a...
...:=x...
end;
procedure qsort(...);
var k,v,...;
function partition (...)...;
var i,j,...;
begin
...:=a...
...:=v...
end;
begin
.
.
end;
begin
.
.
end; ```

Assume that at a given point in time during program execution, following procedures are active: sort, qsort(1,9), qsort(1.3), partition(1,3), exchange(1,3).

Show snapshots of the runtime stack with access links after each of the activations.

 A Theory Explanation.
Programming       Pascal       GATE 1999
 Question 19

The following C declarations

```struct node
{
int i;
float j;
};
struct node *s; ```

define s to be

 A An array, each element of which is a pointer to a structure of type node B A structure of 2 fields, each field being a pointer to an array of 10 elements C A structure of 3 fields: an integer, a float, and an array of 10 elements D An array, each element of which is a structure of type node
Programming       C-Programming       GATE 2000
Question 19 Explanation:
The given code represents an array of s[ ], in this each element is a pointer to structure of type node.
 Question 20

The most appropriate matching for the following pairs

```    X: m=malloc(5); m= NULL;        1: using dangling pointers
Y: free(n); n->value=5;         2: using uninitialized pointers
Z: char *p; *p = ’a’;           3. lost memory  ```

is:

 A X – 1 Y – 3 Z – 2 B X – 2 Y – 1 Z – 3 C X – 3 Y – 2 Z – 1 D X – 3 Y – 1 Z – 2
Programming       Match-the-Following       GATE 2000
Question 20 Explanation:
X → m = NULL will results the loss of memory.
Y → n is pointer to invalid memory, a making it as a dangling pointer.
Z → p is not initialized.
p = malloc (size of(char))p = malloc (size of(char)); should have been used before assigning ‘aa’ to ∗p.
 Question 21

Aliasing in the context of programming languages refers to

 A multiple variables having the same memory location B multiple variables having the same value C multiple variables having the same identifier D multiple uses of the same variable
Programming       Aliasing       GATE 2000
Question 21 Explanation:
In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names.
 Question 22

Consider the following C declaration

```struct {
short s 
union {
float y;
long z;
} u;
} t; ```

Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is

 A 22 bytes B 14 bytes C 18 bytes D 10 bytes
Programming       C-Programming       GATE 2000
Question 22 Explanation:
short  = 5×2 = 10
max[float, long] = max [4, 8] = 8
Total = short + max[float,long] = 10 + 8 = 18
 Question 23

The value of j at the end of the execution of the following C program.

```int incr(int i)
{
static int count = 0;
count = count + i;
return (count);
}
main()
{
int i,j;
for (i = 0; i <= 4; i++)
j = incr(i);
} ```

is

 A 10 B 4 C 6 D 7
Programming       C-Programming       GATE 2000
Question 23 Explanation:
At i=0; count=0
i=1; count=1
i=2; count=3
i=3; count=6
i=4; count=10
It return count value is 10.
 Question 24

Consider the following program is pseudo-Pascal syntax.

```    program main;
var x: integer;
procedure Q [z:integer);
begin
z: z + x;
writeln(z)
end;
procedure P (y:integer);
var x: integer;
begin
x: y + 2;
Q(x);
writeln(x)
end;
begin
x:=5;
P(x);
Q(x);
writeln(x)
end. ```

What is the output of the program, when
(a) The parameter passing mechanism is call-by-value and the scope rule is static scooping?

(b) The parameter passing mechanism is call-by-reference and the scope rule is dynamic scooping?

 A Theory Explanation is given below.
Programming       Descriptive       GATE 2000
Question 24 Explanation:
Note: Out of syllabus.
 Question 25

In the C language

 A At most one activation record exists between the current activation record and the activation record for the main B The number of activation records between the current activation record and the activation record for the main depends on the actual function calling sequence. C The visibility of global variables depends on the actual function calling sequence. D Recursion requires the activation record for the recursive function to be saved on a different stack before the recursive fraction can be called.
Programming       C-Programming       GATE 2002
Question 25 Explanation:
In C Language a function can create activation record from the created function stack.
 Question 26

Consider the following logic program P

```A(x) <- B(x, y), C(y)
<- B(x,x) ```

Which of the following first order sentences is equivalent to P?

 A (∀x) [(∃y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∧ ¬(∃x)[B(x,x)] B (∀x) [(∀y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∧ ¬(∃x)[B(x,x)] C (∀x) [(∃y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∨ ¬(∃x)[B(x,x)] D (∀x) [(∀y) [B(x,y) ∧ C(y)] ⇒ A(x)] ∧ (∃x)[B(x,x)]
Programming       Logic-Programming       GATE 2003
Question 26 Explanation:
Note: This is not in gate syllabus. Please ignore this question.
 Question 27

The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.

```global int i = 100, j = 5;
void P(x)
{
int i = 10;
print(x + 10);
i = 200;
j = 20;
print(x);
}
main()
{
P(i + j);
}```

If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program are

 A 115, 220 B 25, 220 C 25, 15 D 115, 105
Programming       Parameter-Passing       GATE 2003
Question 27 Explanation:
P(i+j)
P(100+5) = P(105)
→void P(105)
{
int i=10;
print (x+10);  ⇒ 105+10=115 prints
i=200;
j = 20;
print (x);  ⇒ x=105 prints
}
115, 105 prints
 Question 28

The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.

```global int i = 100, j = 5;
void P(x)
{
int i = 10;
print(x + 10);
i = 200;
j = 20;
print(x);
}
main()
{
P(i + j);
}```

If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program are:

 A 115, 220 B 25, 220 C 25, 15 D 115, 105
Programming       Parameter-Passing       GATE 2003
Question 28 Explanation:
In dynamic,
In void P(x)
{  int i = 10;
print(x + 10);   ⇒ 105+10 = 115 prints print (x);   ⇒ print x=220;
 Question 29

Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed to be either Java or C++, though the syntax is similar.

```Class P {
void f(int i) {
print(i);
}
}

Class Q subclass of P {
void f(int i) {
print(2*i);
}
} ```

Now consider the following program fragment:

```Px = new Q();
Qy = new Q();
Pz = new Q();
x.f(1); ((P)y).f(1); z.f(1); ```

Here ((P)y) denotes a typecast of y to P. The output produced by executing the above program fragment will be

 A 1 2 1 B 2 1 1 C 2 1 2 D 2 2 2
Programming       Variable Binding       GATE 2003
Question 29 Explanation:
Because of using dynamic binding it results a values such as 2 2 2.
Note: The given question is not in the present syllabus
 Question 30

In the following C program fragment, j, k n and TwoLog_n are interger variables, and A is an array of integers. The variable n is initialized to an integer ≥3, and TwoLog_n is initialized to the value of 2*⌈log2(n)⌉

```   for (k = 3; k < = n; k++)
A[k] = 0;
for (k = 2; k < = TwoLog_n; k++)
for (j = k + 1; j < = n; j++)
A[j] = A[j] || (j%k);
for (j = 3; j < = n; j++)
if (!A[j]) printf("%d", j); ```

The set of numbers printed by this program fragment is

 A {m|m ≤ n, (∃i)[m=i!]} B {m|m ≤ n, (∃i)[m=i2]} C {m|m ≤ n, m is prime} D { }
Programming       C-Programming       GATE 2003
Question 30 Explanation:
Take n=4, so Two log_n=4
Now Trace the code,
for (k=3; k<=n; k++)
A[k]=0; // A=0
A=0
for (k=2; k<=Two log_n; k++)
for(j=k+1; j<=n; j++)
A[j] = A[j] // (j%k); // A = 0 // I=1
A = 0 // I=1
for (j=3; j<=n; j++)
if (!A[j]) printf("%d", j);
// if (!1) means if (0), so printf will never execute
Hence, Option (D) is the answer.
 Question 31

Consider the C program shown below.

```#include
#define print(x) printf("%d", x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y + 2;
Q(x);
*y = x - 1;
print(x);
}
main(void)
{
x = 5;
P(&x);
print(x);
} ```

The output of this program is

 A 12 7 6 B 22 12 11 C 14 6 6 D 7 6 6
Programming       C-Programming       GATE 2003
Question 31 Explanation:
In main function x=5; it is global array
p(&x) it goes to P( ) function
y=5
x=5+2=7;
Q(x)
z=7
z=7+5=12(Print+z→I)
comes to P( )
*y=7-1=6
x=7(Print x→II)
comes to main ( ),
print x=*y=6 (print x→III)
Output: 12 7 6
 Question 32

Consider the following C function.

```void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}```

In order to exchange the values of two variables x and y.

 A call swap (x, y) B call swap (&x, &y) C swap (x,y) cannot be used as it does not return any value D swap (x,y) cannot be used as the parameters are passed by value
Programming       C-Programming       GATE 2004
Question 32 Explanation:
Option A:
Here parameters passed by value in C then there is no change in the values.
Option B:
Here values are not swap.
Here parameters are passed by address in C.
Option C:
It is false. Return value is not valid for exchanging the variables.
Option D:
It is correct.
We cannot use swap(x,y) because parameters are passed by value.
Only exchanging the values (or) variables are passing their address and then modify the content with the help of dereferencing operator(*).
 Question 33

Consider the following C function:

```     int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
} ```

The value returned by f(1) is

 A 5 B 6 C 7 D 8
Programming       C-Programming       GATE 2004
Question 33 Explanation: The value return by f(1) = 7
 Question 34

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = d1d2…dm.

```     int n, rev;
rev = 0;
while (n > 0)
{
rev = rev*10 + n%10;
n = n/10;
}```

The loop invariant condition at the end of the ith iteration is:

 A n = d1d2…dm-i and rev = dmdm-1…dm-i+1 B n = dm-i+1…dm-1dm or rev = dm-i…d2d1 C n ≠ rev D n = d1d2…dm and rev = dm…d2d1
Programming       Loop-Invariants       GATE 2004
Question 34 Explanation:
In each iteration the right most digit of n is going to make to the right end of the reverse.
 Question 35

Consider the following C program segment:

```     char p;
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length — i];
printf("%s",p); ```

The output of the program is

 A gnirts B string C gnirt D no output is printed
Programming       C-Programming       GATE 2004
Question 35 Explanation:
Every string is to be end with '\0'.
P = S[7-1] = S = \0.
In P[ ], the first character is '\0'. Then it will results a empty string. If P become '\0', then it doesn't consider about next values in sequence.
 Question 36

It is desired to design an object-oriented employee record system for a company. Each employee has a name, unique id and salary. Employees belong to different categories and their salary is determined by their category. The functions to get Name, getld and compute salary are required. Given the class hierarchy below, possible locations for these functions are:

(i) getld is implemented in the superclass
(ii) getld is implemented in the subclass
(iii) getName is an abstract function in the superclass
(iv) getName is implemented in the superclass
(v) getName is implemented in the subclass
(vi) getSalary is an abstract function in the superclass
(vii) getSalary is implemented in the superclass
(viii) getSalary is implemented in the subclass Choose the best design

 A (i), (iv), (vi), (viii) B (i), (iv), (vii) C (i), (iii), (v), (vi), (viii) D (ii), (v), (viii)
Programming       OOPS       GATE 2004
Question 36 Explanation:
Name and id are a property of every employee and independent of their category. So these should be implemented in superclass. Every employee has a salary but this is determined by their category. So getsalary must be abstract function in superclass and implemented in subclass.
 Question 37

Consider the following C program

```     main()
{   int x, y, m, n;
scanf ("%d %d", &x, &y);
/* Assume x > 0 and y > 0  */
m = x; n = y;
while (m! = n)
{    if (m > n)
m = m - n;
else
n = n - m;
}
print f ("% d", n);
}```

The program computes

 A x + y using repeated subtraction B x mod y using repeated subtraction C the greatest common divisor of x and y D the least common multiple of x and y
Programming       C-Programming       GATE 2004
Question 37 Explanation:
Given code is same as Euclid's Algorithm for finding Greatest Common Divisor(GCD).
 Question 38

Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.

```      Group-1		        Group-2
P. Functional	      1. Command-based, procedural
Q. Logic	      2. Imperative, abstract data type
R. Object-oriented   3. Side-effect free, declarative, expression evaluation
S. Imperative	      4. Declarative, clausal representation, theorem proving ```
 A P - 2, Q - 3, R - 4, S - 1 B P - 4, Q - 3, R - 2, S - 1 C P - 3, Q - 4, R - 1, S - 2 D P - 3, Q - 4, R - 2, S - 1
Programming       Match-the-Following       GATE 2004
Question 38 Explanation:
P) Functional programming is declarative in nature, involves expression evaluation and side effect free.
Q) Logic is also declarative but involves theorem proving.
R) Object oriented is imperative statement based and have abstract data types.
 Question 39

What does the following C-statement declare?

`    int ( * f) (int * ) ; `
 A A function that takes an integer pointer as argument and returns an integer. B A function that takes an integer as argument and returns an integer pointer. C A pointer to a function that takes an integer pointer as argument and returns an integer. D A function that takes an integer pointer as argument and returns a function pointer.
Programming       C-Programming       GATE 2005
Question 39 Explanation:
int ( * f) (int * )
→ A pointer to a function which takes integer as a pointer and return an integer value.
 Question 40

A common property of logic programming languages and functional languages is:

 A both are procedural languages B both are based on λ-calculus C both are declarative D both use Horn-clauses
Question 40 Explanation:
Functional and logic programming languages are also called declarative languages; programs in these languages are said to describe (declaratively) what to do and not (operationally) how to do it.
 Question 41

Which one of the following are essential features of an object-oriented programming language?

(i) Abstraction and encapsulation
(ii) Strictly-typedness
(iii) Type-safe property coupled with sub-type rule
(iv) Polymorphism in the presence of inheritance
 A (i) and (ii) only B (i) and (iv) only C (i), (ii) and (iv) only D (i), (iii) and (iv) only
Question 41 Explanation:
Abstraction, encapsulation and inheritance are three main features of OOP language.
 Question 42

Consider the following C-program:

```void foo(int n, int sum) {
int k = 0, j = 0;
if (n == 0) return;
k = n % 10; j = n / 10;
sum = sum + k;
foo(j, sum);
printf ("%d,", k);
}

int main () {
int a = 2048, sum = 0;
foo(a, sum);
printf ("%d\n", sum);
}```

What does the above program print?

 A 8, 4, 0, 2, 14 B 8, 4, 0, 2, 0 C 2, 0, 4, 8, 14 D 2, 0, 4, 8, 0
Programming       C-Programming       GATE 2005
Question 42 Explanation:
Int a=2048, Sum=0
⇒ foo (a, sum) = foo (2048,0)
⇒ n == 2048
⇒ k = n%10 = 2048%10 = 8
⇒ j = n/10 = 2048/10 = 204
Sum = Sum+k = 0+8 = 8
foo(j, sum) = foo(204, 8)
⇒ n=204
k = n%10 = 204%10 = 4
j = n/10 = 204/10 = 20
sum = sum+k = 12+0 = 12
foo(j, sum) =foo(2,12)
⇒ n=2
k = n%10 = 2%10 = 2
j = n/10 = 2/10 = 0
sum = 14
foo(0,14) ⇒ n==0
printf("%d", k) ⇒ k = 2, 0, 4, 8
After foo( ) statement one more printf statement is there then if print 0 after all digits of n.
2, 0, 4, 8, 0.
 Question 43

Consider the following C-program:

```double foo (double);    /* Line 1 */

int main() {
double da, db;

// input da

db = foo(da);
}

double foo(double a) {
return a;
} ```

The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:

 A no compile warning or error B some compiler-warnings not leading to unintended results C some compiler-warnings due to type-mismatch eventually leading to unintended results D compiler errors
Programming       C-Programming       GATE 2005
Question 43 Explanation:
When a function is called before its declaration then it leads to compiler error.
 Question 44

Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code.

```            subroutine swap(ix,iy)
it = ix
L1 :    ix = iy
L2 :    iy = it
end
ia = 3
ib = 8
call swap (ia, 1b+5)
print *, ia, ib
end ```
```S1: The compiler will generate code to allocate a temporary nameless cell,
initialize it to 13, and pass the address of the cell swap
S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and -2
```

Exactly the following set of statement(s) is correct:

 A S1 and S2 B S1 and S4 C S3 D S1 and S5
Programming       FORTRAN       GATE 2006
Question 44 Explanation:
Swap operation perform between the ia and temporary nameless cell, therefore the value of ib is remains unchanged.
Swap (8, 13)
⇒ ia will returns value with 13.
⇒ ib is unchanged, because here we using pass by reference value.
➝ Temporary nameless is initialized to 13.
➝ There is No runtime error.
 Question 45

Consider this C code to swap two integers and these five statements after it:

```void swap(int *px, int *py) {
*px = *px - *py;
*py = *px + *py;
*px = *py - *px;
}```
```S1: will generate a compilation error
S2: may generate a segmentation fault at runtime depending on the arguments passed
S3: correctly implements the swap procedure for all input pointers referring
to integers  stored in memory locations accessible to the process
S4: implements the swap procedure correctly for some but not all valid input pointers
S5: may add or subtract integers and pointers.
```
 A S1 B S2 and S3 C S2 and S4 D S2 and S5
Programming       C-Programming       GATE 2006
Question 45 Explanation:
S2:
We may get the segmentation fault if the pointer values are constant (i.e., px or py) (or) (px or py) are points to a memory location is invalid.
S4:
Swap procedure can be implemented correctly but not for all input pointers because arithmetic overflow may occur based on input values.
 Question 46

In the following Pascal program segment, what is the value of X after the execution of the program segment?
X:=-10; Y:=20;
If X > Y then if X < 0 then X:=abs(X) else X:=2*X;

 A 10 B -20 C -10 D None
Programming       PASCAL-Programming       GATE 1995
Question 46 Explanation:
X is remains unchanged. As the if condition is becomes false.
X = -10
 Question 47

Which of the following strings can definitely be said to be tokens without looking at the next input character while compiling a Pascal program?

` I. begin           II. program           III. <>   `
 A I B II C III D All of the above
Programming       Pascal Programming       GATE 1995
Question 47 Explanation:
Note: Out of syllabus.
 Question 48

Assume that X and Y are non-zero positive integers. What does the following Pascal program segment do?

``` while X <>Y do
if X > Y then
X := X – Y
else
Y := Y – X;
write(X); ```
 A Computes the LCM of two numbers B Divides the larger number by the smaller number C Computes the GCD of two numbers D None of the above
Programming       PASCAL-Programming       GATE 1995
Question 48 Explanation:
Let X=3 and Y=5
1st pass : X=3 and Y=2
2nd pass : X=1 and Y=2
3rd pass : X=1 and Y=1
Write(X), which writes 1. Which is nothing but GCD of 3 & 5.
 Question 49

What is the value of X printed by the following program?

``` program COMPUTE (input, output);
var
X:integer;
procedure FIND (X:real);
begin
X:=sqrt(X);
end;
begin
X:=2
Find(X)
Writeln(X)
end ```
 A 2 B √2 C Run time error D None of the above
Programming       Programming       GATE 1995
Question 49 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 50

A variant record in Pascal is defined by

```                    type varirec      =   record
number : integer;
case (var1,var2) of
var1: (x,y : integer);
var2: (p.q.: real)
end
end   ```

Suppose an array of 100 records was declared on a machine which uses 4 bytes for an integer and 8 bytes for a real. How much space would the compiler have to reserve for the array?

 A 2800 B 2400 C 2000 D 1200
Programming       PASCAL-Programming       GATE 1995
Question 50 Explanation:
Note: Out of syllabus.
There are 50 questions to complete.

Register Now