Table of contents | |
Multiple Choice Questions (MCQs) | |
Higher Order Thinking Questions (HOTS) | |
Fill in the Blanks | |
True or False | |
Hands-On Questions |
Q.1. Which of the following is an immutable data structure in Python?
(a) List
(b) Tuple
(c) Set
(d) Dictionary
Ans. (b)
Q.2. What is the main difference between a list and a tuple in Python?
(a) A list is mutable, whereas a tuple is immutable.
(b) A list can store elements of different data types, whereas a tuple can only store elements of the same data type.
(c) A list can have a variable length, whereas a tuple has a fixed length.
(d) All of the above.
Ans. (d)
Q.3. Which of the following is an example of a set in Python?
(a) {1, 2, 3}
(b) [1, 2, 3]
(c) (1, 2, 3)
(d) {"one": 1, "two": 2, "three": 3}
Ans. (a)
Q.4. What is the purpose of a dictionary in Python?
(a) To store elements in a specific order.
(b) To store elements with a key-value pair.
(c) To store elements that are unique and unordered.
(d) To store elements that can be modified.
Ans. (b)
Q.5. Which of the following operations is not supported by a tuple in Python?
(a) Indexing
(b) Slicing
(c) Appending
(d) Concatenation
Ans. (c)
Q.1. Write a Python program to create a tuple with three elements. Perform the following operations on the tuple:
Example Python program:
my_tuple = (10, 20, 30)print(my_tuple[1]) # Accessing the second element
print(len(my_tuple)) # Calculating the length of the tuple
print(40 in my_tuple) # Checking if 40 is present in the tuple
Q.2. Write a Python program to remove duplicate elements from a given tuple.
Example Python program:
my_tuple = (10, 20, 10, 30, 40, 20)unique_tuple = tuple(set(my_tuple))
print(unique_tuple)
Q.3. Explain the concept of set intersection and set difference operations with an example.
Set intersection and set difference operations:
Set Intersection: The intersection of two sets contains the common elements present in both sets. It can be performed using the intersection() method or the & operator.
Example:
set1 = {1, 2, 3, 4}set2 = {3, 4, 5, 6}
intersection = set1.intersection(set2)
print(intersection) # Output: {3, 4}
Set Difference: The difference between two sets contains the elements that are present in one set but not in the other. It can be performed using the difference() method or the - operator.
Example:
set1 = {1, 2, 3, 4}set2 = {3, 4, 5, 6}
difference = set1.difference(set2)
print(difference) # Output: {1, 2}
Q.4. Write a Python program to count the frequency of each character in a given string and store the results in a dictionary.
Example Python program:
def count_characters(string):char_count = {}
for char in string:
char_count[char] = char_count.get(char, 0) + 1
return char_count
my_string = "Hello, Python!"
result = count_characters(my_string)
print(result)
Q.5. Write a Python function that takes a dictionary as input and returns the key with the maximum value.
def find_max_value_key(my_dict):
max_value = max(my_dict.values())
for key, value in my_dict.items():
if value == max_value:
return key
my_dict = {"apple": 3, "banana": 5, "orange": 2}
result = find_max_value_key(my_dict)
print(result)
Q.1. A tuple is a ___________ data structure in Python.
Ans. immutable
Q.2. In a set, the elements are ________ and ________.
Ans. unique, unordered
Q.3. The keys in a dictionary are ________ and ________, whereas the values can be ________.
Ans. unique, immutable, mutable
Q.4. A tuple can be defined using ________ brackets.
Ans. parentheses
Q.5. The ________ function can be used to convert a list into a set.
Ans. set()
Q.1. Tuples are immutable and cannot be modified once created.
Ans. True
Q.2. Sets in Python can contain duplicate elements.
Ans. False
Q.3. Dictionaries in Python preserve the order of elements.
Ans. False
Q.4. A list is an ordered collection of elements.
Ans. True
Q.5. In Python, a dictionary can have multiple values for the same key.
Ans. True
Q.1. Create a tuple named "fruits" with three different fruit names. Print the tuple.
Example Python program:
fruits = ("apple", "banana", "orange")print(fruits)
Q.2. Create a set named "numbers" with five integer values. Print the set.
Example Python program:
numbers = {1, 2, 3, 4, 5}print(numbers)
Q.3. Create a dictionary named "student" with the following key-value pairs:
Example Python program:
student = {"name": "John",
"age": 20,
"grade": "A"
}
print(student)
Q.4. Given the following dictionary:
my_dict = {"apple": 3, "banana": 5, "orange": 2}
Write a program to calculate the total number of fruits.
Example Python program:
my_dict = {"apple": 3, "banana": 5, "orange": 2}total_fruits = sum(my_dict.values())
print(total_fruits)
Q.5. Write a Python function named "common_elements" that takes two sets as input and returns a new set containing the common elements from both sets.
Example Python function:
def common_elements(set1, set2):
return set1.intersection(set2)
set1 = {1, 2, 3}
set2 = {2, 3, 4}
result = common_elements(set1, set2)
print(result)
49 videos|38 docs|18 tests
|
|
Explore Courses for Software Development exam
|