Multiple Choice Questions-tuples & Dictionaries

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Aparnasunil75
A
Aparnasunil75
Community Contributor
Quizzes Created: 20 | Total Attempts: 43,508
| Attempts: 2,347 | Questions: 20
Please wait...
Question 1 / 20
0 %
0/100
Score 0/100
1. Which of the following is a Python tuple?

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.

Submit
Please wait...
About This Quiz
Multiple Choice Questions-tuples & Dictionaries - Quiz

This quiz focuses on tuples and dictionaries in Python, testing knowledge on tuple indexing, slicing, and comprehension, as well as practical applications in Python programming. It assesses key... see moreskills in handling immutable and dictionary data types, vital for efficient coding. see less

2. What will be the output of the following Python code? d = {"john":40, "peter":45} d["john"]

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.

Submit
3. What will be the output of the following Python code snippet? d = {"john":40, "peter":45} "john" in d

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.

Submit
4. Suppose t = (1, 2, 4, 3), which of the following is incorrect?

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.

Submit
5. Which of the following statements create a dictionary?

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.

Submit
6. What will be the output of the following Python code? >>>t = (1, 2) >>>2 * t

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).

Submit
7. To which of the following the "in" operator can be used to check if an item is in it?

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.

Submit
8. What will be the output of the following Python code? >>>t=(1,2,4,3) >>>t[1:3]

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).

Submit
9. What will be the output of the following Python code? >>>t1 = (1, 2, 4, 3) >>>t2 = (1, 2, 3, 4) >>>t1 < t2

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.

Submit
10. What will be the output of the following Python code? >>>t=(1,2,4,3) >>>t[1:-1]

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).

Submit
11. What will be the output of the following Python code snippet? >>> a={1:"A",2:"B",3:"C"} >>> del a

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.
Submit
12. 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=" ")

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".

Submit
13. Which of the following is not a declaration of the dictionary?

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.

Submit
14. What will be the output of the following Python code snippet? d = {"john":40, "peter":45} print(d)

Explanation

This output shows the key-value pairs of the dictionary, where "john" maps to 40 and "peter" maps to 45.

Submit
15. Which of the statements about dictionary values if false?

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.

Submit
16. Which of these about a dictionary is false?

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.

Submit
17. What will be the output of the following Python code snippet? a={1:"A",2:"B",3:"C"} print(a.get(1,4))

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".

Submit
18. 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)]

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).

Submit
19. If a is a dictionary with some key-value pairs, what does a.popitem() do?

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.

Submit
20. What will be the output of the following Python code snippet? a={1:"A",2:"B",3:"C"} print(a.get(5,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.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 1, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 01, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 18, 2020
    Quiz Created by
    Aparnasunil75
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which of the following is a Python tuple?
What will be the output of the following Python code? ...
What will be the output of the following Python code snippet? ...
Suppose t = (1, 2, 4, 3), which of the following is incorrect?
Which of the following statements create a dictionary?
What will be the output of the following Python code? ...
To which of the following the "in" operator can be used to check if an...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code snippet? ...
What will be the output of the following Python code snippet? ...
Which of the following is not a declaration of the dictionary?
What will be the output of the following Python code snippet? ...
Which of the statements about dictionary values if false?
Which of these about a dictionary is false?
What will be the output of the following Python code snippet? ...
What will be the output of the following Python code? ...
If a is a dictionary with some key-value pairs, what does a.popitem()...
What will be the output of the following Python code snippet? ...
Alert!

Advertisement