Which keyword is used to define a function in Python?
- A. define
- B. fun
- C. def (correct)
- D. function
What is the output of print(type([]))?
- A. <class 'tuple'>
- B. <class 'dict'>
- C. <class 'str'>
- D. <class 'list'> (correct)
Which of the following data types is immutable?
- A. list
- B. dict
- C. tuple (correct)
- D. set
What does len("Python") return?
- A. 5
- B. 6 (correct)
- C. 7
- D. Error
Which symbol is used for single-line comments in Python?
- A. //
- B. /* */
- C. --
- D. # (correct)
What is the output of 7 // 2?
- A. 3.5
- B. 4
- C. 3 (correct)
- D. 3.0
What is the output of 2 ** 3?
- A. 6
- B. 8 (correct)
- C. 9
- D. 5
Which method adds an item to the end of a list?
- A. append() (correct)
- B. add()
- C. push()
- D. insert_end()
Which of the following creates an empty dictionary?
- A. []
- B. ()
- C. {} (correct)
- D. dict[]
What is the output of bool("")?
- A. True
- B. False (correct)
- C. None
- D. Error
Which keyword is used to handle exceptions in Python?
- A. catch
- B. handle
- C. except (correct)
- D. rescue
What sequence does range(5) generate?
- A. 0, 1, 2, 3, 4 (correct)
- B. 1, 2, 3, 4, 5
- C. 0, 1, 2, 3, 4, 5
- D. 1, 2, 3, 4
What is the output of "abc".upper()?
- A. abc
- B. Abc
- C. ABC (correct)
- D. Error
Which function is used to read input from the user?
- A. scanf()
- B. read()
- C. get()
- D. input() (correct)
What is the output of 10 % 3?
- A. 0
- B. 3
- C. 1 (correct)
- D. 3.33
What does [x * 2 for x in range(3)] return?
- A. [0, 2, 4] (correct)
- B. [2, 4, 6]
- C. [0, 1, 2]
- D. [0, 2, 4, 6]
Which keyword is used to create a class in Python?
- A. struct
- B. object
- C. define
- D. class (correct)
What is the purpose of the __init__ method in a class?
- A. It destroys the object
- B. It initializes a new object (constructor) (correct)
- C. It imports a module
- D. It defines a static method
What is the output of print("Hi" * 3)?
- A. Hi3
- B. Error
- C. HiHiHi (correct)
- D. Hi Hi Hi
Which statement skips the rest of the current loop iteration?
- A. break
- B. pass
- C. continue (correct)
- D. exit
Which is the correct way to import the math module?
- A. include math
- B. using math
- C. import math (correct)
- D. #import math
What is the output of type(3 / 2)?
- A. <class 'int'>
- B. <class 'float'> (correct)
- C. <class 'double'>
- D. <class 'decimal'>
What does the pass statement do?
- A. Does nothing (acts as a placeholder) (correct)
- B. Exits the program
- C. Skips the loop iteration
- D. Raises an error
What is the output of len({1, 2, 2, 3})?
- A. 4
- B. 3 (correct)
- C. 2
- D. Error
What is the output of [1, 2, 3][-1]?
- A. 1
- B. Error
- C. 2
- D. 3 (correct)
Which keyword is used to create an anonymous function?
- A. lambda (correct)
- B. anon
- C. func
- D. def
Which string method removes whitespace from both ends?
- A. strip() (correct)
- B. trim()
- C. clean()
- D. cut()
What does *args do in a function definition?
- A. Passes keyword arguments
- B. Accepts any number of positional arguments (correct)
- C. Unpacks a dictionary
- D. Multiplies all arguments
What is the output of: a = [1, 2, 3]; b = a; b.append(4); print(a)?
- A. [1, 2, 3]
- B. [1, 2, 3, 4] (correct)
- C. Error
- D. [4]
What is a decorator in Python?
- A. A comment style
- B. A type of loop
- C. A class attribute
- D. A function that modifies the behavior of another function (correct)
Which statement about the GIL in CPython is true?
- A. It allows unlimited threads to run Python bytecode in parallel
- B. It only affects Python 2
- C. It lets only one thread execute Python bytecode at a time (correct)
- D. It speeds up multiprocessing
What is the difference between == and is?
- A. == compares identity, is compares values
- B. They are identical
- C. == compares values, is compares object identity (correct)
- D. is only works with numbers
What does the yield keyword do?
- A. Ends the program
- B. Defines a class
- C. Imports a module
- D. Turns a function into a generator (correct)
What is the output of print(0.1 + 0.2 == 0.3)?
- A. True
- B. False (correct)
- C. Error
- D. None
Given def f(x, l=[]): l.append(x); return l — what does f(1); print(f(2)) output?
- A. [2]
- B. [1]
- C. [1, 2] (correct)
- D. Error
Which function returns the number of items in a list?
- A. size()
- B. count()
- C. length()
- D. len() (correct)
Which data type is created by writing (1, 2, 3)?
- A. dict
- B. set
- C. tuple (correct)
- D. list
Which data type is created by writing {1, 2, 3}?
- A. dict
- B. set (correct)
- C. tuple
- D. list
What does print(10 / 2) display in Python 3?
- A. 5
- B. Error
- C. 5.00
- D. 5.0 (correct)
Which keyword is used for a conditional branch after if?
- A. elseif
- B. else if
- C. elif (correct)
- D. ifelse
What does the str() function do?
- A. Converts a value to a string (correct)
- B. Checks a string
- C. Reverses a string
- D. Splits a string
What does int("42") return?
- A. 42.0
- B. "42"
- C. Error
- D. 42 (correct)
How do you start a for loop over the numbers 0 to 4?
- A. for i to 5:
- B. for i in range(5): (correct)
- C. foreach i in 5:
- D. for (i = 0; i < 5; i++):
Which operator checks if a value is in a list?
- A. in (correct)
- B. exists
- C. contains
- D. has
What does "hello"[0] return?
- A. 'o'
- B. 'h' (correct)
- C. 'hello'
- D. Error
Which method splits a string into a list of words?
- A. cut()
- B. divide()
- C. split() (correct)
- D. words()
What is the output of print(3 > 2 and 2 > 5)?
- A. Error
- B. False (correct)
- C. None
- D. True
Which built-in function returns the largest item?
- A. big()
- B. max() (correct)
- C. largest()
- D. top()
What does abs(-7) return?
- A. -7
- B. Error
- C. 0
- D. 7 (correct)
Which keyword returns a value from a function?
- A. return (correct)
- B. give
- C. output
- D. yield only
Which method removes and returns the last item of a list?
- A. cut()
- B. pop() (correct)
- C. delete()
- D. remove()
What does round(3.7) return?
- A. 4 (correct)
- B. 3.7
- C. 4.0
- D. 3
Which loop keeps running while a condition is true?
- A. repeat
- B. while (correct)
- C. for
- D. do
What does "a,b,c".split(",") return?
- A. ['a,b,c']
- B. ['a', 'b', 'c'] (correct)
- C. 'abc'
- D. ('a', 'b', 'c')
What does ", ".join(["a", "b"]) return?
- A. 'ab'
- B. 'a, b' (correct)
- C. 'a b'
- D. ['a', 'b']
What is the output of "Python"[1:4]?
- A. 'yth' (correct)
- B. 'pyth'
- C. 'Pyt'
- D. 'ytho'
What is the output of "Python"[::-1]?
- A. Error
- B. 'P'
- C. 'nohtyP' (correct)
- D. 'Python'
What does dict.get("key", 0) do if the key is missing?
- A. Raises KeyError
- B. Returns None always
- C. Returns 0 (correct)
- D. Adds the key with value 0
Which method returns all key-value pairs of a dictionary?
- A. entries()
- B. all()
- C. pairs()
- D. items() (correct)
What does enumerate(["a", "b"]) give you in a loop?
- A. Index and value pairs (correct)
- B. Only the values
- C. A dictionary
- D. Only the indexes
What does zip([1, 2], ["a", "b"]) produce when turned into a list?
- A. [[1, 2], ['a', 'b']]
- B. [(1, 'a'), (2, 'b')] (correct)
- C. {1: 'a', 2: 'b'}
- D. [1, 2, 'a', 'b']
What does {x: x * x for x in range(3)} create?
- A. (0, 1, 4)
- B. [0, 1, 4]
- C. {0, 1, 4}
- D. {0: 0, 1: 1, 2: 4} (correct)
What does **kwargs collect in a function?
- A. Extra positional arguments as a tuple
- B. Only required arguments
- C. Global variables
- D. Extra keyword arguments as a dictionary (correct)
Which statement opens a file and closes it automatically?
- A. open("f.txt").auto()
- B. with open("f.txt") as f: (correct)
- C. f = open("f.txt", close=True)
- D. file("f.txt")
Which file mode opens a file for appending?
- A. "x"
- B. "w"
- C. "a" (correct)
- D. "r"
What happens when you open an existing file with mode "w"?
- A. It opens read-only
- B. Its contents are erased first (correct)
- C. New text is added to the end
- D. An error is raised
Which exception is raised when converting int("abc")?
- A. KeyError
- B. TypeError
- C. IndexError
- D. ValueError (correct)
Which exception is raised by accessing a missing dictionary key with d["x"]?
- A. NameError
- B. KeyError (correct)
- C. IndexError
- D. ValueError
What does sorted([3, 1, 2]) return?
- A. [3, 2, 1]
- B. [1, 2, 3] as a new list (correct)
- C. It sorts in place and returns the list
- D. None
What does list.sort() return?
- A. None (correct)
- B. A copy of the list
- C. The sorted list
- D. True
What does the global keyword do inside a function?
- A. Creates a constant
- B. Makes the function public
- C. Lets the function assign to a module-level variable (correct)
- D. Imports a module
What does if __name__ == "__main__": check?
- A. Whether the file is being run directly rather than imported (correct)
- B. Whether Python is installed
- C. Whether the file is the first in a folder
- D. Whether the function is called main
Which module provides random numbers?
- A. rand
- B. numbers
- C. math
- D. random (correct)
What does any([False, True, False]) return?
- A. [True]
- B. True (correct)
- C. False
- D. 1
What does all([]) return?
- A. True (correct)
- B. False
- C. None
- D. Error
Which method adds all items from another list to the end of a list?
- A. add()
- B. extend() (correct)
- C. append()
- D. insert()
What is the output of [1, 2] + [3]?
- A. [4, 2]
- B. Error
- C. [1, 2, 3] (correct)
- D. [1, 2, [3]]
What does a generator expression like (x * 2 for x in range(3)) return?
- A. A set
- B. A generator object that produces values lazily (correct)
- C. A list
- D. A tuple
What is a shallow copy of a list?
- A. A new list whose elements still reference the same objects (correct)
- B. A list with half the elements
- C. A copy of every nested object
- D. A pointer to the same list
Which function makes a deep copy of a nested structure?
- A. dict()
- B. list.copy()
- C. copy.copy()
- D. copy.deepcopy() (correct)
What does @staticmethod mean?
- A. The method is private
- B. The method cannot change
- C. The method takes neither self nor cls (correct)
- D. The method runs once
What does @classmethod receive as its first argument?
- A. Nothing
- B. The instance (self)
- C. The module
- D. The class (cls) (correct)
What does @property do?
- A. Caches the result forever
- B. Makes an attribute private
- C. Lets a method be accessed like an attribute (correct)
- D. Makes a method static
What is the MRO in Python?
- A. The main run order
- B. The order in which classes are searched for a method in inheritance (correct)
- C. The memory release order
- D. The module reload order
What do the dunder methods __enter__ and __exit__ implement?
- A. String conversion
- B. The context manager protocol used by with (correct)
- C. Iteration
- D. Comparison
Which method makes an object usable in a for loop?
- A. __len__
- B. __call__
- C. __iter__ (correct)
- D. __str__
What does functools.lru_cache do?
- A. Caches function results for repeated arguments (correct)
- B. Sorts function arguments
- C. Logs function calls
- D. Limits recursion depth
What is the output of print([i for i in range(10) if i % 3 == 0])?
- A. [1, 4, 7]
- B. [0, 3, 6]
- C. [3, 6, 9]
- D. [0, 3, 6, 9] (correct)
What does the walrus operator := do?
- A. Swaps two variables
- B. Assigns a value as part of an expression (correct)
- C. Declares a constant
- D. Compares two values
Which statement about Python dictionaries in 3.7+ is true?
- A. They preserve insertion order (correct)
- B. They are sorted by key
- C. They allow duplicate keys
- D. They are immutable
What does asyncio mainly help with?
- A. Compiling Python
- B. Speeding up CPU-heavy math
- C. Running many IO-bound tasks concurrently in one thread (correct)
- D. Multiprocessing
What is the output of print(type(lambda: 0))?
- A. <class 'lambda'>
- B. <class 'callable'>
- C. <class 'method'>
- D. <class 'function'> (correct)
Which module is used to work with regular expressions?
- A. string
- B. re (correct)
- C. rx
- D. regex
What do *a, b = [1, 2, 3] assign to a?
- A. 1
- B. [1, 2] (correct)
- C. [1, 2, 3]
- D. (1, 2)