Python Programming - Data Types

Exercise : Data Types - General Questions
  • Data Types - General Questions
56.
What is the output of the following code snippet?
my_list = [1, 2, 3, 4, 5]
result = sum(my_list)
print(result)
15
12345
5
Error
Answer: Option
Explanation:
The sum() function calculates the sum of all elements in a list.

57.
What will be the output of the following code snippet?
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = len(my_dict)
print(result)
{'a': 1, 'b': 2, 'c': 3}
3
6
Error
Answer: Option
Explanation:
The len() function returns the number of items (key-value pairs) in the dictionary.

58.
How do you check if a variable is of type tuple?
is_tuple(variable)
type(variable) == tuple
variable.is_tuple()
tuple(variable)
Answer: Option
Explanation:
The type() function is used to check the type of a variable.

59.
What is the output of the following code snippet?
my_set = {1, 2, 3, 4, 5}
result = my_set.discard(3)
print(result)
{1, 2, 3, 5}
{1, 2, 4, 5}
None
Error
Answer: Option
Explanation:
The discard() method removes the specified element from the set but does not return a value (None).

60.
What is the output of the following code snippet?
my_tuple = (1, 2, 3, 4, 5)
result = my_tuple.index(3)
print(result)
2
3
4
Error
Answer: Option
Explanation:
The index() method in Python returns the index of the first occurrence of the specified element in the tuple. In this case, the element 3 is at index 2 in the tuple my_tuple, so 2 will be printed.