ProgrammingHardAccenture · Product company OA
What is printed?
What is printed?
int steps = 0;
int gcd(int a, int b) { steps++; return b == 0 ? a : gcd(b, a % b); }
int main(void) {
int g = gcd(1071, 462);
printf("%d %d", g, steps);
}- A. 21 5
- B. 21 4
- C. 21 3
- D. 7 4
Correct answer: B. 21 4
The calls are (1071, 462) → (462, 147) → (147, 21) → (21, 0), which returns 21. That is 4 calls, including the final one with b = 0; '3' counts only the divisions.
More C Programming questions
- What is printed? c unsigned v = 0; const char b = "10110"; while (b) v = (v << 1) | (b++ - '0'); printf("%u", v);
- With 4-byte int, what is printed? c int a[2][3][4]; printf("%zu %zu %td", sizeof a[1], sizeof a[1][2], (char )&a[1][2][3] - (char )a);
- Assume ASCII. What is printed? c int my(const char a, const char b) { while (a && a == b) { a++; b++; } return a - b; } int main(void) {…
- In what order are the nodes linked after the four calls? c struct N { int v; struct N nx; }; void ins(struct N h, struct N n) { while (h…
- A function in a.c is declared static int helper(void) { ... }. Which statement is true?
- What is printed? c #define MAX(a, b) a > b ? a : b int x = 3, y = 7; printf("%d", 2 MAX(x, y));