Python Junior
What does the if __name__ == '__main__' idiom do, and why is it used?
Select the correct answer
It runs the guarded code only when the file is imported, never when executed directly
It declares a private namespace so the module's names stay hidden from importers
It marks the file as the program's required entry point before any imports load
It runs the guarded code only when the file is executed directly, not when imported
What is the fundamental difference between the is operator and the == operator in terms of memory and the data model?
Select the correct answer
is compares object identity (same memory), while == compares values
is compares type and hash, while == compares the underlying memory layout
is copies then compares, while == references then compares identity directly
is compares the values held, while == compares identity (same memory address)
What is the difference between a list, a tuple, a set, and a dictionary, and when would you choose each?
Select the correct answer
List maps keys to values, tuple is mutable ordered, set is an immutable sequence, dict holds unique unordered items.
List is ordered immutable, tuple is mutable unique, set is a key-value mapping, dict is an ordered mutable list.
List is unordered mutable, tuple is ordered unique, set maps keys to values, dict is an immutable sequence type.
List is an ordered mutable sequence, tuple is ordered immutable, set holds unique items, dict maps keys to values.
What is a namedtuple, and how does it compare to a regular tuple or a class?
Select the correct answer
A dict subclass with fixed keys that acts like a tuple but allows item reassignment later
A mutable tuple variant that allows attribute access and supports adding new fields later
A tuple subclass with named fields giving readable attribute access while staying immutable
A lightweight class with named slots that is mutable and compares by identity not value
What is the purpose of the else and finally clauses in a try/except block?
Select the correct answer
else runs when an exception is caught; finally runs only if none was raised
else and finally both run only when a raised exception is handled by except
else runs before the try block starts; finally runs only if an error is unhandled
else runs when no exception occurred; finally always runs for cleanup regardless
What is the difference between a classmethod, a staticmethod, and a regular instance method?
Select the correct answer
A staticmethod receives cls, a classmethod receives self, an instance method receives neither.
An instance method receives cls, a classmethod receives self, a staticmethod the module.
An instance method receives self, a classmethod receives cls, a staticmethod receives neither.
All three receive self, but only a classmethod can modify shared class-level state safely.
What is the difference between __str__ and __repr__, and when is each used?
Select the correct answer
__str__ is called by logging only, while __repr__ is used everywhere a string is needed.
__repr__ formats floats and ints, while __str__ handles only container and sequence types.
__str__ gives a readable form for users; __repr__ gives an unambiguous form for developers.
__repr__ gives a readable form for users; __str__ gives an unambiguous form for developers.
What is an Enum in Python, and why would you use one instead of plain constants?
Select the correct answer
A dictionary subclass mapping names to values with no protection against duplicates
A mutable container of constants that can be reassigned at runtime for added flexibility
A subclass of int whose members are plain integers indistinguishable from literals
A class of named constant members that gives readable names, grouping, and type safety
What is the difference between a generator and a list? Why are generators considered 'lazy'?
Select the correct answer
A list and a generator both store all elements, but a generator caches them for repeated reuse later
A list yields values lazily on demand, while a generator builds the full sequence up front eagerly
A list holds every element in memory, while a generator computes each value only when it is requested
A list computes values only when requested, while a generator holds all elements in memory at once
What are the memory benefits of using a generator expression over a list comprehension when processing a very large file?
Select the correct answer
A generator expression keeps only one item in memory at a time, while a list comprehension loads them all
A generator expression copies the file twice in memory, while a list comprehension reads it just once
A generator expression loads all items into memory at once, while a list comprehension keeps only one
Both load every line into memory, but a generator expression compresses them to reduce total usage
What is the difference between *args and **kwargs, and how are they unpacked in a function call?
Select the correct answer
*args collects extra positional arguments as a tuple, while **kwargs collects extra keyword arguments as a dict
*args stores values as a list and **kwargs stores them as a set, both unpacked positionally
*args collects extra keyword arguments as a dict, while **kwargs collects extra positional arguments as a list
*args and **kwargs both collect positional arguments, but only into a tuple of pairs
What are the limitations of lambda functions compared to named functions, and when should they be avoided in production code?
Select the correct answer
A lambda cannot be assigned to a variable or passed around, making it unusable as a callback argument
A lambda cannot accept arguments or return values, so it should be avoided whenever data must be passed in
A lambda runs far slower than a def function because it is recompiled on every single call at runtime
A lambda is limited to a single expression, has no name or docstring, and hurts readability for complex logic
What is a list comprehension, and what are its advantages over using a for loop with append?
Select the correct answer
A concise expression building a list in one line, usually more readable and faster than append in a loop
A shorthand that compiles to identical bytecode as a loop, offering only cosmetic differences in style
A syntax that runs the loop body in parallel threads, making it strictly faster for any large input set
A lazy generator that yields list items on demand, saving memory by never building the full list at once
How does the sorted() function differ from the list.sort() method, and what is the role of the key parameter?
Select the correct answer
sorted() sorts in place while list.sort() returns a copy; key decides whether the order is ascending
Both sort in place, but sorted() works on any iterable; key reverses the final ordering of items
sorted() returns a new list while list.sort() sorts in place; key sets what each item is sorted by
Both return new lists, but list.sort() is faster; key provides a comparison function taking two arguments
What is the difference between f-strings, str.format(), and %-formatting?
Select the correct answer
f-strings and % evaluate at runtime, while str.format() is resolved at compile time.
All three are identical in behavior and differ only in the surrounding quote characters.
Only str.format() supports embedding expressions; f-strings just concatenate plain strings.
f-strings embed expressions inline, while str.format() and % use placeholder substitution.
What is the difference between a module and a package, and what role does __init__.py play?
Select the correct answer
Modules and packages are identical, and __init__.py only stores documentation strings.
A package is a single file while a module is a directory containing several scripts.
A module must contain __init__.py while a package needs no special marker file.
A module is a single file; a package is a directory whose __init__.py marks it.