Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

  1. What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?
    a = 3;
    void n(x) {x = x * a; print(x);}
    void m(y) {a = 1; a = y – a; n(a); print(a);}
    void main() {m(a);}









  1. View Hint View Answer Discuss in Forum

    Dynamic scoping refers to definition of free variable in the reverse order of calling sequence.

    Correct Option: D

    Dynamic scoping refers to definition of free variable in the reverse order of calling sequence.


  1. What will be the output of the following C program ?
       void count (int n){
       static int d=1;

       print f (“%d”, n);
       print f (“%d”, d);
       d++;
       if (n>1) count (n – 1);
       print f (“%d”, d);
    }
      void main( ){
        count(3);
    }









  1. View Hint View Answer Discuss in Forum


    Output will be 312213444.

    Correct Option: A


    Output will be 312213444.



  1. The following function computes the maximum value contained in an integer array p[] of size n (n >= 1).
        int max (int *p, int n){
       int a = 0, b = n –1;
       while (__________){
       if (p[a]<= p[b]){a = a + 1;}
       else {b = b – 1;}
       }
       return p[a];
       }
    The missing loop condition is









  1. View Hint View Answer Discuss in Forum

    P[a] will have the maximum value of the array, when a = b.

    Correct Option: D

    P[a] will have the maximum value of the array, when a = b.


  1. Consider the following C program:
    #include
    void mystery (int *ptra, int *ptrb){
        int *temp;
       temp = ptrb;
       ptrb = ptra;
       ptra = temp;
    }
    int main() {
       int a=2016, b=0, c=4, d=42;
       mystery (&a, &b);
       if (a < c)
       mystery (&c, &a);
       mystery (&a, &d);
       printf (“%d\n”, a);
    }
    The output of the program is_______.









  1. View Hint View Answer Discuss in Forum

    As in C, parameters are passed by value - even if they are pointers. So, here the pointer values are exchanged within the function only (one can use * operator to exchange the values at the location of the pointers and this will affect the values in main). So, there will be no change in a, b, c, d. The output of the program is 2016.

    Correct Option: A

    As in C, parameters are passed by value - even if they are pointers. So, here the pointer values are exchanged within the function only (one can use * operator to exchange the values at the location of the pointers and this will affect the values in main). So, there will be no change in a, b, c, d. The output of the program is 2016.



  1. Consider the C functions foo and bar given below :
    int foo (int val) {
       int x = 0;
       while (val > 0) {
        x = x + foo (val--);
      }
      return val;
    }
    int bar (int val) {
       int x = 0;
       while (val > 0) {
         x = x + bar (val–l);
    }
      return val;
    }
    Invocations of foo (3) and bar (3) will result in:









  1. View Hint View Answer Discuss in Forum

    In the given program, invocation of foo (3) and bar (3), in which, foo (3) turn calls foo (3). This goes on infinite number of times which causes memory overflow and causes abnormal termination.
    bar (3) → bar (2) → bar (1) → bar (0) and (return 0) from here onwards bar (1) will bar (0) and bar (0) will return 0 to bar (1) and this goes on forever without causing memory overflow but it goes on infinite loop execution.
    Hence, option (c) is correct.

    Correct Option: C

    In the given program, invocation of foo (3) and bar (3), in which, foo (3) turn calls foo (3). This goes on infinite number of times which causes memory overflow and causes abnormal termination.
    bar (3) → bar (2) → bar (1) → bar (0) and (return 0) from here onwards bar (1) will bar (0) and bar (0) will return 0 to bar (1) and this goes on forever without causing memory overflow but it goes on infinite loop execution.
    Hence, option (c) is correct.