ProgrammingHard
How many times does a body run in for (int i = 2; i < 100; i *= i)?
- A. 2
- B. 3
- C. 4
- D. 6
Correct answer: B. 3
i takes 2, 4, 16 and then 256 ends the loop, so the body runs for 2, 4 and 16.
More C Programming questions
- Which change makes the loop for (int i = 0; i < strlen(s); i++) run in linear time?
- A 2D grid of 2000 x 2000 cells is searched by a BFS that visits each cell once. About how many cell visits happen?
- A program finds the number of pairs (i, j) with i < j among n = 10^5 elements by a nested loop. How many pair checks happen?
- For n = 10^6, which C code computes the sum of the first n integers in O(1) without overflow in 64-bit long long?
- Which condition makes a loop with an int index i and a sizet bound n safe from an infinite loop when n is 0?
- A loop does for (int i = 1; i <= n; i++) { x = i / 2; } with n = 10^9. What is the main cost concern?