GATE Papers >> CSE >> 2015 >> Question No 152

Question No. 152 CSE | GATE 2015

Consider the C program below.
#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)+stkFun(1,0);
}

The value printed by the above program is ______.


Answer : 15


Solution of Question No 152 of GATE 2015 CSE Paper

StkFunc (-1, 10);  ⇒  size = 10; stkTop = 0 [case - 1 executed]

StkFunc (0, 5);  ⇒  A[0] sets to 5 and stkTop = 1 [case 0 executed]

StkFunc (0, 10);  ⇒  A[1] sets to 10 and stkTop = 2 [case 0 executed]

StkFunc (1, 0);  ⇒  returns 10, stkTop = 1 [deafult case executed]

StkFunc (1, 0);  ⇒  returns 5, stkTop = 0 [default case executed]

$\therefore$  Program prints 10 + 5 = 15

Comments
No Comments
Leave a comment