C-Programming

Question 1
What will be the output of the following C program segment?
char inchar = 'A';
switch (inchar)
{
case 'A' :
    printf ("choice A n") ;
case 'B' :
    printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
    printf ("No Choice") ;
}
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
Consider the following C program
int a, b, c = 0;
void prtFun (void);
int main ()
{
    static int a = 1; /* line 1 */
    prtFun();
    a += 1;
    prtFun();
    printf ( "n %d %d " , a, b) ;
}
 
void prtFun (void)
{
    static int a = 2; /* line 2 */
    int b = 1;
    a += ++b;
    printf (" n %d %d " , a, b);
}
What output will be generated by the given code segment?
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
What does the following fragment of C-program print?
char c[] = "GATE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;
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
Consider the following recursive C function that takes two arguments.
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
Consider the following recursive C function that takes two arguments
unsigned int foo(unsigned int n, unsigned int r) {
  if (n  > 0) return (n%r +  foo (n/r, r ));
  else return 0;
}
What is the return value of the function foo when it is called as foo(345, 10) ?
 
A
345
B
12
C
5
D
3
       Programming       C-Programming       Gate 2011
Question 6 Explanation: 

∴ 5+4+3+0 = 12
Question 7
What does the following program print?
#include
void f(int *p, int *q)
{
  p = q;
 *p = 2;
}
int i = 0, j = 1;
int main()
{
  f(&i, &j);
  printf("%d %d n", i, j);
  getchar();
  return 0;
}
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
What is the value printed by the foloowing c program
#include<stdio.h>
int f(int *a, int n)
{
  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);
}
 
int main()
{
  int a[] = {12, 7, 13, 4, 11, 6};
  printf("%d", f(a, 6));
  getchar();
  return 0;
}
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
Consider the program below: # include < stdio .h > int fun(int n, int * f_p) { int t, f; if (n <= 1) { * f_p = 1; return 1; } t = fun (n- 1, f_p); f = t+ * f_p; * f_p = t; return f; } int main() { int x = 15; printf (" % d\ n", fun(5, & x)); return 0; } The value printed is
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
Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression? a = (x > y)? ((x > z)? x : z): ((y > z)? y : z)
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
What is printed by the following C program? int f (int x, int * py, int * *ppz) void main ( )   {                                                               { int y,    z;                                                          int   c,   * b,      * *a; * *ppz  +  = 1;  z = *ppz;                                   c = 4;     b = &c;  a = &b; * py  + = 2;   y = *py;                                       pr int f (" %d", f (c, b, a)); x  + = 3;                                                         } return x + y + z; }
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
Choose the correct option to fill ? 1 and ? 2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline void recerse (void){ int c; if (?1)reverse ( ); ? 2 } main ( ) { pr int f ("Enter Text "); pr int f ("\ n "); reverse ( );pr int f ("\ n "); }
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
What is the output printed by the following C code?
# include <stdio.h>
int main ()
{
    char a [6] = "world";
    int i, j;
    for (i = 0, j = 5; i < j; a [i++] = a [j--]);
    printf ("%sn", a);
}
 /* Add code here. Remove these lines if not writing code */
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
Consider the C program below. What does it print?
# include <stdio.h>
# define swapl (a, b) tmp = a; a = b; b = tmp
void swap2 ( int a, int b)
{
        int tmp;
        tmp = a; a = b; b = tmp;
 }
void swap3 (int*a, int*b)
{
        int tmp;
        tmp = *a; *a = *b; *b = tmp;
}
int main ()
{
        int num1 = 5, num2 = 4, tmp;
        if (num1 < num2) {swap1 (num1, num2);}
        if (num1 < num2) {swap2 (num1 + 1, num2);}
        if (num1 >= num2) {swap3 (&num1, &num2);}
        printf ("%d, %d", num1, num2);
}
 /* Add code here. Remove these lines if not writing code */
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
Consider the C program given below. What does it print?
#include <stdio.h>
int main ()
{
        int i, j;
        int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
        for(i = 0; i < 3; i++) {
             a[i] = a[i] + 1;
             i++;
        }
        i--;
        for (j = 7; j > 4; j--) {
              int i = j/2;
              a[i] = a[i] - 1;
        }
        printf ("%d, %d", i, a[i]);
}
 /* Add code here. Remove these lines if not writing code */
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
C program is given below:
# include <stdio.h>
int main ()
{
        int i, j;
        char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
        char b [3] [2];
        char *p = *b;
        for (i = 0; i < 2; i++) {
              for (j = 0; j < 3; j++) {
              *(p + 2*j + i) = a [i] [j];
              }
        }
}
 /* Add code here. Remove these lines if not writing code */
What should be the contents of the array b at the end of the program?
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
Consider the code fragment written in C below :
void f (int n)
{
  if (n <=1)  {
   printf ("%d", n);
  }
  else {
   f (n/2);
   printf ("%d", n%2);
  }
}
What does f(173) print?
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
Consider the code fragment written in C below :
void f (int n)
{
    if (n <= 1)  {
        printf ("%d", n);
    }
    else {
        f (n/2);
        printf ("%d", n%2);
    }
}
Which of the following implementations will produce the same output for f(173) as the above code? P1
void f (int n)
{
    if (n/2)  {
        f(n/2);
    }
    printf ("%d", n%2);
}
P2
void f (int n)
{
    if (n <=1)  {
        printf ("%d", n);
    }
    else {
        printf ("%d", n%2);
        f (n/2);
    }
}
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
Consider the following C function, what is the output?
#include <stdio.h>
int f(int n)
{
    static int r = 0;
    if (n <= 0) return 1;
    if (n > 3)
    {
        r = n;
        return f(n-2)+2;
    }
    return f(n-1)+r;
}
int main()
{
    printf("%d", f(5));
}
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
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 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
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 22 Explanation: 
int ( * f) (int * )
→ A pointer to a function which takes integer as a pointer and return an integer value.
Question 23
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 23 Explanation: 
When a function is called before its declaration then it leads to compiler error.
Question 24
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
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: 
Let us consider below line inside the for loop p[i] = s[length — i]; For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0’ So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as P[0] will not change for i >0. Nothing is printed if we print a string with first character ‘\0’
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
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 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
Consider the C program shown below.
#include <stdio.h>
#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 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.
PHP Code Snippets Powered By : XYZScripts.com