Quiz · Medium
Twenty questions on typing, compilers, the call stack, memory, number and text representation, Python semantics and object-oriented principles.
Which pair describes Python's typing discipline?
Static and weak
Static and strong
Dynamic and strong
Dynamic and weak
Python checks types at run time (dynamic) and refuses mixing unrelated types such as '3' + 4 (strong).
Which phase of compilation turns a stream of tokens into a tree that reflects the program's structure?

Parsing
Lexing
Code generation
Linking
The lexer produces tokens; the parser builds the syntax tree (like the one shown); later phases generate code.
The abstract syntax tree in the picture is for which loop?
A for loop summing numbers
A recursive factorial
Binary search
Euclid's algorithm by subtraction: while b != 0, subtract the smaller from the larger, return a
The root has a while node (condition b ≠ 0) with a branch comparing a > b and subtracting, and returns a. Image: Dcoetzee, CC0, via Wikimedia Commons.
What does -7 // 2 evaluate to in Python?
-3
-4
3
-3.5
Floor division rounds toward negative infinity: −3.5 floors to −4. Truncation (as in C) would give −3.
What does bool('0') return in Python?
False
Error
True
None
Any non-empty string is truthy, including '0'. Only '' is falsy.
What is printed?
a = [1, 2]
b = a
b.append(3)
print(a)[1, 2, 3]
[1, 2]
[3]
Error
b = a makes both names refer to the same list. Appending through b changes what a sees.
What is the result of round(3.5) and round(2.5) in Python 3?
4 and 3
3 and 2
3 and 3
4 and 2
Python 3 uses banker's rounding: halves round to the nearest even integer, so 3.5 → 4 and 2.5 → 2.
In the call-stack diagram, what is stored between the parameters and the locals of a frame?

The heap pointer
The return address
The global variables
The next function's code
The return address tells the CPU where to continue in the caller once this function returns.
Which statement about stack and heap memory is correct?
The heap is faster and always smaller
Local variables always live on the heap
Stack allocation is automatic and LIFO; heap allocation has a flexible lifetime
Heap memory is freed automatically on function return
Stack frames are pushed and popped as functions are called and return. Heap objects live until freed or garbage-collected.
Why does CPython need a cycle collector in addition to reference counting?
Objects that reference each other keep non-zero counts even when unreachable
Reference counting is too slow
Counting only works for integers
The stack overflows otherwise
Two objects referring to each other never reach a count of zero; the cycle collector finds such unreachable groups.
What are the ASCII codes (decimal) of 'A' and 'a'?

97 and 65
64 and 96
65 and 98
65 and 97
'A' = 65 and 'a' = 97, a difference of 32 (a single bit). Image: Klaas van Buiten, CC BY-SA 4.0, via Wikimedia Commons.
What is 5 ^ 3 in Python?
8
6
15
2
^ is bitwise XOR: 101 XOR 011 = 110 = 6. The power operator is **.
Why does 0.1 + 0.2 == 0.3 evaluate to False in most languages?
Python has a bug with decimals
The == operator only compares integers
0.1 and 0.2 cannot be represented exactly in binary floating point
Addition always loses precision
The stored values are approximations; their sum is 0.30000000000000004. Use math.isclose or the decimal module.
A 32-bit signed integer holds 2,147,483,647. In C, what is the typical result of adding 1?
It wraps to −2,147,483,648
It becomes 2,147,483,648
It becomes 0
The program always crashes
Two's complement wraps around (formally undefined behavior for signed types). Python integers never overflow.
In the endianness diagram, how does a little-endian machine store a 32-bit number compared with a big-endian one?

Bits reversed within each byte
Same order but different address range
Bytes sorted alphabetically
Bytes in reverse order, least significant byte first
Little-endian places the least significant byte at the lowest address. The bytes themselves are not bit-reversed. Image: Daniele Giacomini, CC BY-SA 2.5, via Wikimedia Commons.
Which OOP principle lets one interface (for example shape.area()) run different code for different object types?
Encapsulation
Polymorphism
Abstraction only
Serialization
Polymorphism: the same call dispatches to each subclass's own implementation.
Which principle says a class should have only one reason to change?
Open/closed
Liskov substitution
Single responsibility
Interface segregation
The S in SOLID.
True or false: a closure in Python can modify an enclosing variable without any declaration.
True
False
Assigning to a variable in an enclosing function requires nonlocal; otherwise Python creates a new local variable.
True or false: finally runs even when the try block contains a return statement.
True
False
The finally block always executes before the function actually returns.
What does the C++ snippet in the figure do with **newPtr = 33; after newPtr = &myPtr?

Changes the value of myVar to 33
Changes the pointer myPtr
Declares a new variable
Frees myVar
Dereferencing twice follows newPtr → myPtr → myVar and writes 33 into myVar. Image: Pat McClanahan, CC BY-SA 4.0, via Wikimedia Commons.