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)→6int(b)→3int(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
666Statement 3
print(int(a) == b)Calculation
int(a)→6b→3.7
6==3.7
Output
FalseStatement 4
print(bool(a), bool(0), bool(''), bool(' '), bool([]))Output
True False False True FalseReason
Non-empty string →
True0→FalseEmpty string →
FalseString with space →
TrueEmpty 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) == bPython allows comparison between integers and floating-point numbers because both are numeric data types.
int(a)becomes integer6bis float3.7
Python automatically compares their numeric values. Since the values are different, the result becomes:
FalseNo 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 = 10Expression 2
int(a + b + c)This produces an error because:
ais a stringbis a float
Python cannot add string and float directly.
Error
TypeErrorTherefore, 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.