🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Welcome to this Python Dictionary Quiz designed for beginners! This quiz covers the basics of dictionaries in Python, a powerful and versatile data structure essential for storing and managing data pairs. Whether you're just starting out or looking to refresh your knowledge, these questions will help you understand the fundamentals of Python dictionaries. Let's get started!
1. What is a Python dictionary?
Answer:
Explanation:
A Python dictionary is a collection of key-value pairs where each key is unique and is used to store and retrieve values.
2. How do you access the value associated with a key in a dictionary?
Answer:
Explanation:
In Python dictionaries, values are accessed by specifying the key in square brackets, like dict[key].
3. Which of the following is the correct way to create an empty dictionary?
Answer:
Explanation:
An empty dictionary in Python is created using curly braces {}, not square brackets [] or parentheses ().
4. What will be the output of the following code?
my_dict = {'a':1, 'b':2, 'c':3}
print(my_dict['b'])
Answer:
Explanation:
This code accesses the value associated with the key 'b' in the dictionary, which is 2.
5. What is the result of the following operation?
'a' in {'a': 1, 'b': 2}
Answer:
Explanation:
The 'in' operator checks if the key 'a' is present in the dictionary, which it is, so it returns True.
6. How do you add a new key-value pair to a dictionary?
Answer:
Explanation:
A new key-value pair is added to a dictionary by assigning a value to a new key using dict[key] = value.
7. What will be the output of the following code?
my_dict = {'a':1, 'b':2, 'c':3}
print(len(my_dict))
Answer:
Explanation:
The len() function returns the number of key-value pairs in the dictionary, which is 3 in this case.
8. How can you remove a key-value pair from a dictionary?
Answer:
Explanation:
The del statement is used to remove a key-value pair from a dictionary by specifying the key.
9. Which method is used to get the value for a key or return a default if the key is not found?
Answer:
Explanation:
The get() method is used to retrieve the value of a specified key or return a default value if the key is not found.
10. What will be the output of the following code?
my_dict = {'a':1, 'b':2, 'c':3}
print(my_dict.get('d', 4))
Answer:
Explanation:
Since 'd' is not a key in the dictionary, the get() method returns the default value 4.
11. What type of keys can be used in a Python dictionary?
Answer:
Explanation:
Keys in a Python dictionary can be any immutable type, such as strings, numbers, and tuples.
12. What will be the output of the following code?
my_dict = {'a': 1, 'b': 2}
my_dict['a'] = 10
print(my_dict)
Answer:
Explanation:
Assigning a new value to an existing key in a dictionary updates the value associated with that key.
13. How do you get a list of all keys in a dictionary?
Answer:
Explanation:
The keys() method returns a view object displaying a list of all the keys in the dictionary.
14. What will happen if you try to access a key that doesn’t exist in a dictionary?
Answer:
Explanation:
Accessing a non-existent key in a dictionary raises a KeyError.
15. What does the pop() method do in a dictionary?
Answer:
Explanation:
The pop() method removes the key-value pair with the specified key and returns its value.
16. Which method is used to remove all items from a dictionary?
Answer:
Explanation:
The clear() method removes all items from the dictionary, leaving it empty.
17. How are duplicate keys handled in a Python dictionary?
Answer:
Explanation:
If a duplicate key is used in a dictionary, the new value overwrites the existing value associated with that key.
18. What is the output of the following code?
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['d'])
Answer:
Explanation:
Trying to access a key that doesn't exist in the dictionary ('d' in this case) results in a KeyError.
19. What will be the output of the following code?
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.update({'c': 4, 'd': 5})
print(my_dict)
Answer:
Explanation:
The update() method updates the dictionary with elements from another dictionary, adding new elements and updating existing ones.
20. What does the items() method return in a Python dictionary?
Answer:
Explanation:
The items() method returns a view object that displays a list of dictionary's key-value tuple pairs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment