ProgrammingHardProduct company OA
What is printed?
What is printed?
int steps(int n) { return n == 1 ? 0 : 1 + steps(n % 2 ? 3 * n + 1 : n / 2); }
int main(void) { printf("%d %d", steps(6), steps(7)); }- A. 9 17
- B. 8 16
- C. 6 16
- D. 8 17
Correct answer: B. 8 16
steps counts Collatz moves until 1. From 6: 3, 10, 5, 16, 8, 4, 2, 1, which is 8 moves. From 7: 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, which is 16. '9 17' counts the numbers visited instead of the moves.
More C Programming questions
- Assume ASCII. What does f("Code21") print? c void f(const char s) { if (s) { f(s + 1); if (s % 2) putchar(s); } }
- What is printed? c int a = 1, b = 2, c = 3; int arr[] = {&a, &b, &c}; int pp = arr + 1; pp += 10; pp[1] += 20; printf("%d %d %d", a, b, c);
- Assume 32-bit unsigned int. What is printed? c unsigned mask(int lo, int hi) { return ((1u << (hi - lo + 1)) - 1) << lo; } int main(void)…
- What is printed? c int f(unsigned x) { int c = 0; while (x & (x >> 1)) { x &= x >> 1; c++; } return c + (x != 0); } int main(void) {…
- What is printed? c char d[6] = "zzzzz"; strncpy(d, "abc", 2); printf("%s", d);
- Assume ASCII. What is printed? c char s[] = "xyzAB"; for (char p = s; p; p++) if (p >= 'a' && p <= 'z') p = 'a' + (p - 'a' + 3) % 26…