Read x; While(x < 60) print x; x = x*3. The last number printed is 54; what initial value did the user enter for x?

Prepare for the Progressive Pre-Employment Assessment Test with targeted practice questions. Sharpen your skills with detailed explanations and hints. Ace your assessment!

Multiple Choice

Read x; While(x < 60) print x; x = x*3. The last number printed is 54; what initial value did the user enter for x?

Explanation:
The key idea is how a while loop that prints first and then updates works: you start with the initial x, print it, then multiply x by 3 and repeat as long as the value at the start of an iteration is still less than 60. This creates a sequence x, x·3, x·9, x·27, and so on, with each printed value being less than 60. To get the last printed value as 54, you want a starting x that, when multiplied by 3 a few times, produces 54 as one of the printed values, while all earlier printed values stay below 60. If you start with 2, the printed sequence goes 2, 6, 18, 54. After printing 54, x becomes 162, and the loop stops because 162 is not less than 60. Hence 54 is the last number printed. Starting with 3, 4, or 5 yields last printed values of 27, 36, and 45 respectively, which don’t match 54. Therefore the initial value must be 2.

The key idea is how a while loop that prints first and then updates works: you start with the initial x, print it, then multiply x by 3 and repeat as long as the value at the start of an iteration is still less than 60. This creates a sequence x, x·3, x·9, x·27, and so on, with each printed value being less than 60.

To get the last printed value as 54, you want a starting x that, when multiplied by 3 a few times, produces 54 as one of the printed values, while all earlier printed values stay below 60. If you start with 2, the printed sequence goes 2, 6, 18, 54. After printing 54, x becomes 162, and the loop stops because 162 is not less than 60. Hence 54 is the last number printed.

Starting with 3, 4, or 5 yields last printed values of 27, 36, and 45 respectively, which don’t match 54. Therefore the initial value must be 2.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy