ProgrammingHard
unsigned n = 5; int c = 0; while (n-- > 0) c++; printf("%d", c); What is printed?
- A. 4
- B. 5
- C. 6
- D. 0
Correct answer: B. 5
The unsigned n goes 5, 4, 3, 2, 1 and each test passes before the decrement. When n is 0 the test fails, so c is 5.
More C Programming questions
- int a = 0; for (int i = 0; i < 1000000; i++) a ^= i; printf("%d", a); What is printed?
- int x = 1, c = 0; while (x < 1000000000) { x = 3; c++; } printf("%d", c); What is printed (no overflow occurs)?
- A linear search over an array of length n finds the value only at the last position. How many comparisons does it make?
- Bubble sort with a swapped flag stops early. On an already sorted array of length n, how many passes run?
- A recursive binary search on n = 1000 sorted values reaches what maximum recursion depth?
- Merge sort on n = 2^20 elements has what recursion depth?