Exercise: Data Types

Questions for: Data Types

What is the purpose of the chr() function?
A:
Returns the character at a specified Unicode code point
B:
Converts a string to uppercase
C:
Checks if a variable is of a certain type
D:
Rounds a floating-point number down to the nearest integer
Answer: A
The chr() function returns a string representing a character whose Unicode code point is the integer.
What is the output of the following code snippet?
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set)
A:
{1, 2, 3, 4, 5}
B:
{1, 2, 4, 5}
C:
{1, 2, 4}
D:
Error
Answer: B
The remove() method is used to remove a specific element from a set.
How do you convert a string to lowercase?
A:
string.lower()
B:
toLower(string)
C:
string.lowercase()
D:
lower(string)
Answer: A
The lower() method is used to convert a string to lowercase in Python.
What will be the output of the following code snippet?
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.get('d', 'Not Found')
print(result)
A:
1
B:
Not Found
C:
4
D:
Error
Answer: B
The get() method returns the value for the specified key. If the key is not found, it returns the default value, which is 'Not Found' in this case.
What is the purpose of the count() method in Python lists?
A:
To find the total number of elements in the list
B:
To count the occurrences of a specific element in the list
C:
To reverse the order of elements in the list
D:
To add an element to the end of the list
Answer: B

The count() method is used to count the number of occurrences of a specified element in a list.

Here is an example program that uses the count() method:

list = [1, 2, 3, 4, 1, 2]
print(list.count(1))

This program will print the number 2, which is the number of times the element 1 appears in the list.

Ad Slot (Above Pagination)
Quiz