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));
}- A. 9
- B. 3
- C. 8
- D. 6
Correct answer: D. 6
The recursion counts the permutations of three items, which is 3! = 6.
More C Programming questions
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 0x0F; uint8t r =…
- How many set bits does this loop count in 0xFFFF0000? c #include <stdio.h> int main(void) { unsigned x = 0xFFFF0000u; int c = 0; while (x)…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned a = 65536u; printf("%u", a a); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", sizeof(char) - 2 > 0); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; int best = a[0], cur = a[0]; for (int i…
- How many subarrays of {1, -1, 2, -2, 3} have sum zero? Compute with this program. c #include <stdio.h> int main(void) { int a[] = {1, -1…