1.
To which of the following the “in” operator can be used to check if an item is in it?
Correct Answer
D. All of the above
Explanation
The "in" operator can be used to check if an item is in Lists, Dictionary, and Tuples. This operator checks if the item is present in the given data structure and returns a boolean value (True or False) accordingly. Therefore, the correct answer is "All of the above" as the "in" operator can be used with all three data structures.
2.
Which of the following is a Python tuple?
Correct Answer
B. (1,2,3)
Explanation
A Python tuple is a collection of ordered and immutable elements. In the given options, only (1,2,3) is a tuple because it is enclosed in parentheses and contains comma-separated values. The other options ([1, 2, 3], {1, 2, 3}, {}) are lists, sets, and dictionaries respectively, as they are enclosed in square brackets, curly braces, and curly braces with key-value pairs.
3.
Suppose t = (1, 2, 4, 3), which of the following is incorrect?
Correct Answer
B. T[3] = 45
Explanation
The given answer is incorrect because it tries to assign a new value (45) to the element at index 3 of the tuple t. However, tuples are immutable in Python, which means their elements cannot be modified once they are assigned. Therefore, trying to assign a new value to t[3] will result in a TypeError.
4.
What will be the output of the following Python code?
>>>t=(1,2,4,3)
>>>t[1:3]
Correct Answer
C. (2,4)
Explanation
The code is using slicing on the tuple t. The syntax t[1:3] means to select elements from index 1 up to, but not including, index 3. In the tuple t, the element at index 1 is 2 and the element at index 2 is 4. Therefore, the output will be (2, 4).
5.
What will be the output of the following Python code?
>>>t=(1,2,4,3)
>>>t[1:-1]
Correct Answer
C. (2,4)
Explanation
The code is using slicing to extract a portion of the tuple 't'. The syntax t[1:-1] means to start from the element at index 1 (which is 2) and go up to, but not including, the element at the last index (which is 3). Therefore, the output will be (2, 4).
6.
What will be the output of the following Python code?
>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0, len(t), 2)]
Correct Answer
C. [1, 4, 8]
Explanation
range(0, len(t), 2) generates the indices of t with a step size of 2: [0, 2, 4].
The list comprehension [t[i] for i in range(0, len(t), 2)] iterates over these indices and retrieves the elements from the tuple t.
The retrieved elements are (1, 4, 8).
7.
What will be the output of the following Python code?
d = {"john":40, "peter":45}
d["john"]
Correct Answer
A. 40
Explanation
The given Python code creates a dictionary called "d" with two key-value pairs: "john" with a value of 40 and "peter" with a value of 45. The line "d["john"]" accesses the value associated with the key "john" in the dictionary "d". Since the value of "john" is 40, the output of the code will be 40.
8.
What will be the output of the following Python code?
>>>t = (1, 2)
>>>2 * t
Correct Answer
A. (1, 2, 1, 2)
Explanation
The code multiplies the tuple t by 2, resulting in a new tuple with the elements of t repeated twice. Therefore, the output will be (1, 2, 1, 2).
9.
What will be the output of the following Python code?
>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2
Correct Answer
B. False
Explanation
The code is comparing two tuples t1 and t2 using the less than operator. In Python, when comparing tuples, it compares the elements at corresponding indices. The comparison is done element by element until a mismatch is found. In this case, the first mismatch occurs at index 2, where t1 has the element 4 and t2 has the element 3. Since 4 is greater than 3, t1 is considered greater than t2. Therefore, the output of the code will be False.
10.
Which of the following statements create a dictionary?
Correct Answer
D. All of the mentioned
Explanation
All of the mentioned statements create a dictionary. In Python, a dictionary is a collection of key-value pairs enclosed in curly braces {}. The first statement, d = {}, creates an empty dictionary. The second statement, d = {"john":40, "peter":45}, creates a dictionary with two key-value pairs, where "john" is the key and 40 is the value, and "peter" is the key and 45 is the value. The third statement, d = {40:"john", 45:"peter"}, also creates a dictionary with two key-value pairs, but the keys are integers (40 and 45) and the values are strings ("john" and "peter"). Therefore, all of the mentioned statements create a dictionary.
11.
What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
print(d)
Correct Answer
B. {'john': 40, 'peter': 45}
Explanation
This output shows the key-value pairs of the dictionary, where "john" maps to 40 and "peter" maps to 45.
12.
What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
"john" in d
Correct Answer
A. True
Explanation
The code snippet creates a dictionary "d" with two key-value pairs. The key "john" has a value of 40 and the key "peter" has a value of 45. The line "john in d" checks if the key "john" is present in the dictionary "d". Since it is present, the output will be True.
13.
Which of these about a dictionary is false?
Correct Answer
B. The keys of a dictionary can be accessed using values
Explanation
This statement is false because in a dictionary, the keys are used to access the corresponding values, not the other way around. Keys in a dictionary are unique and serve as identifiers for the values they are associated with. Therefore, it is not possible to access the keys using the values in a dictionary.
14.
Which of the following is not a declaration of the dictionary?
Correct Answer
C. {1,”A”,2”B”}
Explanation
The correct answer is {1,”A”,2”B”} because it is missing the colon (:) between the keys and values, which is necessary for declaring a dictionary in Python. The other options are valid declarations of dictionaries.
15.
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
Correct Answer
A. 1 A 2 B 3 C
Explanation
The code snippet creates a dictionary named 'a' with keys 1, 2, and 3, and corresponding values "A", "B", and "C" respectively. The for loop iterates through the items in the dictionary, which pairs each key with its corresponding value. The print statement prints each key-value pair, separated by a space, using the 'end' parameter to prevent a new line after each pair. Therefore, the output will be "1 A 2 B 3 C".
16.
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
Correct Answer
B. A
Explanation
The given Python code snippet creates a dictionary called "a" with keys 1, 2, and 3 and corresponding values "A", "B", and "C" respectively. The code then uses the get() method to retrieve the value associated with key 1 from the dictionary "a". Since the key 1 exists in the dictionary, the get() method returns the value "A". Therefore, the output of the code will be "A".
17.
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(5,4))
Correct Answer
D. 4
Explanation
The code snippet creates a dictionary 'a' with keys 1, 2, and 3 mapped to the values "A", "B", and "C" respectively. The print statement uses the get() method to access the value associated with the key 5 in the dictionary 'a'. Since the key 5 is not present in the dictionary, the get() method returns the default value specified as the second argument, which is 4. Therefore, the output of the code will be 4.
18.
Which of the statements about dictionary values if false?
Correct Answer
C. Values of a dictionary must be unique
Explanation
The statement "Values of a dictionary must be unique" is false because in a dictionary, multiple keys can have the same value. This means that different keys can be associated with the same value in a dictionary.
19.
What will be the output of the following Python code snippet?
>>> a={1:"A",2:"B",3:"C"}
>>> del a
Correct Answer
C. Del deletes the entire dictionary
Explanation
When you use del with a variable holding a dictionary, it deletes the entire dictionary, not just the keys or values. So in this case, executing del a would remove the entire dictionary a from memory.
20.
If a is a dictionary with some key-value pairs, what does a.popitem() do?
Correct Answer
A. Removes an arbitrary element
Explanation
The method a.popitem() removes an arbitrary element from the dictionary a. This means that it will remove and return any key-value pair from the dictionary, without any specific order or pattern. It is a valid method for dictionaries and can be used to remove elements when the specific key is not known or does not matter.