Choose the correct output of the following Python program snippet: D = 11 B, E…

2026

Choose the correct output of the following Python program snippet:

D = 11
B, E = 0, 1
while D > 0:
    R = D % 2
    D //= 2
    B += R * E
    E *= 10
print(B)

Answer: A. 1011CONCEPTIn a repeated-division conversion, each remainder D % 2 is the next binary digit, starting from the least significant position. The statement D //= 2…

  1. A.

    1011

  2. B.

    1101

  3. C.

    1001

  4. D.

    1100

Attempted by 601 students.

Show answer & explanation

Correct answer: A

CONCEPT

In a repeated-division conversion, each remainder D % 2 is the next binary digit, starting from the least significant position.

The statement D //= 2 removes the digit just found, while E takes the values 1, 10, 100, ... so B stores those binary digits as a decimal-looking integer.

APPLICATION

Start with D = 11, B = 0, and E = 1. Trace the loop after each complete iteration:

  1. R = 11 % 2 = 1; then D = 11 // 2 = 5, B = 0 + 1 * 1 = 1, and E = 10.

  2. R = 5 % 2 = 1; then D = 5 // 2 = 2, B = 1 + 1 * 10 = 11, and E = 100.

  3. R = 2 % 2 = 0; then D = 2 // 2 = 1, B = 11 + 0 * 100 = 11, and E = 1000.

  4. R = 1 % 2 = 1; then D = 1 // 2 = 0, B = 11 + 1 * 1000 = 1011, and E = 10000.

CROSS-CHECK

Reading the successive remainders from the last one to the first gives 10112, and 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 = 11. Therefore print(B) outputs 1011.

Explore the full course: Bpsc

Loading lesson…