ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Integer n = 17, i = 2
Boolean isPrime = true
while (i * i <= n)
    if (n mod i == 0)
        isPrime = false
        break
    end if
    i = i + 1
end while
Print isPrime, i
  1. A. false 5
  2. B. true 4
  3. C. true 17
  4. D. true 5

Correct answer: D. true 5

17 is prime, so the if-branch never triggers; the loop simply keeps incrementing i while i*i<=17 (i.e. i=2,3,4), and stops when i=5 since 5*5=25>17. isPrime stays true, i ends at 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic