C Language | Set 2
Following questions have been asked in GATE CS exam.
1. Consider the following C program segment:
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.
The output of the program is (GATE CS 2004)
d) no output is printed
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 is ‘\0’
So p becomes ‘\0’. It doesn’t matter what comes in p, p….. as P will not change for i >0. Nothing is printed if we print a string with first character ‘\0’
2. Consider the following C function
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
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:
The value returned by f(1) is (GATE CS 2004)
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
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
5. Consider the following C program
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
This is an implementation of Euclid’s algorithm to find GCD