C-Programming

Question 1
A
No Choice
B
Choice A
C
D
Program gives no output as it is erroneous
       Programming       C-Programming       Gate 2012
Question 1 Explanation: 
Everything in the switch will be executed, because there is no break; statement after case ‘A’. So it executes all the subsequent statements until it find a break;
So,
→ Choice A
→ Choice B. No choice. Is the output.
Question 2
A
B
C
D
       Programming       C-Programming       Gate 2012
Question 2 Explanation: 

Hence
4 2
6 2
2 0
Question 3
A
B
C
D
       Programming       C-Programming       Gate 2012
Question 3 Explanation: 
Line 1 replaced by auto int a=1;
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.
Question 4
 
A
GATE2011
B
E2011
C
2011
D
011
       Programming       C-Programming       Gate 2011
Question 4 Explanation: 
p is the starting address of array.
p[3] - p[1] = 4, and p+4 will be pointing to the fifth position in the array 'c'. So printf starts printing from 2 and prints 2011.
Question 5
 
A
9
B
8
C
5
D
2
       Programming       C-Programming       Gate 2011
Question 5 Explanation: 

∴ 1+0+0+0+0+0+0+0+0+1+0 = 2
Question 6
   
A
345
B
12
C
5
D
3
       Programming       C-Programming       Gate 2011
Question 6 Explanation: 

∴ 5+4+3+0 = 12
Question 7
 
A
2 2
B
2 1
C
0 1
D
0 2
       Programming       C-Programming       2010
Question 7 Explanation: 

Both pointers points to j = 1
now *p = 2
where j is updated with value 2.
printf (i, j) i = 0, j = 2
Question 8
A
-9
B
5
C
15
D
19
       Programming       C-Programming       2010
Question 8 Explanation: 
int a[ ] = {12, 7, 13, 4, 11, 6}
if (n <= 0)
return 0;
else if (*a % 2 = = 0)
return *a + f(a+1, n-1);
else
return *a – f(a+1, n-1);


⇒12+(7-(13-(4+(11-(6)))))
⇒12+(7-(13-(4+5)))
⇒12+7-(4)
⇒12+3
⇒15
Question 9
A
6
B
8
C
14
D
15
       Programming       C-Programming       2009
Question 9 Explanation: 
int x=15;
printf(fun(5,&x));
The code is implemented using Tail Recursion.
fun(5,15)

fun(4,15)

fun(3,15)

fun(2,15)

fun(1,15)
→ First we will trace fun(1,15) which returns 1.
→ Then trace fun(2,15) using fun(1,15) value, it returns 2.
→ Then fun(3,15), it returns 3≃(1+2)
→ Then fun(4,15), it returns 5=(2+3)
→ Then fun(5,15), it returns 8=(3+5)
If you call fun(6,15) then it will return 13=(5+8)
Here fun(n,*x)≃fun(n-1,&x)+fun(n-2,&x), where fun(n-1,&x) is storing in variable ‘t’ & fun(n-2,&x) is storing in variable x(*f-p).
∴ The program is nth Fibonacci number.
Question 10
 
A
x = 3, y = 4, z = 2
B
x = 6, y = 5, z = 3
C
x = 6, y = 3, z = 5
D
x = 5, y = 4, z = 5
       Programming       C-Programming       Gate-2008
Question 10 Explanation: 
Required final output value of a=4.
→ We can directly eliminate the options B & C, because none of the variable can assign a value 4.
→ Given explanation is
a=(x>y)?((x>z)?x:z):((y>z)?y:z)
Option A:
x=3; y=4; z=2
a=(3>4)?⇒No
Then evaluate second expression⇒(4>2)?Yes
⇒a=y
a=4 (True)
Option D:
x=5; y=4; z=5
a=(5>4)⇒Yes
Then evaluate first expression ⇒ (5>5)? No
⇒ a=z ⇒ a=5 (Not true)
⇒Answer is Option A.
Question 11
A
18
B
19
C
21
D
22
       Programming       C-Programming       Gate-2008
Question 11 Explanation: 
f(c,b,a)
f(c,&c,&(&c))=f(4,4,4)
c is 4, b is a pointer pointing address of a, a is a pointer to pointer of c. Hence both b and c are pointing to same memory address i.e., a.
Hence whatever increment operation happens in f, it happens/ reflects on same value i.e., a.
**ppz+=1;
z=**ppz; //z=5
These steps update it to 5 and stored in z.
*py+=2; //changes c to 7, x is unchanged.
y=*py; //y=7
It updates to 7 and stored in y.
x+=3 //x is incremented by 3.
returns x+y+z=7+7+5=19
Question 12
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       C-Programming       Gate-2008
Question 12 Explanation: 
void reverse(void)
{
int c;
if(?1) reverse( );
?2
}
main( )
{
printf(“Enter Text”);
printf(“\n”);
reverse( );
printf(“\n”);
}
We can simply eliminate A & B for ?2.
& Hence
?1 is ((c=getchar( )) != ‘\n’)
?2 is putchar(c);
Question 13
A
1661 and 1640
B
59 and 59
C
1640 and 1640
D
1640 and 1661
       Programming       C-Programming       Gate-2008
