Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

Direction: Consider the following recursive C function that takes two arguments unsigned into foo (unsigned int, n, unsigned int r) { if n > 0 return n% foo (n/r, r);
else return 0,
}

  1. What is the return value of the function foo, when it is called as foo (345, 10)?









  1. View Hint View Answer Discuss in Forum


    5 + 4 + 3 = 12

    Correct Option: B


    5 + 4 + 3 = 12


  1. What is the return value of the function foo, when it is called as foo (513, 2)?









  1. View Hint View Answer Discuss in Forum


    1 + 1 = 2

    Correct Option: D


    1 + 1 = 2



  1. What does the following fragment of C-program print?
    char c[] = “GATE 2011”
    char * p = c;
    printf (“%s”, p + p[3] – p[1];









  1. View Hint View Answer Discuss in Forum

    2011 charc[] = "GATE2011";
    p now has the base address string "GATE2011"
    char*p =c;
    p[3] is 'E' and p[1] is 'A'.
    p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4
    So the expression p + p[3] - p[1] becomes p + 4 which is base address of string "2011

    Correct Option: C

    2011 charc[] = "GATE2011";
    p now has the base address string "GATE2011"
    char*p =c;
    p[3] is 'E' and p[1] is 'A'.
    p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4
    So the expression p + p[3] - p[1] becomes p + 4 which is base address of string "2011


  1. What does the following program print?
    #include < stdio.h >
    void f (int *p, int *q)
       p = q;
    *p = 2;
    }
    int i = 0, j = 1;
    int main () {
       f(&i, &j);
       printf(“%d%d/n”, i, j);
    }









  1. View Hint View Answer Discuss in Forum

    * p points to i and q points to j */
    void f(int *p, int *q)
    {
    p = q; /* p also points to j now */
    *p = 2; /* Value of j is changed to 2 now */
    }

    Correct Option: D

    * p points to i and q points to j */
    void f(int *p, int *q)
    {
    p = q; /* p also points to j now */
    *p = 2; /* Value of j is changed to 2 now */
    }



  1. What will be the output of the following C program segment?
    char inChar = ‘A’;
    switch (inChar) {
    case ‘A’ : prinf(“Choice A\n”);
    case ‘B’ :
    case ‘C’ : printf (“Choice B’):
    case ‘D’ :
    case ‘E’ :
    default: printf (“No Choice”);}









  1. View Hint View Answer Discuss in Forum

    In switch case statements, there can be more cases, which case satisfied the condition will be executed, so we have to add break statement after every case in switch. If there is no break statement then all switch cases will be executed and default case will also be executed.
    In the given program, there is no error so, case A is executed then case B and then case C and then case D and E and finally the default case will be executed and print.

    Correct Option: C

    In switch case statements, there can be more cases, which case satisfied the condition will be executed, so we have to add break statement after every case in switch. If there is no break statement then all switch cases will be executed and default case will also be executed.
    In the given program, there is no error so, case A is executed then case B and then case C and then case D and E and finally the default case will be executed and print.