Pseudo-code · Question 30 · Hard

What will be the output of the following pseudocode? (Note: for-loop bounds are inclusive.)

Integer i, j, flag, s
Set s = 0
for (i = 2 to 30)
    flag = 1
    for (j = 2 to i - 1)
        if (i mod j == 0)
            flag = 0
            break
        end if
    end for
    if (flag == 1)
        s = s + i
    end if
end for
Print s
  1. 129
  2. 130
  3. 100
  4. 128
Answer and explanation

Answer: A. 129

Explanation: The inner loop sets flag to 0 when i has a divisor between 2 and i - 1, so only primes are added. The primes up to 30 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and their sum is 129.

Previous: What will be the output of the following pseudocode? (Note: for-loop bounds…

Practise pseudo-code

Try a fresh set of pseudo-code questions with an explanation after every answer, free and without signing up: Pseudo-code practice. Or sit a full placement mock test.

More pseudo-code questions

More technical & programming practice

C Programming · Java · Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge