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
  1. A1800
  2. B1900
  3. 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.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. When the following function pow is called as pow(2, 10), how many times is pow called, including the first call?
  2. What does the following function s return when called as s(7)?
  3. When the following procedure p is called as p(3), what sequence of values is output?
  4. What does the following function fib return when called as fib(10)?
  5. When the following function c is called as c(4, 2), how many times is c called, including the first call?