141. What is the output of print(len({'a':1, 'b':2}))?
- a) 2
- b) 4
- c) {'a':1, 'b':2}
- d) Error
len() returns the number of key-value pairs in a dictionary.
Test your knowledge of Python with these interactive multiple-choice questions.
141. What is the output of print(len({'a':1, 'b':2}))?
len() returns the number of key-value pairs in a dictionary.
142. Which method returns a set of dictionary keys?
my_dict.keys() returns a view of all keys.
143. What does print({'a', 'b'} - {'b', 'c'}) output?
144. Which method returns a default value if a key doesn't exist?
my_dict.get(key, default) returns default if key is missing.
145. What is the output of print({'a':1} == {'a':1.0})?
146. Which method removes and returns an arbitrary key-value pair?
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')))?
148. Which operator performs set intersection?
149. What does print({'a':1}.update({'b':2})) output?
update() modifies the dictionary in-place and returns None.
150. Which method creates a dictionary from two sequences?
dict(zip(['a','b'], [1,2])) creates {'a':1, 'b':2}.
151. What is the output of print({'a', 'b'} | {'b', 'c'})?
152. Which method returns a new dictionary with default values?
dict.fromkeys(['a','b'], 0) creates {'a':0, 'b':0}.
153. What is the output of print('a' in {'a':1})?
in operator checks for key existence in dictionaries.
154. Which method removes a key and returns its value?
my_dict.pop('a') removes 'a' and returns its value.
155. What does print({'a':1}.copy()) output?
copy() creates a shallow copy of the dictionary.
156. Which operator performs symmetric difference (elements in either set but not both)?
157. What is the output of print(bool(set()))?
158. Which method adds an element to a set?
my_set.add(x) adds x to the set.
159. What does print({'a':1}.get('b', 2)) output?
160. Which method removes an element from a set without raising an error if missing?
my_set.discard(x) removes x if present (no error if not found).