Study the following code fragment and answer every sub-part exhaustively. a =…

2026

Study the following code fragment and answer every sub-part exhaustively.

a = '6'
b = 3.7
c = True
d = None

print(type(int(a) + int(b) + int(c)))

print(a * int(b))

print(int(a) == b)

print(bool(a), bool(0), bool(''), bool(' '), bool([]))

(i) Predict the output of each print statement. For errors, write the error type and state the reason.

(ii) Using only built-in functions, write two different ways to round b to the nearest integer and verify that each gives a value 4.

(iii) int(a) == b evaluates without error. Explain why Python does not raise TypeError here.

(iv) A student claims int(a) + int(b) + int(c) and int(a + b + c) yield the same result. Prove whether this is true/false with relevant example.

Show answer & explanation

(i) Output Prediction

Statement 1

print(type(int(a) + int(b) + int(c)))

Calculation

  • int(a)6

  • int(b)3

  • int(c)1

6+3+1=10

Output

<class 'int'>

Statement 2

print(a * int(b))

Calculation

  • a = '6'

  • int(b)3

String multiplication repeats the string 3 times.

Output

666

Statement 3

print(int(a) == b)

Calculation

  • int(a)6

  • b3.7

6==3.7

Output

False

Statement 4

print(bool(a), bool(0), bool(''), bool(' '), bool([]))

Output

True False False True False

Reason

  • Non-empty string → True

  • 0False

  • Empty string → False

  • String with space → True

  • Empty list → False

(ii) Two Ways to Round b

b = 3.7

print(round(b))
print(int(b + 0.5))

Output

4
4

(iii) Why No TypeError Occurs

int(a) == b

Python allows comparison between integers and floating-point numbers because both are numeric data types.

  • int(a) becomes integer 6

  • b is float 3.7

Python automatically compares their numeric values. Since the values are different, the result becomes:

False

No TypeError occurs because numeric types are compatible for comparison.

(iv) Are Both Expressions Same?

Expression 1

int(a) + int(b) + int(c)

Result

6 + 3 + 1 = 10

Expression 2

int(a + b + c)

This produces an error because:

  • a is a string

  • b is a float

Python cannot add string and float directly.

Error

TypeError

Therefore, the student's claim is false.

The two expressions do not always give the same result.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Hpsc Pgt Computer Science