Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Output Questions - Part 1
PYTHON

Output Questions - Part 1

Practice 15 Python predict-the-output questions covering interning, default arguments, short-circuit evaluation, division, and common gotchas.

1. What is printed by this integer identity check?

a = int("256")
b = int("256")
c = int("257")
d = int("257")

print(a is b)
print(c is d)

Output:

True
False

CPython caches small integers in the range -5 to 256, so even two independently constructed int("256") objects resolve to the same cached objecta is b is True. The value 257 is outside that cache, so c and d are two separate objects and c is d is False. This is the single most asked Python is vs == question in interviews. (Using int(...) forces fresh construction; writing c = 257; d = 257 as literals would fold to one constant and give True — that is constant folding, not the cache.)

2. What does this print?

s1 = "hello"
s2 = "hello"

print(s1 is s2)
print(s1 == s2)

Output:

True
True

Short string literals are interned by CPython, so both names point to the same object and is is True. == compares the values, which are of course equal. The trap: is works here only because of interning — it is not guaranteed for strings built at runtime.

3. What is printed by this function call sequence?

def add(x, items=[]):
    items.append(x)
    return items

print(add(1))
print(add(2))
print(add(3))

Output:

[1]
[1, 2]
[1, 2, 3]

The default argument [] is evaluated once at function definition time and reused on every call. The list keeps growing across calls. This is the classic “mutable default argument” trap — the fix is items=None and creating a fresh list inside the function.

4. What do these short-circuit expressions print?

print(0 and 5)
print(3 and 5)
print(0 or 5)
print(3 or 5)

Output:

0
5
5
3

and returns the first falsy operand or the last operand; or returns the first truthy operand or the last. Neither operator coerces to a boolean — they return one of the actual operands. So 0 and 5 is 0 (first falsy), 3 and 5 is 5 (both truthy, last wins), 0 or 5 is 5, and 3 or 5 is 3.

5. What is printed by these division expressions?

print(7 // 2)
print(-7 // 2)
print(7 // -2)

Output:

3
-4
-4

// is floor division: it rounds down toward negative infinity, not toward zero. So -7 // 2 is -4 (since -3.5 floors to -4), and 7 // -2 is also -4. If you expected -3, you fell for the “truncation toward zero” mental model, which is correct in C but not in Python.

6. What do these modulo expressions print?

print(7 % 3)
print(-7 % 3)
print(7 % -3)

Output:

1
2
-2

The result of % has the sign of the divisor. -7 % 3 is 2 (not -1): it is the value r with 0 <= r < 3 such that -7 = 3 * q + r, i.e. -7 = 3 * (-3) + 2. Similarly 7 % -3 is -2 because the divisor is negative. This is a very common Python interview trap.

7. What does this closure-in-loop code print?

funcs = []
for i in range(3):
    funcs.append(lambda: i)

for f in funcs:
    print(f())

Output:

2
2
2

All three lambdas close over the same variable i. By the time they are called, the loop has finished and i is 2 — so every lambda returns 2. This is the classic “late binding” closure trap. The fix is a default argument lambda i=i: i or using functools.partial.

8. What is printed by this list aliasing code?

a = [1, 2, 3]
b = a
b.append(4)

print(a)

Output:

[1, 2, 3, 4]

b = a does not copy the list — it makes b another reference to the same object. Mutating through b (append) is visible through a. To copy, use a.copy(), list(a), or a[:].

9. What is printed by this tuple mutation attempt?

t = (1, 2, 3)
t += (4, 5)

print(t)

Output:

(1, 2, 3, 4, 5)

This does not error. Tuples are immutable, but t += ... first builds a new tuple (1, 2, 3, 4, 5) and then rebinds t to it. The original tuple object was never modified — the name now points at a new object. (If the tuple contained a list, that list would still be mutable.)

10. What does this float comparison print?

print(0.1 + 0.2 == 0.3)
print(0.1 + 0.2)

Output:

False
0.30000000000000004

Floating-point values are stored in binary, so 0.1 + 0.2 is 0.30000000000000004, not 0.3. Comparing floats for exact equality is unreliable — use a tolerance (abs(a - b) < 1e-9) or math.isclose.

11. What does this boolean-integer comparison print?

print(True == 1)
print(False == 0)
print(True + True)

Output:

True
True
2

In Python, bool is a subclass of int. True has the value 1 and False has 0, so True == 1 and False == 0 are True, and True + True is 2. This surprises programmers coming from languages where booleans are not numbers.

12. What is printed by these string operations?

s = "abcdef"

print(s[::-1])
print(s[1:4])
print("ha" * 3)

Output:

fedcba
bcd
hahaha

s[::-1] reverses the string (step -1), s[1:4] slices characters at indices 1, 2, 3 → "bcd", and "ha" * 3 repeats the string three times.

13. What is printed by this loop-with-else code?

for i in range(3):
    if i == 1:
        break
else:
    print("no break")

print("done")

Output:

done

The else block of a for loop runs only if the loop completes without break. Here break fires at i == 1, so the else is skipped and only "done" prints. Flip the loop to range(0) (no iterations, no break) and the else would run.

14. What does this enumerate + unpacking code print?

for i, c in enumerate("abc", start=1):
    print(i, c)

Output:

1 a
2 b
3 c

enumerate yields (index, element) pairs, and the start=1 offsets the index to begin at 1. The tuple unpacks into i and c.

15. What is printed by this dictionary code?

d = {"a": 1, "b": 2}

print(d.get("c"))
print(d.get("c", 99))
print(d.setdefault("c", 100))
print(d["c"])

Output:

None
99
100
100

d.get("c") returns None when the key is missing. d.get("c", 99) returns the default 99 without inserting. setdefault inserts "c": 100 if absent and returns the value — so now d["c"] is 100. This is a common campus-assessment question on dict access methods.

My Private Notes

Notes are auto-saved locally to this device.