GATE Computer ScienceVery hardGATE CS (PYQ pattern)
int x = 10; void f() { static int x = 1; x += 2; { int x = 100; x++; } printf("%d ", x); } int main() { f(); f(); { extern int x; printf("%d ", x); } for (int x = 0; x < 2; x++) f(); printf("%d", x); } What is printed?
- A. 3 5 10 7 9 2
- B. 3 5 103 7 9 10
- C. 3 3 10 5 7 10
- D. 3 5 10 7 9 10
Correct answer: D. 3 5 10 7 9 10
f's static x is separate from the global: it goes 3, 5, then 7 and 9 in the loop (4 calls total). The inner block's x is local. The extern block sees the global 10, and the loop variable x is gone after the for, so the last print is the global 10.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- unsigned char rev(unsigned char x) { unsigned char r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } return r; }…
- Assume 4-byte int and 8-byte pointers. char s[] = "ab0cd"; char p = "ab0cd"; int a[3][4]; printf("%zu %zu %zu %zu %zu", sizeof(s)…
- For a BST built from distinct keys (deletion of a node with two children uses its inorder successor), consider: I. Inserting a new key and…
- A directed graph has adjacency lists (visited in the order listed): A: B, C; B: D, E; C: E, F; D: G; E: D, H; F: H, B; G: none; H: G, A…
- A DAG on vertices 1..7 has edges 1->2, 1->3, 2->4, 3->4, 3->5, 4->6, 5->6, 7->5. How many distinct topological orderings does it have?
- Union-find on elements 1..12 (initially separate) uses union by size (the root of the larger set becomes the parent; on a tie the root of…