Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C function :
        int f (int n)
        { static int i = 1;
        if (n > = 5) return n;
        n = n + 1;
         i ++;
        return f(n);
        }
    The value returned by f (1) is









  1. View Hint View Answer Discuss in Forum

    The iterations that the given code will undergo are: From the given conditions we does not take into account when n = 1
    Iteration 1
    N = 1 + 1 = 2 therefore, i = 2
    Iteration 2
    N = 2 + 2 = 4 therefore, i = 3
    Iteration 3
    N = 4 + 3 = 2 = 4 therefore, i = 4
    Hence, the value returned after three iterations is 7
    When, i = 4 and it also fulfill the condition of n>=5

    Correct Option: C

    The iterations that the given code will undergo are: From the given conditions we does not take into account when n = 1
    Iteration 1
    N = 1 + 1 = 2 therefore, i = 2
    Iteration 2
    N = 2 + 2 = 4 therefore, i = 3
    Iteration 3
    N = 4 + 3 = 2 = 4 therefore, i = 4
    Hence, the value returned after three iterations is 7
    When, i = 4 and it also fulfill the condition of n>=5


  1. What does the following algorithm approximate?
    (Assume m > 1, ∈ > 0).
    x = m; y = 1; while (x– y > ∈ )
       {   x = (x + y)/ 2;
        y = m / x;
        }
    print (x);









  1. View Hint View Answer Discuss in Forum

    Here we take let x = 16
    Loop will stop when x – y = 0 or > 0

    Iteration X Y
    1(16 + 1)/ 2 = 8 16/ 8 = 2
    2(8 + 2)/ 2 = 5 16/ 5 = 3
    3(5 + 3)/ 2 = 4 16/ 4 = 4

    Here X = Y
    Then take X which is 4.
    (m)1 / 2 = 4 = (16)1 / 2
    Hence (c) is correct option.

    Correct Option: C

    Here we take let x = 16
    Loop will stop when x – y = 0 or > 0

    Iteration X Y
    1(16 + 1)/ 2 = 8 16/ 8 = 2
    2(8 + 2)/ 2 = 5 16/ 5 = 3
    3(5 + 3)/ 2 = 4 16/ 4 = 4

    Here X = Y
    Then take X which is 4.
    (m)1 / 2 = 4 = (16)1 / 2
    Hence (c) is correct option.



  1. Consider the following C program :
    main ()
       {int x, y, m, n;
       scanf (“%d %d” , &x, &y);
       / * Assume 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);
    }
    The program computes









  1. View Hint View Answer Discuss in Forum

    This is an implementation of Euclid's algorithm to find GCD
    Here if m > n, then m = m – n
    m < n, then n = n – m
    Let take X = 24, Y = 9
    Then m = 24, n = 9

    iteration m n
    124 – 9 = 15 9
    215 – 9 = 6 9
    36 9 – 6 = 3
    46 – 3 = 3 3

    Here m = n, so n returned
    Which is GCD (Greatest common divisor) of X&Y
    Hence (c) is correct option.

    Correct Option: C

    This is an implementation of Euclid's algorithm to find GCD
    Here if m > n, then m = m – n
    m < n, then n = n – m
    Let take X = 24, Y = 9
    Then m = 24, n = 9

    iteration m n
    124 – 9 = 15 9
    215 – 9 = 6 9
    36 9 – 6 = 3
    46 – 3 = 3 3

    Here m = n, so n returned
    Which is GCD (Greatest common divisor) of X&Y
    Hence (c) is correct option.


  1. Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = d1 d2... dm .
       int n, rev;
       rev = 0;
       while (n > 0) {
         rev = rev * 10 + n% 10;
         n = n/ 10;
       }
    The loop invariant condition at the end of the ith iteration is









  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    NA



  1. 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









  1. View Hint View Answer Discuss in Forum

    When a function is called without being defined, C. Compiler assumes if to reterm “ Int” but here (800) is returning “ double” and hence the compiler will throw type mis-match error. So it is a compiler errors.

    Correct Option: D

    When a function is called without being defined, C. Compiler assumes if to reterm “ Int” but here (800) is returning “ double” and hence the compiler will throw type mis-match error. So it is a compiler errors.