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 »

141. What is the output of print(len({'a':1, 'b':2}))?

  • a) 2
  • b) 4
  • c) {'a':1, 'b':2}
  • d) Error
Answer: A - len() returns the number of key-value pairs in a dictionary.

142. Which method returns a set of dictionary keys?

  • a) keys()
  • b) get_keys()
  • c) items()
  • d) values()
Answer: A - my_dict.keys() returns a view of all keys.

143. What does print({'a', 'b'} - {'b', 'c'}) output?

  • a) {'a'}
  • b) {'c'}
  • c) {'a', 'c'}
  • d) set()
Answer: A - The - operator performs set difference (elements in first set but not in second).

144. Which method returns a default value if a key doesn't exist?

  • a) get()
  • b) setdefault()
  • c) default()
  • d) find()
Answer: A - my_dict.get(key, default) returns default if key is missing.

145. What is the output of print({'a':1} == {'a':1.0})?

  • a) True
  • b) False
  • c) None
  • d) Error
Answer: A - Dictionary comparison considers 1 and 1.0 as equal values.

146. Which method removes and returns an arbitrary key-value pair?

  • a) popitem()
  • b) pop()
  • c) remove()
  • d) discard()
Answer: A - my_dict.popitem() removes and returns a random item (Python 3.7+ returns last inserted).

147. What is the output of print(len(set('hello')))?

  • a) 4
  • b) 5
  • c) 1
  • d) Error
Answer: A - Sets remove duplicates, so 'hello' becomes {'h','e','l','o'} (length 4).

148. Which operator performs set intersection?

  • a) &
  • b) |
  • c) -
  • d) ^
Answer: A - The & operator returns elements common to both sets.

149. What does print({'a':1}.update({'b':2})) output?

  • a) None
  • b) {'a':1, 'b':2}
  • c) {'b':2}
  • d) Error
Answer: A - update() modifies the dictionary in-place and returns None.

150. Which method creates a dictionary from two sequences?

  • a) dict(zip(keys, values))
  • b) dict.fromkeys(keys, values)
  • c) dict.combine(keys, values)
  • d) dict.map(keys, values)
Answer: A - dict(zip(['a','b'], [1,2])) creates {'a':1, 'b':2}.

151. What is the output of print({'a', 'b'} | {'b', 'c'})?

  • a) {'a', 'b', 'c'}
  • b) {'b'}
  • c) {'a', 'c'}
  • d) set()
Answer: A - The | operator performs set union (all unique elements from both sets).

152. Which method returns a new dictionary with default values?

  • a) fromkeys()
  • b) setdefault()
  • c) defaults()
  • d) newdict()
Answer: A - dict.fromkeys(['a','b'], 0) creates {'a':0, 'b':0}.

153. What is the output of print('a' in {'a':1})?

  • a) True
  • b) False
  • c) 1
  • d) Error
Answer: A - The in operator checks for key existence in dictionaries.

154. Which method removes a key and returns its value?

  • a) pop()
  • b) remove()
  • c) delete()
  • d) discard()
Answer: A - my_dict.pop('a') removes 'a' and returns its value.

155. What does print({'a':1}.copy()) output?

  • a) {'a':1}
  • b) ['a',1]
  • c) ('a',1)
  • d) Error
Answer: A - copy() creates a shallow copy of the dictionary.

156. Which operator performs symmetric difference (elements in either set but not both)?

  • a) ^
  • b) &
  • c) |
  • d) -
Answer: A - The ^ operator returns elements in either set but not in both.

157. What is the output of print(bool(set()))?

  • a) False
  • b) True
  • c) None
  • d) Error
Answer: A - Empty sets are falsy in Python.

158. Which method adds an element to a set?

  • a) add()
  • b) append()
  • c) insert()
  • d) push()
Answer: A - my_set.add(x) adds x to the set.

159. What does print({'a':1}.get('b', 2)) output?

  • a) 2
  • b) None
  • c) 1
  • d) Error
Answer: A - Returns the default value (2) when key 'b' doesn't exist.

160. Which method removes an element from a set without raising an error if missing?

  • a) discard()
  • b) remove()
  • c) pop()
  • d) delete()
Answer: A - my_set.discard(x) removes x if present (no error if not found).
« 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.