Bird
0
0

Consider this function that returns the sum of positive numbers until a zero is found:

hard📝 Application Q9 of 15
C - Loop Control Statements
Consider this function that returns the sum of positive numbers until a zero is found:
int sumUntilZero(int arr[], int size) {
  int sum = 0;
  for(int i=0; i 0) sum += arr[i];
  }
  return sum;
}
What will be the output for input array {1, 2, -1, 0, 5}?
A8
B0
C3
D6
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop and sum

    Elements: 1 (sum=1), 2 (sum=3), -1 (ignored), 0 (return sum=3).
  2. Step 2: Return sum when zero found

    Function returns 3 immediately when zero is found at index 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Return inside loop returns sum at zero [OK]
Quick Trick: Return sum immediately when zero found inside loop [OK]
Common Mistakes:
  • Including negative numbers in sum
  • Continuing loop after zero
  • Returning wrong sum value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes