Exercise: Data Types

Questions for: Data Types

What is the purpose of the append() method in Python lists?
A:
To remove an element from the list
B:
To add an element to the end of the list
C:
To add an element at a specified position
D:
To check if an element is present in the list
Answer: B
The append() method is used to add an element to the end of a list.
How do you convert a floating-point number to an integer?
A:
int(x)
B:
convert(x, int)
C:
x.integer()
D:
round(x)
Answer: A
The int() function can be used to convert a floating-point number to an integer by truncating the decimal part.
What is the output of the following code snippet?
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4])
A:
[2, 3, 4]
B:
[1, 2, 3]
C:
[1, 2, 3, 4]
D:
[3, 4, 5]
Answer: A
Slicing is used to extract a portion of a list. The specified range is [1:4], which includes elements at indices 1, 2, and 3.
What is the purpose of the type() function?
A:
To convert a variable to a different data type
B:
To check the data type of a variable
C:
To perform mathematical operations
D:
To round a floating-point number
Answer: B
The type() function is used to determine the type of a variable.
Which of the following data types is mutable?
A:
int
B:
float
C:
str
D:
list
Answer: D
Lists in Python are mutable, meaning their elements can be changed after the list is created.
Ad Slot (Above Pagination)
Quiz