ITPEC FE Subject B April 2024 Question 3

From the answer group below, select the correct answer to be inserted into blank in the program.

The program outputs the even numbers between 1 and 100. Subsequently, it prints the sum of those even numbers. Note that division is performed for data type integer, that is, a ÷ b is the quotient of a divided by b.

[Program]
integer: i
integer: sum ← 0
for(increase i from 1 to 100 by 1)
if(blank)
output i
sum ← sum + i
endif
endfor
output sum

Answer group

Explanation

i mod 2 = 0 — a number is even exactly when dividing it by 2 leaves no remainder.

mod gives the remainder, while ÷ here is integer division and gives the quotient. For i from 1 to 100, i mod 2 is 0 for 2, 4, …, 100 and 1 for the odd numbers. So the loop outputs the 50 even numbers and then their sum, 2,550.

Answer (c)

Why not others:

  • •(a) — i ÷ 2 is the quotient, which is 0 only for i = 1: the program outputs 1 and a sum of 1
  • •(b) — the quotient is non-zero for every i from 2 to 100, so it outputs all of 2 to 100, odd and even
  • •(d) — a remainder of 1 selects the odd numbers 1, 3, …, 99
  • •(e) — true only for i = 2, so it outputs just 2
  • •(f) — true for every i except 2, so it outputs 1 and 3 to 100

Key rule: Test divisibility with the remainder, x mod n = 0, not with the quotient; under integer division the quotient is 0 only when x is less than n.

Math & Numbers · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.