Python questions and answers
Data types, comprehensions and mutability. Practise python questions from the technical & programming section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.
Sample python questions with worked answers
Question 1
Which of the following Python built-in types is immutable?
- list
- tuple
- dict
- set
Answer: B. tuple
Explanation: A tuple cannot be changed after creation (no item assignment, append or remove). list, dict and set can all be modified in place.
Question 2
What will be the output of the following Python code?
print([x * x for x in range(6) if x % 2])
- [0, 4, 16]
- [1, 9, 25]
- [1, 9, 25, 49]
- [0, 1, 4, 9, 16, 25]
Answer: B. [1, 9, 25]
Explanation: range(6) gives 0 to 5. x % 2 is truthy only for odd x (1, 3, 5), and their squares are 1, 9 and 25.
Question 3
What will be the output of the following Python code?
fs = [lambda: i for i in range(3)]
print([f() for f in fs])
- [0, 1, 2]
- [0, 0, 0]
- [2, 2, 2]
- NameError
Answer: C. [2, 2, 2]
Explanation: Each lambda looks up i when it is called, not when it is created (late binding). By then the comprehension has finished with i = 2, so all three return 2.
All 30 python questions with answers
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- Which of the following Python built-in types is immutable?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
Python: common questions
What are python questions?
Data types, comprehensions and mutability. They appear in the technical & programming section of many placement tests.
Where can I practise python questions with answers for free?
Right here on LogicWiz Exams. Every python question has a worked explanation after you answer. Practice is free and needs no sign-up.
How should I practise python?
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 python questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Technical & Programming practice.
More technical & programming topics
- Pseudo-code: Trace loops, recursion and bitwise logic.
- C Programming: Output prediction, pointers and operators.
- Java: OOP, strings, collections and exceptions.
- Data Structures: Arrays, stacks, queues, trees, graphs and hashing.
- Algorithms: Complexity, sorting, searching, recursion and DP.
- Object-Oriented Programming: Encapsulation, inheritance, polymorphism, abstraction.
- DBMS: Keys, normalisation, transactions and indexing.
- SQL: Joins, grouping, subqueries and query output.
- Operating Systems: Processes, scheduling, memory and deadlocks.
- Computer Networks: OSI/TCP-IP, protocols, addressing and ports.
- Software Engineering: SDLC, testing, agile and version control.
- Cloud & DevOps: Service models, containers, CI/CD and Linux.
- GenAI Basics: LLMs, tokens, prompting, embeddings and RAG.
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.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
- 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