Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the C program below.
    #include
    int *A, stkTop;
    int stkFunc(int opcode, int val)
    {
    static int size = 0, stkTop = 0;
    switch (opcode)
    {
      case –1: size = val; break;
      case 0: if(stkTop < size) A[stkTop++]=
       val; break;
    default: if (stkTop) return A[– –stkTop];
    }
    return – 1;
    }
    int main()
    {
    int B[20]; A = B; stkTop = –1;
    stkFunc(–1, 10);
    stkFunc(0, 5);
    stkFunc(0, 10);
    printf("%d\n", stkFunc(1,0)+stkFun(1,0);
    }
    The value printed by the above program is ______.
    1. 5
    2. 15
    3. 21
    4. 51
Correct Option: B

The code is pushing 5 and 10 on stack and then popping the top two elements and printing their sum.



Your comments will be displayed only after manual approval.