ProgrammingHardTCS NQT · Infosys pattern
What is printed? (binary string of n using recursion, division by 2)
What is printed? (binary string of n using recursion, division by 2)
void toBinary(int n) {
if (n == 0) return;
toBinary(n / 2);
printf("%d", n % 2);
}
int main(void) {
toBinary(13);
}- A. 1011
- B. 1101
- C. 13
- D. 0000
Correct answer: B. 1101
The recursive call happens BEFORE the printf, so digits are produced in most-significant-first order as the stack unwinds. 13 in binary is 1101, and this standard recursion prints exactly that.
More C Programming questions
- What is printed? (two mutually exclusive recursive branches selected by a condition, with accumulation via return value) c int f(int n) {…
- What is printed? (recursion where the base case is reached via an off-by-one condition) c int f(int n) { if (n < 1) return 0; return n +…
- What is printed? (recursion with a pointer parameter used only for read, not accumulation) c int maxOf(int a[], int n) { if (n == 1)…
- What is printed? (recursive function returning a computed value through a chain of ternaries) c int classify(int n) { return n == 0 ? 0…
- What is printed? c #define SQUARE(x) x x int main(void) { int a = 3; printf("%d", SQUARE(a + 1)); }
- What is printed? c #define PRINT(x) printf(#x " = %dn", x) int main(void) { int apple = 10; PRINT(apple); }