116k views
2 votes
Consider the given code fragment. Determine the output of the program fragment.m = 2 repeat until (m > 500) { display(m) m = m * 2 } a) 2, 4, 8, 16, 32, 64, 128, 256, 512 b) 2, 4, 8, 16, 32, 64, 128, 256 c) 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 d) 2, 4, 8, 16, 32, 64, 128, 256, 1024

1 Answer

3 votes

Step-by-step explanation:

a "repeat until" loop checks the criteria at the end of the loop.

but the sequence of the instructions inside the loop writes m before it gets increased (multiplied by 2).

so,

b) 2, 4, 8, 16, 32, 64, 128, 256

is correct, because at the end m = 256, it gets displayed, then m gets multiplied by 2 (now it is 512). and NOW the loop checks the end criteria (m > 500) and ends the loop.

so, the number 512 never gets displayed. and we are not getting to any larger number anyway.

User RajeshM
by
8.5k points