Python Programming - Data Types

Exercise : Data Types - General Questions
  • Data Types - General Questions
61.
What will be the output of the following code snippet?
my_list = [4, 2, 7, 1, 9]
result = sorted(my_list)
print(result)
[1, 2, 4, 7, 9]
[9, 7, 4, 2, 1]
True
False
Answer: Option
Explanation:
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.

62.
What is the output of the following code snippet?
my_string = "Hello, World!"
result = my_string.split()
print(result)
['Hello,', 'World!']
'Hello, World!'
('Hello', 'World!')
Error
Answer: Option
Explanation:

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.


63.
What is the purpose of the any() function?
Checks if any element in an iterable is False
Checks if any element in an iterable is True
Checks if any element in an iterable is None
Checks if a variable is of a certain type
Answer: Option
Explanation:
The any() function returns True if at least one element in the iterable is True, otherwise False.

64.
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', 'b', 'c'}
['a', 'b', 'c']
dict_keys(['a', 'b', 'c'])
Error
Answer: Option
Explanation:

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.


65.
What is the output of the following code snippet?
my_string = "Hello, World!"
result = my_string.count("l")
print(result)
1
2
3
4
Answer: Option
Explanation:
The count() method returns the number of occurrences of the specified substring in the string.