Following questions have been asked in GATE CS exam.

**1. Consider the following C program segment:**

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

*chevron_right*

*filter_none*

**The output of the program is (GATE CS 2004)**

a) gnirts

b) gnirt

c) string

d) no output is printed

Answer(d)

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’

2. Consider the following C function

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

*chevron_right*

*filter_none*

**In order to exchange the values of two variables x and y. (GATE CS 2004)**

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

Answer(d)

Why a, b and c are incorrect?

a) call swap (x, y) will not cause any effect on x and y as parameters are passed by value.

b) call swap (&x, &y) will no work as function swap() expects values not addresses (or pointers).

c) swap (x, y) cannot be used but reason given is not correct.

**3. 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); ` `} ` |

*chevron_right*

*filter_none*

**The value returned by f(1) is (GATE CS 2004)**

a) 5

b) 6

c) 7

d) 8

Answer (c)

Since i is static, first line of f() is executed only once.

Execution of f(1) i = 1 n = 2 i = 2 Call f(2) i = 2 n = 4 i = 3 Call f(4) i = 3 n = 7 i = 4 Call f(7) since n >= 5 return n(7)

4. 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; ` `} ` |

*chevron_right*

*filter_none*

**The loop invariant condition at the end of the ith iteration is:(GATE CS 2004)**

a) n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1

b) n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1

c) n ≠ rev

d) n = D1D2….Dm and rev = DmDm-1…D2D1

Answer (a)

5. Consider the following C program

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

*chevron_right*

*filter_none*

**The program computes (GATE CS 2004)**

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

Answer(c)

This is an implementation of Euclid’s algorithm to find GCD

## Recommended Posts:

- C Language | Set 1
- C Language | Set 10
- C Language | Set 6
- C Language | Set 4
- C Language | Set 7
- C Language | Set 8
- C Language | Set 9
- C Language | Set 3
- C Language | Set 5
- DFA for accepting the language L = { a
^{n}b^{m}| n+m=even } - DFA for accepting the language L = {a
^{n}b^{m}| n+m=odd} - NPDA for accepting the language L = {a
^{m}b^{(m+n)}c^{n}| m,n ≥ 1} - NPDA for accepting the language L = {a
^{m}b^{(2m+1)}| m ≥ 1} - Construct a DFA which accept the language L = {a
^{n}b^{m}| n > =1, (m) mod 3 = 1} - NPDA for accepting the language L = {a
^{m}b^{n}c^{(m+n)}| m,n ≥ 1} - NPDA for accepting the language L = {a
^{n}b^{n}c^{m}| m,n>=1} - NPDA for accepting the language L = {a
^{m}b^{n}c^{p}d^{q}| m+n=p+q ; m,n,p,q>=1} - NPDA for accepting the language L = {a
^{m}b^{(2m)}| m>=1} - NPDA for accepting the language L = {a
^{n}b^{n}| n>=1} - NPDA for accepting the language L = {a
^{(m+n)}b^{m}c^{n}| m,n ≥ 1}