Question 13 Explanation: 
Both functions perform same operation, so output is same, means either (B) or (C) is correct.
f1(2) = 2*f1(1) + 3*f1(0) = 2
f1(3) = 2*f1(2) + 3*f1(1) = 2*2 + 3*1 = 7
f1(4) = 2*f1(3) + 3*f1(2) = 2*7 + 3*2 = 20
f1(5) = 2*f1(4) + 3*f1(3) = 2*20 + 3*7 = 40 + 21 = 61
We can skip after this as the only remaining choice is (C).
f1(6) = 2*f1(5) + 3*f1(4) = 2*61 + 3*20 = 122 + 60 = 182
f1(7) = 2*f1(6) + 3*f1(5) = 2*182 + 3*61 = 364 + 183 = 547
f1(8) = 2*f1(7) + 3*f1(6) = 2*547 + 3*182 = 1094 + 546 = 1640
Question 14
A
dlrow
B
Null String
C
dlrld
D
worow
       Programming       C-Programming       Gate 2008-IT
Question 14 Explanation: 
As the base address or starting of the string "Null" is placed. So while reading array if Null comes it assumes that this is the end of array. So, it terminates here only.
Question 15
A
5, 5
B
5, 4
C
4, 5
D
4, 4
       Programming       C-Programming       Gate 2008-IT
Question 15 Explanation: 
"If (num1 ≥ num2)
{Swap3 (&num1, &num2) ; }"
Statement is true, so call by reference will be performed and the value of num1 and num2 will get exchanged.
Question 16
 
A
2, 3
B
2, 4
C
3, 2
D
3, 3
       Programming       C-Programming       Gate 2008-IT
Question 16 Explanation: 
Note that there are two variables named 'i', with different scope.
First for loop will run for i = 0, 2 and 4 as i is incremented twice and resultant array will be 'a' = 2, 2, 4, 4, 5, 6, 7, 8. Loop will terminate at i = 4.
After that value of 'i' will become '3' as there is a decrement operation after for loop.
Next for loop is running for j = 7, 6, 5 and corresponding 'i' values which is a local variable inside for loop will be
3(7/2), 3(6/2), 2(5/2)
Array after this for loop will be
a = 2, 2, 3, 2, 5, 6, 2, 8.
After the loop, current 'i' value is 3 and elements at a[3] = 2.
Question 17
 
A
B
C
D
       Programming       C-Programming       Gate 2008-IT
Question 17 Explanation: 
*p = a[0][0] = a
*(p+2) = a[0][1] = b
*(p+4) = a[0][2] = c
*(p+1) = a[1][0] = d
*(p+3) = a[1][1] = e
*(p+5) = a[1][2] = f
Question 18
 
A
010110101
B
010101101
C
10110101
D
10101101
       Programming       C-Programming       Gate 2008-IT
Question 18 Explanation: 

So, after traversing the tree we get:
10101101
Question 19
A
Both P1 and P2
B
P2 only
C
P1 only
D
Neither P1 nor P2
       Programming       C-Programming       Gate 2008-IT
Question 19 Explanation: 
P1 prints same as the original program.
But P2 prints the reverse of original sequence printed by original program.
Question 20
A
5
B
7
C
9
D
18
       Programming       C-Programming       Gate-2007
Question 20 Explanation: 
We need to evaluate f(5)
f(5) = f(3) + 2
f(3) = f(2) + 5 (where r is static and value of r=5)
f(2) = f(1) + 5
f(1) = f(0) + 5
f(0) = 1
⟹ f(5) = 1+5+5+5+2 = 18
Question 21
 
A
S1
B
S2 and S3
C
S2 and S4
D
S2 and S5
       Programming       C-Programming       Gate-2006
Question 21 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 22
 
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 22 Explanation: 
int ( * f) (int * )
→ A pointer to a function which takes integer as a pointer and return an integer value.
Question 23
 
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 23 Explanation: 
When a function is called before its declaration then it leads to compiler error.
Question 24
 
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 24 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 25
 
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 25 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 26
 
A
5
B
6
C
7
D
8
       Programming       C-Programming       Gate-2004
Question 26 Explanation: 

The value return by f(1) = 7
Question 27
 
A
gnirts
B
string
C
gnirt
D
no output is printed
       Programming       C-Programming       Gate-2004
Question 27 Explanation: 
Every string is to be end with '\0'.
P[0] = S[7-1] = S[6] = \0.
In P[ ], the first character is '\0'. Then it will results a empty string. If P[0] become '\0', then it doesn't consider about next values in sequence.
Question 28
 
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 28 Explanation: 
Given code is same as Euclid's Algorithm for finding Greatest Common Divisor(GCD).
Question 29
 
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 29 Explanation: 
Take n=4, so Two log_n=4
Now Trace the code,
for (k=3; k<=n; k++)
A[k]=0; // A[3]=0
A[4]=0
for (k=2; k<=Two log_n; k++)
for(j=k+1; j<=n; j++)
A[j] = A[j] // (j%k); // A[3] = 0 // I=1
A[4] = 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 30
 
A
12 7 6
B
22 12 11
C
14 6 6
D
7 6 6
       Programming       C-Programming       Gate-2003
Question 30 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 31
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 31 Explanation: 
In C Language a function can create activation record from the created function stack.
Question 32
 
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 32 Explanation: 
The given code represents an array of s[ ], in this each element is a pointer to structure of type node.
Question 33
 
A
22 bytes
B
14 bytes
C
18 bytes
D
10 bytes
       Programming       C-Programming       Gate-2000
Question 33 Explanation: 
short [5] = 5×2 = 10
max[float, long] = max [4, 8] = 8
Total = short[5] + max[float,long] = 10 + 8 = 18
Question 34
 
A
10
B
4
C
6
D
7
       Programming       C-Programming       Gate-2000
Question 34 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 35
 
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 35 Explanation: 
Try for (3,2,2), it will go for infinite loop.
Question 36
 
A
Theory Explanation is given below.
       Programming       C-Programming       Gate-2001
There are 36 questions to complete.