Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

  1. The output of the following C program is ______.
    void f1(int a, int b)
    {
       int c;
    c=a; a=b; b=c;
    }
    void f2(int *a, int *b)
    {
       int c;
       c=*a; *a=*b; *b=c;
    }
    int main ()
    {
       int a=4, b=5, c=6;
       f1(a, b);
       f2(&b, &c);
       printf(“%d”, c–a–b);
    }









  1. View Hint View Answer Discuss in Forum

    In function "main ()"
    f1 is called by value, so local variables a, b, c of f1 are customized but not the local variables a, b, c of main function.
    f2 is called by reference.
    int main () {
    int a = 4, b = 5, c = 6
    f1(a, b)
    f2(&b, &c)
    printf ("%d", c-a-b);
    }
    f2(int *a, int *b)
    {
    int c;
    c = * a; c 5
    * a = b; [will change 'b' value of main to c value of main]
    *b = c; [will change 'c' value of main to c value of f2]
    }

    Correct Option: A

    In function "main ()"
    f1 is called by value, so local variables a, b, c of f1 are customized but not the local variables a, b, c of main function.
    f2 is called by reference.
    int main () {
    int a = 4, b = 5, c = 6
    f1(a, b)
    f2(&b, &c)
    printf ("%d", c-a-b);
    }
    f2(int *a, int *b)
    {
    int c;
    c = * a; c 5
    * a = b; [will change 'b' value of main to c value of main]
    *b = c; [will change 'c' value of main to c value of f2]
    }


  1. Consider the following pseudo code, where x and y are positive integers.
    begin
    q: = 0
    r: = x
    while r > y do
       being
        r: = r – y
        q: = q + 1
       end
    end
    The post condition that needs to be satisfied after the program terminates is









  1. View Hint View Answer Discuss in Forum

    The loop terminater when r < y. so, r < y is one post condition.
    In each iteration q is incremented by 1 and y is subtracted from r. Initialvalue of r is x.

    So, loop iterates
    x
    times and q will be equal to
    x
    yy

    and r = x% y ⇒ x = qy + r
    So, (b) is correct answer.

    Correct Option: B

    The loop terminater when r < y. so, r < y is one post condition.
    In each iteration q is incremented by 1 and y is subtracted from r. Initialvalue of r is x.

    So, loop iterates
    x
    times and q will be equal to
    x
    yy

    and r = x% y ⇒ x = qy + r
    So, (b) is correct answer.



  1. What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory ?
    int main()
    {
    unsigened int x[4] [3] = {(1,2,3), {4,5,6}, {7, 8, 9}, {10, 11, 12}};
    printf(“%u, %u, %u”, x+3, *(x+3),
        *(x+2)+3);
    }









  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    NA


  1. A variable x is said to be live at a statement Si in a program if the following three conditions hold simultaneously:
    I. There exists a statement Sj that uses x
    II. There is a path from Si to Sj in the flow graph corresponding to the program
    III. The path has no intervening assignment to x including at Si and Sj

    The variables which are live at the statement in basic block 2 and at the statement in basic block 3 of the above control flow graph are









  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    NA



  1. Consider the following C function.
    int fun(int n)
    {
        int x = 1, k;
       if (n = = 1) return x;
       for (k =1; k   x = x + fun(k) * fun(n–k);
       return x;
    }
    The return value of fun(5) is _____.









  1. View Hint View Answer Discuss in Forum

    Recurrence Relation is
    f(n) = 1, if n = 1

    Correct Option: C

    Recurrence Relation is
    f(n) = 1, if n = 1