GATE Computer ScienceModerateGATE CS (PYQ pattern)
int f(void) { static int c = 0; return ++c; } int main() { int x = f() || f(); int y = f() && f() > 3 ? f() : -f(); printf("%d %d %d", x, y, f()); } What is printed?
- A. 1 5 6
- B. 1 1 5
- C. 1 -3 4
- D. 1 -4 5
Correct answer: D. 1 -4 5
|| and && evaluate left to right with a sequence point and short-circuit, and ?: evaluates its condition first, so every call order here is fixed. x: the first f() returns 1, so the right side is skipped; x = 1. y parses as (f() && (f() > 3)) ? f() : -f(): f() = 2 is true, f() = 3 > 3 is false, so the else branch runs, y = -f() = -4. The last call returns 5. Calling the skipped f() in x would give 1 5 6; grouping ?: inside && gives 1 1 5.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- In a min-heap stored 1-indexed in an array with children of i at 2i and 2i+1, a node at index 77 is replaced by a smaller key and sifted…
- int a[] = {5, 3, 8, 4, 2, 9, 1, 7, 6, 10}; int st[10], top = -1, nge[10]; for (int i = 0; i < 10; i++) nge[i] = -1; for (int i = 0; i <…
- A min-stack keeps an auxiliary stack: a value is pushed on it when it is less than or equal to the current auxiliary top (or it is empty)…
- for (i = 1; i < 1000; i = 2) for (j = 0; j < i; j++) cnt++; What is the final value of cnt (starting from 0)?
- A table of size 11 uses linear probing with h(k) = k mod 11 and tombstones: a deleted slot is marked DELETED; search continues past…
- The maximum of every window of 3 consecutive elements of [4, 2, 12, 11, -5, 7, 9, 3, 1, 6] is found with a deque of indices: before…