-
web.groovymark@gmail.com
- December 8, 2024
Question 21
Which of the following can be used to loop through the keys and values of a dictionary?
a) for key, value in x
b) for key, value in x.items()
c) for key, value in x.values()
d) for key in x.keys()
Correct Answer: b) for key, value in x.items()
Explanation: The items() method returns a view object that contains the key-value pairs, which can be used to iterate through both keys and values.
Question 22
What will this Python code output?
x = {“a”: 1, “b”: 2}
for key in x:
print(key)
a) “a”
b) “b”
c) “a” and “b”
d) Error
Correct Answer: c) "a" and "b"
Explanation: Iterating through a dictionary by default gives the keys.
Question 23
How can you check if a key exists in a dictionary x?
a) key in x
b) x.has_key(key)
c) key in x.keys()
d) x.find(key)
Correct Answer: a) key in x
Explanation: The in operator checks if the specified key exists in the dictionary.
Question 24
What is the result of this Python code?
x = {“a”: 1, “b”: 2}
print(“c” in x)
a) True
b) False
c) Error
d) None
Correct Answer: b) False
Explanation: The in operator checks if the key "c" exists in the dictionary, which it does not.
Question 25
Which method can be used to remove all items from a dictionary in Python?
a) clear()
b) del()
c) remove()
d) delete()
Correct Answer: a) clear()
Explanation: The clear() method removes all items from the dictionary.
Question 26
What is the output of this Python code?
x = {“a”: 1, “b”: 2}
x.clear()
print(x)
a) {}
b) {“a”: 1, “b”: 2}
c) None
d) Error
Correct Answer: a) {}
Explanation: The clear() method empties the dictionary.
Question 27
How do you combine two dictionaries in Python 3.9+?
a) x + y
b) x | y
c) x & y
d) x.add(y)
Correct Answer: b) x | y
Explanation: In Python 3.9 and later, you can use the | operator to merge two dictionaries.
Question 28
What will this Python code output?
x = {“a”: 1, “b”: 2}
y = {“b”: 3, “c”: 4}
z = x | y
print(z)
a) {“a”: 1, “b”: 2, “c”: 4}
b) {“a”: 1, “b”: 3, “c”: 4}
c) {“a”: 1, “b”: 2, “b”: 3, “c”: 4}
d) Error
Correct Answer: b) {"a": 1, "b": 3, "c": 4}
Explanation: The | operator merges the dictionaries, with values from y overriding values from x.
Question 29
What will this Python code output?
x = (1, 2, 3)
y = x + (4, 5)
print(y)
a) (1, 2, 3)
b) (1, 2, 3, 4, 5)
c) Error
d) (4, 5)
Correct Answer: b) (1, 2, 3, 4, 5)
Explanation: You can concatenate tuples using the + operator.
Question 30
Which of the following is a valid way to define a tuple in Python?
a) x = (1,)
b) x = 1,
c) x = tuple([1, 2])
d) All of the above
Correct Answer: d) All of the above
Explanation: All these are valid ways to define a tuple in Python.
Question 31
What will this Python code output?
x = (1, 2, 3)
y = list(x)
y.append(4)
x = tuple(y)
print(x)
a) (1, 2, 3)
b) (1, 2, 3, 4)
c) Error
d) [1, 2, 3, 4]
Correct Answer: b) (1, 2, 3, 4)
Explanation: The tuple x is converted to a list to append an element, then converted back to a tuple.
Question 32
How can you access the first element of a tuple x?
a) x.get(0)
b) x[1]
c) x[0]
d) x.first()
Correct Answer: c) x[0]
Explanation: Tuples are indexed starting from 0, so x[0] gives the first element.
Question 33
What is the result of this Python code?
x = (1, 2, 3)
print(x.index(2))
a) 0
b) 1
c) 2
d) Error
Correct Answer: b) 1
Explanation: The index() method returns the index of the specified element, in this case, 2 is at index 1.
Question 34
How can you determine the length of a tuple x?
a) x.size()
b) x.length()
c) len(x)
d) x.count()
Correct Answer: c) len(x)
Explanation: The len() function returns the number of elements in a tuple.
Question 35
What will this Python code output?
x = (1, 2, 3)
y = x * 2
print(y)
a) (1, 2, 3)
b) (1, 2, 3, 1, 2, 3)
c) Error
d) None of the above
Correct Answer: a) [1, 2, 3] [1, 2, 3, 4]
Explanation: The copy() method creates a shallow copy of the list x. Modifying the copied list y does not affect the original list x.
Question 36
What will this Python code output?
x = (1, 2, 3)
y = x * 2
print(y)
a) (1, 2, 3)
b) (1, 2, 3, 1, 2, 3)
c) Error
d) None of the above
Correct Answer: b) (1, 2, 3, 1, 2, 3)
Explanation: The * operator repeats the tuple elements, so the tuple (1, 2, 3) is repeated twice.
Question 37
What is the output of this code?
x = (1, 2, 3)
print(x[-1])
a) 1
b) 3
c) Error
d) None
Correct Answer: b) 3
Explanation: Negative indexing starts from the end of the tuple, so x[-1] gives the last element, which is 3.
Question 38
What will this Python code output?
x = (1, 2, 3, 2, 2)
print(x.count(2))
a) 1
b) 2
c) 3
d) Error
Correct Answer: c) 3
Explanation: The count() method returns the number of times the value 2 appears in the tuple, which is 3.
Question 39
What will this Python code output?
x = {1, 2, 3}
y = {3, 4, 5}
z = x & y
print(z)
a) {3}
b) {1, 2, 3, 4, 5}
c) {1, 2, 4, 5}
d) Error
Correct Answer: a) {3}
Explanation: The & operator gives the intersection of two sets, so {3} is the common element.
Question 40
How can you check if x is a subset of y in Python sets?
a) x.contains(y)
b) x <= y
c) subset(x, y)
d) x in y
Correct Answer: b) x <= y
Explanation: The <= operator checks if a set is a subset of another set.