[{"id":"pythonadvanced-001","courseId":"python-advanced","domain":"syntax-and-types","topic":"Int-to-str digit limit","prompt":"On CPython 3.11 or later with default settings, a request handler runs int(user_text) on a 5000-digit decimal string. What happens?","hint":"The limit depends on which number base is being converted.","explain":"CPython caps the digits allowed when converting between int and text, to block slow-conversion attacks. Some number bases are exempt, and the cap is a configurable global setting.","source":"https://docs.python.org/3/library/stdtypes.html#integer-string-conversion-length-limitation","interaction":{"type":"single"},"options":[{"id":"a","text":"It succeeds, because the cap only applies to hexadecimal, octal and binary strings.","whyNot":"Those are the exempt bases; base-10 conversion from a string is capped."},{"id":"b","text":"It raises ValueError, because the default cap for decimal conversion is 4300 digits.","why":"int() from a base-10 string is limited to 4300 digits by default, so 5000 digits is rejected."},{"id":"c","text":"It succeeds silently, because the cap applies only to str() output, not to int() input.","whyNot":"The limit covers int(string) with base 10, as well as str(integer) and repr(integer)."},{"id":"d","text":"It raises MemoryError, because Python cannot allocate integers that many digits wide.","whyNot":"A 5000-digit value is small enough to allocate; the rejection is a deliberate ValueError from the digit cap."}],"answer":"b","explanation":"CPython limits decimal int/str conversions to 4300 digits by default to mitigate denial-of-service attacks; hexadecimal, octal and binary conversions are unlimited.","concept":"CPython limits decimal int/str conversions to 4300 digits by default to mitigate denial-of-service attacks; hexadecimal, octal and binary conversions are unlimited.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"user_text = '7' * 5000\nn = int(user_text)","takeaways":["Untrusted numeric text can hit a ValueError; the cap is a DoS guard, so change it only on purpose."]},{"id":"pythonadvanced-002","courseId":"python-advanced","domain":"syntax-and-types","topic":"round() rounds ties to even","prompt":"In Python 3, round(2.5) returns 3, because a value exactly halfway between two integers always rounds up.","hint":"Think about what round() does when the input sits exactly between two whole numbers.","explain":"round() picks the nearest multiple of 10 to the power of -ndigits. When the input sits exactly midway, it needs a tie-break rule, and that rule is documented.","source":"https://docs.python.org/3/library/functions.html#round","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"round() does not round halves up; a tie goes to the even neighbour, so round(2.5) gives 2."},{"id":"b","text":"False","why":"round(2.5) returns 2, since the tie is resolved toward the even choice rather than upward."}],"answer":"b","explanation":"round() sends a value exactly halfway between two candidates to the even one, so round(0.5) is 0 and round(1.5) is 2.","concept":"round() sends a value exactly halfway between two candidates to the even one, so round(0.5) is 0 and round(1.5) is 2.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Exact halves go to the even neighbour in round(); don't assume they round up."]},{"id":"pythonadvanced-003","courseId":"python-advanced","domain":"syntax-and-types","topic":"Deferred evaluation of annotations","prompt":"In Python 3.14, a class body contains annotated attributes. Put these steps, from compile time to the first read of __annotations__, in order.","hint":"Ask when the annotation expressions actually run.","explain":"Python 3.14 changed when annotation expressions run: they are packaged into a function, and the function is called only when the annotations are examined.","source":"https://peps.python.org/pep-0649/","interaction":{"type":"sort","items":[{"text":"The compiler moves each annotation expression into a generated __annotate__ function.","why":"The expressions are packaged at compile time instead of being run where they appear."},{"text":"The class object gets a reference to that __annotate__ function.","why":"The object holds the function without calling it, so nothing has been evaluated yet."},{"text":"A first read of __annotations__ calls __annotate__ and gets the dict.","why":"__annotations__ is a descriptor that calls the function on demand, the first time it is read."},{"text":"The resulting dict is cached, so later reads don't re-run the expressions.","why":"The descriptor calls the annotate function once, then reuses the stored result."}]},"options":[],"answer":"o:abcd","explanation":"PEP 649 compiles annotation expressions into an __annotate__ function; __annotations__ calls it once on first access and caches the resulting dict.","concept":"PEP 649 compiles annotation expressions into an __annotate__ function; __annotations__ calls it once on first access and caches the resulting dict.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Annotation expressions in 3.14 run when annotations are examined, not when the class body executes."]},{"id":"pythonadvanced-004","courseId":"python-advanced","domain":"syntax-and-types","topic":"Type parameter syntax","prompt":"On Python 3.12, this module defines a generic function. Which statement about its type parameter T is true?","hint":"Ask where the name T lives and what the runtime records about it.","explain":"Brackets after def or class declare type parameters in a scope of their own. Names resolve differently there than in ordinary globals, and the runtime keeps a record of the parameters.","source":"https://peps.python.org/pep-0695/","interaction":{"type":"single"},"options":[{"id":"a","text":"globals()['T'] returns the TypeVar, because all type parameters live in the module's globals.","whyNot":"Type parameters declared with the new syntax do not appear in the dict returned by globals() or locals()."},{"id":"b","text":"first.__type_params__ is a tuple containing a TypeVar named T.","why":"The new syntax records type parameters on the object; for a generic function, __type_params__ is a tuple of TypeVar, ParamSpec and TypeVarTuple instances."},{"id":"c","text":"Calling first([1, 2]) raises TypeError, because T must be passed as an argument at call time.","whyNot":"Type parameters are not runtime arguments; the call works like any other call to first."},{"id":"d","text":"T is an ordinary global, so a global T must be defined before the def line runs.","whyNot":"The type parameter has its own scope, so the def line does not need a global T to exist."}],"answer":"b","explanation":"PEP 695 type parameters get their own scope, are exposed as a tuple in __type_params__, and are kept out of globals() and locals().","concept":"PEP 695 type parameters get their own scope, are exposed as a tuple in __type_params__, and are kept out of globals() and locals().","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"def first[T](items: list[T]) -> T:\n    return items[0]","takeaways":["Find a generic function's type parameters via __type_params__, not by searching globals()."]},{"id":"pythonadvanced-005","courseId":"python-advanced","domain":"syntax-and-types","topic":"Containment checks test identity first","prompt":"The class below defines __eq__ to always return False. The snippet tests membership twice. What does it print on CPython?","hint":"Look at what the membership test checks for each element, and in what order.","explain":"A membership test on a list compares the value with each element. For each element it asks about object identity as well as equality, and the identity question comes first.","source":"https://docs.python.org/3/reference/expressions.html#membership-test-operations","interaction":{"type":"single"},"options":[{"id":"a","text":"False, then False","whyNot":"Membership uses only ==, and that always returns False here; identity is not consulted."},{"id":"b","text":"True, then False","why":"The same object passes the identity check on line one; the new Token fails both identity and the always-False ==."},{"id":"c","text":"True, then True","whyNot":"Containment compares by value, and a new Token counts as equal to the element already in the list."},{"id":"d","text":"False, then True","whyNot":"The second line matches by identity, because a freshly built Token is the same object as the one in the list."}],"answer":"b","explanation":"For a list, x in y is equivalent to any(x is e or x == e for e in y), so identity is tested before equality for each element.","concept":"For a list, x in y is equivalent to any(x is e or x == e for e in y), so identity is tested before equality for each element.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"class Token:\n    def __eq__(self, other):\n        return False\n\nt = Token()\nprint(t in [t])\nprint(Token() in [t])","takeaways":["x in a list tests identity before ==, so a custom __eq__ can't stop an object matching itself."]}]