ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int used[3];
int count(int d) {
    if (d == 3) return 1;
    int t = 0;
    for (int i = 0; i < 3; i++) {
        if (used[i]) continue;
        used[i] = 1;
        t += count(d + 1);
        used[i] = 0;
    }
    return t;
}
int main(void) {
    printf("%d", count(0));
}
  1. A. 9
  2. B. 3
  3. C. 8
  4. D. 6

Correct answer: D. 6

The recursion counts the permutations of three items, which is 3! = 6.

More C Programming questions

Exams that ask this topic