Python - MCQ Questions and Answers

Test your knowledge of Python with these interactive multiple-choice questions.

« First 7 8 9 10 Last »
« Previous Page Next Page »

161. What is the output of print(type(lambda x: x))?

  • a) <class 'function'>
  • b) <class 'lambda'>
  • c) <class 'expression'>
  • d) Error
Answer: A - Lambda functions are regular function objects in Python.

162. Which keyword is used to define a function?

  • a) def
  • b) function
  • c) lambda
  • d) func
Answer: A - Functions are defined using def function_name():.

163. What does print((lambda x: x*2)(5)) output?

  • a) 10
  • b) 25
  • c) 55
  • d) Error
Answer: A - The lambda function doubles its input (5*2=10).

164. Which statement immediately exits a function?

  • a) return
  • b) exit
  • c) break
  • d) quit
Answer: A - return exits the function and optionally returns a value.

165. What is the output of def f(x=[]): x.append(1); return x; print(f() + f())?

  • a) [1, 1, 1, 1]
  • b) [1, 1]
  • c) [1] [1]
  • d) Error
Answer: A - Mutable default arguments retain changes between calls.

166. Which symbol is used for unpacking argument lists?

  • a) *
  • b) **
  • c) &
  • d) @
Answer: A - * unpacks iterables, ** unpacks dictionaries.

167. What is the output of print([x for x in range(3)])?

  • a) [0, 1, 2]
  • b) [1, 2, 3]
  • c) range(0, 3)
  • d) Error
Answer: A - List comprehension generates a list from the range.

168. Which decorator converts a method to a static method?

  • a) @staticmethod
  • b) @classmethod
  • c) @property
  • d) @static
Answer: A - @staticmethod doesn't receive implicit first argument.

169. What does print(list(map(lambda x: x**2, [1,2,3]))) output?

  • a) [1, 4, 9]
  • b) [1, 2, 3]
  • c) [2, 4, 6]
  • d) Error
Answer: A - map() applies the lambda to square each number.

170. Which function applies a function to pairs of elements?

  • a) reduce()
  • b) map()
  • c) filter()
  • d) apply()
Answer: A - reduce() cumulatively applies a function (from functools in Python 3).

171. What is the output of print([x for x in range(5) if x%2==0])?

  • a) [0, 2, 4]
  • b) [1, 3, 5]
  • c) [0, 1, 2, 3, 4]
  • d) Error
Answer: A - List comprehension with condition filters even numbers.

172. Which decorator converts a method to a class method?

  • a) @classmethod
  • b) @staticmethod
  • c) @property
  • d) @class
Answer: A - @classmethod receives the class as first argument.

173. What is the output of print(list(filter(lambda x: x>0, [-1,0,1,2])))?

  • a) [1, 2]
  • b) [-1, 0]
  • c) [0, 1, 2]
  • d) Error
Answer: A - filter() keeps only elements where lambda returns True.

174. Which function creates an iterator of tuples from multiple iterables?

  • a) zip()
  • b) map()
  • c) enumerate()
  • d) combine()
Answer: A - zip([1,2], ['a','b']) yields (1,'a'), (2,'b').

175. What does print({x:x**2 for x in range(3)}) output?

  • a) {0:0, 1:1, 2:4}
  • b) [0, 1, 4]
  • c) {0,1,4}
  • d) Error
Answer: A - Dictionary comprehension creates key-value pairs.

176. Which function adds a counter to an iterable?

  • a) enumerate()
  • b) count()
  • c) zip()
  • d) index()
Answer: A - enumerate(['a']) yields (0,'a').

177. What is the output of print((x**2 for x in range(3)))?

  • a) <generator object>
  • b) [0, 1, 4]
  • c) (0, 1, 4)
  • d) Error
Answer: A - Parentheses create a generator expression, not a tuple.

178. Which function returns a list of results from applying a function?

  • a) map()
  • b) apply()
  • c) filter()
  • d) reduce()
Answer: A - map() applies a function to each item in an iterable.

179. What does print(sorted([3,1,2], reverse=True)) output?

  • a) [3, 2, 1]
  • b) [1, 2, 3]
  • c) [3, 1, 2]
  • d) Error
Answer: A - reverse=True sorts in descending order.

180. Which function returns a list of elements where function returns True?

  • a) filter()
  • b) map()
  • c) reduce()
  • d) select()
Answer: A - filter() includes only elements that satisfy the condition.
« First 7 8 9 10 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.