C-Programming |Gate-2018|
1. Consider the following C program:
#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) ;
}
The output of this program is ______. [GATE – 2018]
a. 4
b. 5
c. 6
d. 7
Answer : a)
2. Consider the following C program.
#include <stdio.h>
struct Outnode {
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 : [GATE – 2018]
a. ‘0’, ‘a+2’
b. 0, a+2
c. 0, c
d. ‘0’, ‘c’
Answer : c)
C-Programming |Gate-2018|
3. Consider the following C program:
#include<stdio.h>
void fun1(char *s1, char *s2) {
char *tmp;
tmp = s1;
s1 = s2;
s2 = tmp;
}
void fun2(char **s2, 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 : [GATE – 2018]
a. Hi Bye Hi Bye
b. Bye Hi Bye Hi
c. Bye Hi Hi Bye
d. Hi Bye Bye Hi
Answer : d)
4. Consider the following C code. Assume that unsigned long int type length is 64 bits.
unsigned long int fun(unsigned long int n) {
unsigned long int i, j, 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 : [GATE – 2018]
a. 4
b. 5
c. 6
d. 40
Answer : b)
C-Programming |Gate-2018|
5. Consider the following program written in pseudo-code. Assume that x and y are integers.
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 ______. [GATE – 2018]
a. 10230
b. 10231
c. 10232
d. 10233
Answer : a)
OS – GATE Previous Year Questions
DS – GATE Previous Year Questions