1
What is the primary reason Python is called 'Python'?
It is named after a snake
It is named after a British comedy group
It is named after a famous mathematician
It is named after a programming concept
Explanation: Python is named after "Monty Python's Flying Circus", a British comedy series from the 1970s. Guido van Rossum, Python's creator, was a fan of the show and chose this name for his programming language.
2
Which of the following is NOT a characteristic of Python as described in the lecture?
Python is a low-level programming language
Python is free and open source
Python is portable between different systems
Python is easy to start using
Explanation: Python is actually a high-level programming language, not a low-level one. Python abstracts away many low-level details like memory management, making it easier to write and understand code. All other listed characteristics are true about Python - it is free and open source, can run on various platforms, and is known for its ease of use.
3
What is the significance of indentation in Python?
It is optional and used for readability
It is part of the syntax and defines code blocks
It is used to declare variables
It is used to compile code
Explanation: In Python, indentation is a fundamental part of the language syntax, not just for readability. It is used to define code blocks such as loops, functions, classes, and conditional statements. Unlike many other programming languages that use braces {} or keywords to define blocks, Python uses indentation to determine the grouping of statements. Incorrect indentation will result in syntax errors.
4
What does the ID of an object in Python represent?
The type of the object
The value of the object
The memory address of the object
The class of the object
Explanation: In Python, the ID of an object (which can be obtained using the id() function) represents the object's memory address. It is a unique identifier for the object during its lifetime and is guaranteed to be unique among simultaneously existing objects. This ID is particularly useful when working with mutable and immutable objects to understand object references and identity.
5
What is the primary reason Python uses the same memory location for variables `a=7`, `b=7`, and the constant `7`?
To reduce memory fragmentation
To optimize performance by reusing small integers
To enforce type safety
To simplify garbage collection
Explanation: Python implements integer interning (also known as integer caching) for small integers (typically -5 to 256) as an optimization technique. When these small integers are used, Python reuses the same objects instead of creating new ones for each variable. This is done primarily for performance optimization, as small integers are frequently used in programs. This behavior is an implementation detail and should not be relied upon for program logic.
6
What is the size in bytes of the integer `1` in Python?
16 bytes
20 bytes
24 bytes
28 bytes
Explanation: In Python 3, even a simple integer like `1` takes 28 bytes of memory. This is because Python integers are actually objects that contain not just the value, but also additional information like reference counting, type information, and other object metadata. This size might seem large compared to other languages, but it's part of Python's design that prioritizes flexibility and functionality over memory efficiency for primitive types.
7
What are the three main components of a long object in Python as described in the lecture?
Object reference counter, pointer to the type object, and the actual value
Object ID, type, and value
Object size, pointer to the type object, and the actual value
Object reference counter, object size, and the actual value
Explanation: In Python, every long object (and in fact, every object) consists of three main components:
1. A reference counter that keeps track of how many references point to the object
2. A pointer to the type object that defines what kind of object it is
3. The actual value of the object
This structure is fundamental to Python's object model and memory management system, particularly for its reference counting garbage collection mechanism.
8
Given the code:
a = 300
b = 300
print(id(a) == id(b))
What will be the output and why?
False, because integers outside the range [-5, 256] are not interned by Python
True, because all integers in Python share the same memory location
False, because variables 'a' and 'b' are different names
True, because 'a' and 'b' have the same value
Explanation: In Python, integers between -5 and 256 (inclusive) are interned, meaning Python reuses the same objects for these values. However, for integers outside this range (like 300), Python creates new objects each time. Therefore, even though both `a` and `b` have the value 300, they refer to different objects in memory, resulting in different memory addresses (IDs). This is why `id(a) == id(b)` returns `False`. This behavior is an implementation detail and shouldn't be relied upon for program logic.