Saturday 20 January 2024

Top 20 Python Dictionary Questions with answer

 


Question 1:

What is a dictionary in Python?

a) A collection of ordered elements

b) A collection of unordered elements

c) A single element

d) A data type


Question 2:

How do you create an empty dictionary in Python?

a) empty_dict = {}

b) empty_dict = dict()

c) empty_dict = new dict()

d) Both a and b


Question 3:

How do you access the value associated with a specific key in a dictionary?

a) dictionary.value(key)

b) dictionary[key]

c) dictionary.get(key)

d) dictionary.retrieve(key)


Question 4:

What is the purpose of the len() function when used with a dictionary?

a) It returns the total number of key-value pairs in the dictionary

b) It returns the last key in the dictionary

c) It returns the length of each value in the dictionary

d) It returns the sum of all values in the dictionary


Question 5:

How do you add a new key-value pair to a dictionary?

a) dictionary.add(key, value)

b) dictionary[key] = value

c) dictionary.insert(key, value)

d) dictionary.append(key, value)


Question 6:

What is the key difference between a dictionary and a list in Python?

a) Dictionaries are ordered, while lists are unordered

b) Dictionaries are mutable, while lists are immutable

c) Dictionaries can contain only numeric elements

d) Dictionaries are unordered and do not allow duplicate keys


Question 7:

How do you check if a key is present in a dictionary?

a) key in dictionary

b) dictionary.contains(key)

c) dictionary.exists(key)

d) key.exists(dictionary)


Question 8:

What does the dictionary.keys() method return?

a) The values of the dictionary

b) The keys of the dictionary

c) The key-value pairs of the dictionary

d) The length of the dictionary


Question 9:

How do you remove a key-value pair from a dictionary?

a) dictionary.remove(key)

b) dictionary.discard(key)

c) dictionary.delete(key)

d) All of the above


Question 10:

Which method is used to retrieve the value associated with a key, and if the key is not present, it returns a default value?

a) dictionary.get(key, default)

b) dictionary.retrieve(key, default)

c) dictionary.value(key, default)

d) dictionary.fetch(key, default)


Question 11:

What is the purpose of the pop() method in Python dictionaries?

a) Adds an element to the dictionary

b) Removes the last element from the dictionary and returns its value

c) Removes the first occurrence of the specified element

d) Removes the key-value pair for a specified key


Question 12:

How do you update the value associated with a key in a dictionary?

a) dictionary.update(key, new_value)

b) dictionary[key] = new_value

c) dictionary.modify(key, new_value)

d) dictionary.change_value(key, new_value)


Question 13:

What is the output of the following code?

my_dict = {"a": 1, "b": 2, "c": 3}

del my_dict["b"]

print(my_dict)

a) {"a": 1, "b": 2, "c": 3}

b) {"a": 1, "c": 3}

c) {"a": 1, "b": 2}

d) Raises an error


Question 14:

How do you iterate over the keys of a dictionary?

a) for key in dictionary.keys():

b) for key in dictionary:

c) for key in dictionary.values():

d) for key in dictionary.items():


Question 15:

What is the purpose of the values() method in Python dictionaries?

a) Returns the keys of the dictionary

b) Returns the values of the dictionary

c) Returns the key-value pairs of the dictionary

d) Returns the length of the dictionary


Question 16:

Which method is used to clear all key-value pairs from a dictionary?

a) dictionary.clear()

b) dictionary.remove_all()

c) dictionary.delete()

d) dictionary.empty()


Question 17:

What is the purpose of the items() method in Python dictionaries?

a) Returns the keys of the dictionary

b) Returns the values of the dictionary

c) Returns the key-value pairs of the dictionary

d) Returns the length of the dictionary


Question 18:

What is the output of the following code?

my_dict = {"apple": 3, "banana": 5, "cherry": 2}

sorted_dict = dict(sorted(my_dict.items()))

print(sorted_dict)

a) {"apple": 3, "banana": 5, "cherry": 2}

b) {"cherry": 2, "apple": 3, "banana": 5}

c) {"banana": 5, "apple": 3, "cherry": 2}

d) Raises an error


Question 19:

How do you create a dictionary with keys as numbers from 1 to 5 and values as their squares?

a) squares = {i: i ** 2 for i in range(1, 6)}

b) squares = {i: i * i for i in range(1, 6)}

c) squares = {i: i ** 2 for i in [1, 2, 3, 4, 5]}

d) All of the above


Question 20:

What is the purpose of the copy() method in Python dictionaries?

a) Creates a shallow copy of the dictionary

b) Creates a deep copy of the dictionary

c) Returns the reversed dictionary

d) Appends a copy of the dictionary to itself


Answer Key:

  1. b) A collection of unordered elements
  2. d) Both a and b
  3. b) dictionary[key]
  4. a) It returns the total number of key-value pairs in the dictionary
  5. b) dictionary[key] = value
  6. d) Dictionaries are unordered and do not allow duplicate keys
  7. a) key in dictionary
  8. b) The keys of the dictionary
  9. d) All of the above
  10. a) dictionary.get(key, default)
  11. b) Removes the last element from the dictionary and returns its value
  12. b) dictionary[key] = new_value
  13. b) {"a": 1, "c": 3}
  14. b) for key in dictionary:
  15. b) Returns the values of the dictionary
  16. a) dictionary.clear()
  17. c) Returns the key-value pairs of the dictionary
  18. b) {"cherry": 2, "apple": 3, "banana": 5}
  19. a) squares = {i: i ** 2 for i in range(1, 6)}
  20. a) Creates a shallow copy of the dictionary

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (117) C (77) C# (12) C++ (82) Course (62) Coursera (179) coursewra (1) Cybersecurity (22) data management (11) Data Science (95) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (748) Python Coding Challenge (221) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses