Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following recursive C function.
    void get (int n)
    {
       if (n<1) return;
       get (n–1);
       get (n–3);
       printf(“%d”, n);
    }
    If get (6) function is being called in main () then how many times will the get () function be invoked before returning to the main () ?









  1. View Hint View Answer Discuss in Forum


    Total calls = 25

    Correct Option: B


    Total calls = 25


  1. Consider the following C program.
    #include
    int f1(void);
    int f2(void);
    int f3(void);
    int x = 10;
    int main ()
       {
        int x = 1;
        x + = f1 () + f2 () + f3() + f2();
        pirntf (“%d”, x);
        retirm 0;
      }
    int f1 () {int x = 25; x++; return x;}
    int f2 () {static int x = 50; x++; return x;}
    int f3 () {x *= 10; return x};
    The output of the program is ____.









  1. View Hint View Answer Discuss in Forum

    In function main x will be updated as follows

    x = x + f1 () + f2 () + f3 () + f2 ()x
    1265110052

    Note: static variable in f2 () will be initialized only once & it retains value in between function calls.

    Correct Option: C

    In function main x will be updated as follows

    x = x + f1 () + f2 () + f3 () + f2 ()x
    1265110052

    Note: static variable in f2 () will be initialized only once & it retains value in between function calls.



  1. Consider the following C program :
    #include
    int main ()
        {
       int i, j, k = 0;
       j = 2 * 3/ 4 + 2.0/ 5 + 8/ 5;
       k –= – –j;
       for (i = 0; i < 5: i++)
       {
       switch (i + k)
       {
       case 1 :
       case 2 : printf (“\ n%d”, i+k);
       case 3 : printf (“\n%d”, i+k);
       default: printf (“\n%d”, i+k);
       }
      }
     return 0;
    }
    The number of time printf statement is executed is ______.









  1. View Hint View Answer Discuss in Forum

    j and k will be evaluated to 2 and – 1 respectively. In for loop:
    When i = 0; 1 time printed (– 1)
    When i = 1; 1 time printed (0)
    When i = 2; 3 times printed (1, 1, 1)
    When i = 3; 3 times printed (2, 2, 2)
    When i = 4; 2 times printed (3, 3)
    ∴ on the whole printf is executed 10 times

    Correct Option: A

    j and k will be evaluated to 2 and – 1 respectively. In for loop:
    When i = 0; 1 time printed (– 1)
    When i = 1; 1 time printed (0)
    When i = 2; 3 times printed (1, 1, 1)
    When i = 3; 3 times printed (2, 2, 2)
    When i = 4; 2 times printed (3, 3)
    ∴ on the whole printf is executed 10 times


  1. Consider the following C program segment.
    #include
    int main()
    {
        char sl[7] = “1234”, *p;
       p = sl + 2;
       *p = ‘0’;
       printf (“%s”, s1);
    }
    What will be printed by the program?









  1. View Hint View Answer Discuss in Forum


    After *P = '0', array will become

    and we are pointing string S which is 1204.

    Correct Option: C


    After *P = '0', array will become

    and we are pointing string S which is 1204.



  1. Suppose you are provided with the following function declaration in the C programming language.
    int partition(int a[], int n);
    The function treats the first element of a[] as s pivot, and rearranges the array so that all elements less than or equal to the pivot is in the left part of the array, and all elements greater than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last element of the left part. The return value is the number of elements in the left part. The following partially given function in the C programming language is used to find the kth smallest element in an array a[] of size n using the partition function. We assume k £ n. int kth_smallest(int a[], int n, int k)
    {
    int left_end = partition(a, n);
    if (left_end+1 = =k)
    {
       Return a[left_end];
    }
    if(left_end+1) > k)
    {
       return kth_smallest(________);
    }
    else
    {
    return kth_smallest(________);
    }
    }
    The missing argument lists are respectively









  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    NA