Built-ins & Standard Library questions and answers

sorted, zip, any/all, collections, itertools and imports. Practise built-ins & standard library questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample built-ins & standard library questions with worked answers

Question 1

A program starts with the line import math as m. Which call correctly computes the square root of 16?

  1. math.sqrt(16)
  2. sqrt(16)
  3. m.sqrt(16)
  4. m.math.sqrt(16)

Answer: C. m.sqrt(16)

Explanation: import math as m binds the module only to the name m. The names math and sqrt are not defined in the program, so both would raise NameError.

Question 2

A file tools.py ends with a block under if __name__ == "__main__":. When does that block run?

  1. Only when tools.py is run directly, not when it is imported by another file
  2. Only when tools.py is imported by another file
  3. Every time, whether tools.py is run or imported
  4. Only when tools.py defines a function called main

Answer: A. Only when tools.py is run directly, not when it is imported by another file

Explanation: Python sets __name__ to "__main__" in the file that is being run directly. When the file is imported, __name__ is the module name "tools", so the condition is False and the block is skipped.

Question 3

A program imports the same module helper from two different files. How many times is helper's top-level code executed in one run of the program?

  1. Once; later imports reuse the already loaded module
  2. Twice, once for each import statement
  3. Never, until one of its functions is called
  4. Once per function that the importing files use

Answer: A. Once; later imports reuse the already loaded module

Explanation: The first import runs the module's code and stores the module object in sys.modules. Every later import of the same name simply fetches that stored object.

All 20 built-ins & standard library questions with answers

Built-ins & Standard Library: common questions

What are built-ins & standard library questions?

sorted, zip, any/all, collections, itertools and imports. They appear in the python section of many placement tests.

Where can I practise built-ins & standard library questions with answers for free?

Right here on LogicWiz Exams. Every built-ins & standard library question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise built-ins & standard library?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of built-ins & standard library questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.

More python topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep