Builtins, Control Flow & Scope Interview Questions
any()/all(), loop control flow, scope, closures, and Python's namespace model.
Question 1: What is a sentinel object?
Ans
A sentinel is a unique object used to represent a special state that cannot safely be confused with a normal value such as None.
Example
python
MISSING = object()
value = data.get("name", MISSING)
if value is MISSING:
print("missing")
Important Point
Use identity comparison with the sentinel.
Question 2: What is truthiness?
Ans
Objects can be evaluated in Boolean contexts. False, None, numeric zero, empty strings, and empty containers are falsey by default; most other objects are truthy.
Example
python
items = []
if not items:
print("empty")
Important Point
Custom classes can define `__bool__` or `__len__` to control truth testing.
Question 3: What is any()?
Ans
any() returns True when at least one item in an iterable is truthy and stops early once it finds one.
Example
python
values = [0, 0, 5, 0]
print(any(values))
Important Point
It short-circuits, so generators can avoid unnecessary work.
Question 4: What is all()?
Ans
all() returns True when every item in an iterable is truthy and stops at the first falsey item.
Example
python
values = [2, 4, 6]
print(all(x % 2 == 0 for x in values))
Important Point
`all([])` is True because there is no element that violates the condition.
Question 5: What is pass?
Ans
pass is a no-operation statement used where Python syntax requires a statement but no action is currently needed.
Example
python
class FutureFeature:
pass
Important Point
`pass` does not mean 'skip the loop iteration'; use `continue` for that.
Question 6: pass vs continue vs break?
Ans
pass does nothing, continue skips to the next loop iteration, and break exits the loop.
Example
python
for n in range(5):
if n == 1:
continue
if n == 4:
break
print(n)
Important Point
Use each based on control-flow intent; they are not interchangeable.
Question 7: What is else on a loop?
Ans
A loop's else block runs when the loop finishes normally without hitting break.
Example
python
for n in [1, 3, 5]:
if n % 2 == 0:
break
else:
print("No even number")
Important Point
The loop else is tied to normal completion, not to whether the loop body executed at least once.
Question 8: What is sorted key function?
Ans
The key argument tells sorted() what value to compare for each element.
The key function is called to obtain comparison values; it does not need to return booleans.
Question 9: What is a namespace?
Ans
A namespace is a mapping from names to objects. Python has local, enclosing, global, and built-in namespaces, among others.
Example
python
x = 10
print(globals()["x"])
Important Point
Namespace concepts explain why two scopes can contain the same name without being the same binding.
Question 10: What is scope?
Ans
Scope defines where a name can be accessed directly. Python's name lookup follows local, enclosing, global, and built-in scopes.
Example
python
x = "global"
def f():
x = "local"
print(x)
f()
Important Point
A nested function can read an enclosing binding but needs `nonlocal` to reassign it.
Question 11: What is a closure cell?
Ans
When a nested function retains a variable from an enclosing scope, Python stores that captured binding in a closure cell accessible through function metadata.
Example
python
def outer():
x = 10
def inner(): return x
return inner
f = outer()
print(f.__closure__)
Important Point
This is an implementation-level introspection detail; ordinary code should usually reason in terms of closures.
Question 12: What is function first-class object?
Ans
Python functions can be assigned to variables, stored in collections, passed as arguments, and returned from other functions.
This property enables callbacks, decorators, and higher-order functions.
Question 13: What is callable()?
Ans
callable() returns whether an object appears callable, meaning it can be invoked with parentheses according to Python's runtime rules.
Example
python
print(callable(len))
print(callable(10))
Important Point
Being callable does not guarantee that calling it with a particular argument list will succeed.
Question 14: What is the difference between None, False, and 0?
Ans
None represents absence of a value, False is a boolean false value, and 0 is the integer zero. They can all be falsey but have different types and meanings.
Example
python
print(None is None)
print(False == 0)
print(type(None), type(False), type(0))
Important Point
Do not use falsey checks when the requirement specifically distinguishes None from other falsey values.