GATE | GATE-CS-2015 (Set 2) | Question 48

Consider the C program below.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
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)+ stkFunc(1, 0));
}

chevron_right


The value printed by the above program is ___________
(A) 9
(B) 10
(C) 15
(D) 17


Answer: (C)

Explanation: The code in main, basically initializes a stack of size 10, then pushes 5, then pushes 10.

Finally the printf statement prints sum of two pop operations which is 10 + 5 = 15.



    stkFunc (-1, 10);   // Initialize size as 10
    stkFunc (0, 5);    // push 5
    stkFunc (0, 10);   // push 10

    // print sum of two pop
    printf ("%d\n", stkFunc(1, 0) + stkFunc(1, 0));


Quiz of this Question

My Personal Notes arrow_drop_up