Exam-style question
Try this first
An algorithm processes a list of numbers. If the current number is even, it adds 1 to a counter. If it's odd, it subtracts 1 from the counter. What is the value of the counter after processing the list [5, 2, 8, 3] if the counter starts at 0?.
- A.-1
- B.0
- C.1
- D.2
Answer and explanation
Concise answer
-1
Why this is correct
The counter starts at 0. For 5 (odd), counter becomes -1. For 2 (even), counter becomes 0. For 8 (even), counter becomes 1. For 3 (odd), counter becomes 0. Oh, wait let me recheck the calculation. 5 is odd: 0 - 1 = -1. 2 is even: -1 + 1 = 0. 8 is even: 0 + 1 = 1. 3 is odd: 1 - 1 = 0. The calculation was incorrect. I need to re-evaluate it based on the prompt's instruction: For 5 (odd), counter becomes 0 - 1 = -1.
Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.
Common mistake
No common mistake is linked to this question yet.
