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?
- math.sqrt(16)
- sqrt(16)
- m.sqrt(16)
- 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?
- Only when tools.py is run directly, not when it is imported by another file
- Only when tools.py is imported by another file
- Every time, whether tools.py is run or imported
- 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?
- Once; later imports reuse the already loaded module
- Twice, once for each import statement
- Never, until one of its functions is called
- 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
- sum, len, max and min are applied to a list of three marks. What two numbers…
- sorted is called with reverse=True on a list. What does the code print for the…
- any and all are each given a list containing a mix of truthy and falsy values…
- A program starts with the line import math as m. Which call correctly computes…
- isinstance is given a tuple of types in one call and a single type in the…
- math.floor is applied to a negative number and math.ceil to a positive one…
- enumerate is given start=1 while looping over two names. What does the loop…
- A Counter is built from a list of drink orders and is then asked about a drink…
- Words are grouped by their first letter using defaultdict(list). What does the…
- A deque gets an appendleft, a pop and a rotate(1). What does it hold at the end?
- max is called on a dictionary of city temperatures, once plainly and once with…
- zip is used with the * operator to split a list of pairs. What does this code…
- A file tools.py ends with a block under if __name__ == "__main__":. When does…
- itertools is used to count the 2-item combinations and the 2-item permutations…
- sorted is given a key that returns a tuple, to rank students by marks (high to…
- itertools.groupby is applied directly to a string in which one letter appears…
- Booleans are used in an isinstance check, in addition and inside sum. What…
- A program imports the same module helper from two different files. How many…
- A defaultdict(int) is updated for one key, read for a second key and tested…
- all, any and max are each given an empty list, with a default supplied to max…
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
- Variables, Types & Operators: Dynamic typing, numbers, operators, precedence and truthiness.
- Python Strings: Indexing, slicing, methods, formatting and immutability.
- Lists & Tuples: Slicing, methods, copies, sorting and unpacking.
- Dictionaries & Sets: Lookups, iteration, hashable keys and set operations.
- Conditions & Loops: if/elif, for, while, range, break, continue and loop else.
- Functions & Scope: Arguments, defaults, *args, closures, lambda and recursion.
- Comprehensions & Generators: List, dict and set comprehensions, yield and lazy evaluation.
- Classes & Objects: Attributes, inheritance, super(), dunder methods and properties.
- Exceptions & Files: try/except/else/finally, raising errors and the with statement.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- SQL: SQL quizzes by topic, from SELECT and joins to window functions: query output and concepts for analyst and developer roles.
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