ProgrammingVery hard
A loop does for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) work();. For n = 1024, how many times is work() called in total?
- A. 1024
- B. 2048
- C. 2047
- D. 2046
Correct answer: C. 2047
The outer loop takes i = 1024, 512, ..., 1, and the inner loop runs i times for each. The total is 1024 + 512 + ... + 1 = 2047.
More C Programming questions
- Which of these is an O(n) method to test whether a string of length n is a palindrome in C?
- char s[] = "hello world"; char w = strchr(s, ' ') + 1; printf("%s", w); What is printed?
- char a[8] = "abc"; memset(a + 1, 'z', 2); printf("%s", a); What is printed?
- char s[] = "12abc"; char end; long v = strtol(s, &end, 10); printf("%c", end); What is printed?
- char buf[3]; int n = snprintf(buf, 3, "%s", "hello"); printf("%d", n); What is printed?
- char p = "abc"; printf("%d", (p + 3)); What is printed?