ProgrammingHardProduct company OA

What is printed?

What is printed?
int st[10], top = -1;
#define PUSH(x) (st[++top] = (x))
#define POP() (st[top--])
int main(void) {
    PUSH(3); PUSH(5);
    int x = POP(); int y = POP();
    PUSH(x * y); PUSH(2);
    int a = POP(); int b = POP();
    printf("%d %d %d", a, b, top);
}
  1. A. 2 15 -1
  2. B. 15 2 -1
  3. C. 2 8 -1
  4. D. 2 15 0

Correct answer: A. 2 15 -1

Pops return values in reverse order of pushes. After popping 5 and 3, 15 and then 2 are pushed; popping gives 2 first, then 15. Four pushes and four pops bring top back to −1. Keeping each POP in its own statement avoids unsequenced changes to top.

More C Programming questions

Exams that ask this topic