Programming Basics (Pseudocode Tracing)
The following function isLeap returns true if the year y is a leap year. and is evaluated before or. For which y does isLeap(y) return true?
○boolean: isLeap(integer: y)
return y mod 4 = 0 and y mod 100 ≠ 0 or y mod 400 = 0
- A1800
- B1900
- C2000
Show answer and explanation
Answer: C. 2000
Because and takes precedence over or, the condition means (y mod 4 = 0 and y mod 100 ≠ 0) or y mod 400 = 0. A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. 1800 and 1900 are divisible by 100 but not by 400, so they give false. 2000 is divisible by 400, so it gives true.
- A. 1800 mod 100 = 0 makes the and part false, and 1800 mod 400 = 200 makes the right side of or false, so the result is false.
- B. 1900 mod 100 = 0 makes the and part false, and 1900 mod 400 = 300 makes the right side of or false, so the result is false.
- C. Correct. 2000 mod 400 = 0, so the result is true.
Continue: 9 more Programming Basics (Pseudocode Tracing) questions