Programming GATE 2018 Solved Questions

Ques 1: Consider the following C program.

GATE 2018 Q no 2

#include <stdio.h>
struct Ournode
{
  char x,y,z;
};
int main()
{
 struct Ournode p = {'1', '0', 'a'+2}; 
 struct Ournode *q = &p; 
 printf ("%c, %c", *((char*)q+1), *((char*)q+2));
 return 0; 
}

The output of this program is:

(A) 0, c

(B) 0, a+2

(C) ‘0’, ‘a+2’

(D) ‘0’, ‘c’

Ans: (A) 0, c

Solution: ‘a’ + 2 will be ‘c’,

so Ournode p = {‘1’, ‘0’, ‘c’}

and output will be 0, c

Ques 2: Consider the following C program:

GATE 2018 Q no 21

#include<stdio.h>
int counter=0;
int calc (int a, int b) {
        int c;
        counter++;
        if(b==3) return (a*a*a);
        else {
                c = calc(a, b/3);
                return (c*c*c);
        }
}
int main() 
{
        calc(4, 81);
        printf("%d", counter);
}

Output of this program is_______

Ans: 4

Solution: Each function increments counter value by 1.

Goal is to find the number of function calls.

Sequence of function calls:

calc(4, 81) —> calc(4, 27) —> calc(4, 9) —> calc(4, 3) —> return

4 function calls.

Ques 3: Consider the following C program:

GATE 2018 Q no 29

#include <stdio.h>
void fun1(char *s1, char *s2)
{
 char *tmp; 
 tmp = s1; 
 s1 = s2; 
 s2 = tmp;
} 
void fun2(char **s1, char **s2)
{ 
 char *tmp; 
 tmp = *s1; 
 *s1 = *s2; 
 *s2 = tmp; 
} 
int main()
{ 
 char *str1 = "Hi", *str2 = "Bye"; 
 fun1(str1, str2); 
 printf("%s %s ", str1, str2); 
 fun2(&str1, &str2); 
 printf("%s %s", str1, str2); 
 return 0; 
} 

The output of the program above is

(A) Hi Bye Bye Hi

(B) Hi Bye Hi Bye

(C) Bye Hi Hi Bye

(D) Bye Hi Bye Hi

Ans: (A) Hi Bye Bye Hi

Solution: In call to first function, call by value method is used. So contents of str1 and str2 are not changed in calling function i.e. main()

In call to second function, call by reference method is used. So contents of str1 and str2 are changed in calling function i.e. main()

Ques 4:Consider the following C code. Assume that unsigned long int type length is 64 bits.

GATE 2018 Q no 32

unsigned long int fun(unsigned long int n)
{
 unsigned long int i, j = 0, sum = 0;
 for (i = n; i > 1; i = i/2) 
 j++;
 for ( ; j > 1; j = j/2) 
 sum++;
 return(sum);
}

The value returned when we call fun with the input 240 is

(A) 4

(B) 5

(C) 6

(D) 40

Ans: (B) 5

Solution: First for loop will make j=40

Next for loop will divide j value (which is 40now) by 2 each time until j≤1

j loop starts:

j = 40 and sum =1

j = 20 and sum=2

j = 10 and sum=3

j = 5 and sum=4

j = 2 and sum=5

j=1 break

So, Sum=5

Ques 5:Consider the following program written in pseudo-code. Assume that x and y are integers.

GATE 2018 Q no 45

Count(x,y) {
 if (y != 1){
 if (x != 1) {
 print("*");
 Count(x/2, y);
 }
 else {
 y = y-1;
 Count(1024, y);
 }
 }
}

The number of times that the print statement is executed by the call Count(1024,1024) is _

Ans: 10230

Solution : Here, for each y value print(“∗”) will run 10 times.

Once x value reaches 1, count(1024,y−1) will be called.

Variable y can take values [2  1024] i.e. total 1023 values. So,

The total number of times ‘∗’ will be printed =  ((Number of times ‘∗’ printed per y value))*((number of values y takes)).

Number of times ‘∗’ printed =10∗1023=10230

error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.