ProgrammingModerate

Which function call costs O(n) time per call and can be avoided by caching its result in a loop?

  1. A. The 'sizeof' operator applied to an array
  2. B. A call to free on a single pointer
  3. C. Integer addition with +
  4. D. strlen(s) inside a loop condition over a string of length n

Correct answer: D. strlen(s) inside a loop condition over a string of length n

strlen has to scan the string each time it is called, so putting it in the loop condition repeats that scan. sizeof is a compile-time constant and free on one pointer is O(1) in typical use.

More C Programming questions

Exams that ask this topic