VOOZH about

URL: https://www.geeksforgeeks.org/c/c-language-set-2/

⇱ C Language | Set 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Language | Set 2

Last Updated : 23 Jul, 2025
Following questions have been asked in GATE CS exam. 1. Consider the following C program segment: 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 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: 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 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
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
Comment