




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which statement properly imports the sqrt function from the math module? A) import sqrt B) import math.sqrt C) from math import sqrt D) from sqrt import math Answer: C Explanation: "from math import sqrt" imports only the sqrt function from the math module. Question 2. What does "import math as m" allow you to do? A) Use m instead of math to access module functions B) Import only some functions from math
C) Rename math functions D) Import math as a string Answer: A Explanation: "import math as m" allows you to refer to the math module as "m" in your code. Question 3. Which of the following will import all public names from the random module? A) import random.all B) from random import * C) random import * D) import * from random Answer: B
Question 5. What does the dir() function do when used with a module? A) Deletes the module B) Lists all attributes and functions defined in the module C) Imports another module D) Returns the file path Answer: B Explanation: dir() lists the names defined in a module. Question 6. Which variable affects the list of directories Python searches for modules? A) os.environ B) sys.argv
C) sys.path D) path.sys Answer: C Explanation: sys.path is a list of directory names that Python searches for modules. Question 7. What is the output of math.ceil(4.2)? A) 4 B) 5 C) 4. D) 3 Answer: B
Question 9. What is the purpose of math.trunc(5.99)? A) Rounds to the nearest integer B) Drops the decimal part, returning 5 C) Rounds up D) Returns 6 Answer: B Explanation: math.trunc removes the decimal part without rounding. Question 10. What will math.factorial(5) return? A) 5 B) 25 C) 120
Answer: C Explanation: The factorial of 5 is 5Ć4Ć3Ć2Ć1 = 120. Question 11. What does random.seed(10) do? A) Sets the range of random numbers B) Changes the random number algorithm C) Initializes the random number generator for reproducibility D) Increases randomness Answer: C Explanation: random.seed initializes the generator to produce the same sequence of random numbers.
B) random.item(lst) C) random.choice(lst) D) lst.random() Answer: C Explanation: random.choice() picks a random item from a sequence. Question 14. What does random.sample([1, 2, 3, 4], 2) do? A) Returns two random unique elements from the list B) Returns all elements shuffled C) Returns a random integer D) Returns the first 2 elements Answer: A
Explanation: random.sample returns k unique elements from the population. Question 15. Which function from the platform module returns the system's name (e.g., 'Windows', 'Linux')? A) platform.system() B) platform.machine() C) platform.processor() D) platform.version() Answer: A Explanation: platform.system() returns the OS type. Question 16. What does platform.machine() return?
D) platform.machine() Answer: B Explanation: platform.python_implementation() gives the Python implementation name. Question 18. How do you create a user-defined module? A) By writing code in a .txt file B) By writing code in a .py file C) By creating a folder named module D) By using the import statement Answer: B Explanation: User-defined modules are created by writing code in .py files.
Question 19. What is the purpose of the pycache directory? A) To store source code B) To store compiled bytecode of Python modules C) To store logs D) To store package names Answer: B Explanation: pycache stores the compiled bytecode files. Question 20. What is the value of name when a module is run directly? A) 'imported'
Answer: B Explanation: name is set to the module's name during import. Question 22. What is the purpose of prefixing an attribute with a single underscore (e.g., _var)? A) Makes it public B) Denotes a private or internal-use variable by convention C) Makes it global D) Makes it static Answer: B Explanation: A single underscore indicates intended internal use (not enforced).
Question 23. What does double underscore (e.g., __var) trigger in Python classes? A) Private variable (with name mangling) B) Static variable C) Public variable D) Protected variable Answer: A Explanation: Double underscores trigger name mangling for pseudo- private variables. Question 24. What is the main role of the init.py file in a package? A) To define the package's public interface B) To store bytecode
Explanation: Only directories containing init.py are recognized as packages. Question 26. Which syntax allows importing a nested module named submod in package pkg? A) import pkg.submod B) import submod.pkg C) from pkg import submod D) Both A and C Answer: D Explanation: Both "import pkg.submod" and "from pkg import submod" are valid.
Question 27. What does the statement "from. import submodule" do inside a package? A) Imports submodule from the current package B) Imports from parent package C) Imports from system path D) Causes syntax error Answer: A Explanation: A leading dot indicates a relative import from the current package. Question 28. How does Python distinguish nested packages from simple directory trees? A) By the presence of .py files