How many hashes (#) will the following snippet send to the console?
var = 1 while var < 10: print("#") var = var << 1
- one
- four
- eight
- two
Explanation: Let’s analyze this example:
- the var variable is assigned the integer value of 1,
- the while loop begins comparing 1 < 10, and since it is True, the loop is entered,
- a first # is printed on the console,
- the var variable is binary shifted 1 position to the left, and it is now 2,
- the while loop compares 2 < 10, and since it is True, the loop is entered,
- a second # is printed on the console,
- the var variable is binary shifted 1 position to the left, and it is now 4,
- the while loop compares 4 < 10, and since it is True, the loop is entered,
- a third # is printed on the console,
- the var variable is binary shifted 1 position to the left, and it is now 8,
- the while loop compares 8 < 10, and since it is True, the loop is terminated,
- a fourth # is printed on the console,
- the var variable is binary shifted 1 position to the left, and it is now 16,
- the while loop compares 16 < 10, and since it is False, the loop is terminated.
More Questions: Python Essentials 1 – Module 3 Test
Please login or Register to submit your answer