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);
}- A. 2 15 -1
- B. 15 2 -1
- C. 2 8 -1
- 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
- A binary tree is stored with child indices (−1 for none). What does h(0) return? c int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1}…
- With the tree below, what does in(0) print? c char key[] = "ABCDE"; int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1}; void in(int n)…
- What is wrong with this code? c char s = malloc(5); strcpy(s, "hello");
- With 4-byte int, what is printed? c int a[3] = {7, 8, 9}; int (p)[3] = &a; printf("%d %d %zu", (p)[1], p + 1, sizeof p);
- What is printed? c int c = 0, calls = 0; void comb(int start, int n, int k) { calls++; if (k == 0) { c++; return; } for (int i = start; i…
- What is printed? c int x = 3, y = 1; printf("%d %d", !x == y, !(x == y));