Exercise: Data Types

Questions for: Data Types

What is the output of the following code snippet?
my_string = "Hello, World!"
result = my_string.count("l")
print(result)
A:
1
B:
2
C:
3
D:
4
Answer: C
The count() method returns the number of occurrences of the specified substring in the string.
What will be the result of the following code snippet?
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.keys()
print(result)
A:
{'a', 'b', 'c'}
B:
['a', 'b', 'c']
C:
dict_keys(['a', 'b', 'c'])
D:
Error
Answer: C

The keys() method in Python returns a view object that displays a list of all the keys in the dictionary.

In this case, it will return a view object containing the keys 'a', 'b', and 'c'. When printed, it shows as dict_keys(['a', 'b', 'c']). This is a view object, not a list, but it behaves similarly in many situations.

What is the purpose of the any() function?
A:
Checks if any element in an iterable is False
B:
Checks if any element in an iterable is True
C:
Checks if any element in an iterable is None
D:
Checks if a variable is of a certain type
Answer: B
The any() function returns True if at least one element in the iterable is True, otherwise False.
What is the output of the following code snippet?
my_string = "Hello, World!"
result = my_string.split()
print(result)
A:
['Hello,', 'World!']
B:
'Hello, World!'
C:
('Hello', 'World!')
D:
Error
Answer: A

The split() method in Python splits a string into a list of substrings based on a specified separator. If no separator is provided, it defaults to splitting the string by "whitespace".

In this case, since no separator is provided, the string my_string will be split into substrings based on whitespace, resulting in ['Hello,', 'World!'], which will be printed.

What will be the output of the following code snippet?
my_list = [4, 2, 7, 1, 9]
result = sorted(my_list)
print(result)
A:
[1, 2, 4, 7, 9]
B:
[9, 7, 4, 2, 1]
C:
True
D:
False
Answer: A
The sorted() function in Python returns a new sorted list from the elements of the given iterable, in this case, the list my_list. So, the elements of my_list will be sorted in ascending order, and the sorted list [1, 2, 4, 7, 9] will be printed.
Ad Slot (Above Pagination)
Quiz