General Python IV
1.
What is the correct way to define a constant in Python?
const CONSTANT_NAME = value
CONSTANT_NAME = value
final CONSTANT_NAME = value
constant CONSTANT_NAME = value
2.
How do you convert a string to uppercase in Python?
string.upper()
uppercase(string)
str.to_upper()
string.toUpper()
3.
How do you find the factorial of a number in Python?
math.factorial(number)
factorial(number)
number.factorial()
math.calculate_factorial(number)
4.
How do you check if a string contains only alphabetic characters?
string.isalpha()
alpha(string)
string.contains_alpha()
is_alphabetic(string)
5.
What is the purpose of the `random` module in Python?
To generate random numbers
To shuffle elements in a list
To create random strings
To perform random mathematical operations
6.
How do you convert a string to a datetime object in Python?
datetime.parse(string)
str.to_datetime()
datetime.strptime(string, format)
convert_to_datetime(string)
7.
What is the purpose of the `filter()` function in Python?
To filter elements in a list based on a condition
To create a filter for images
To eliminate duplicate elements in a list
To apply a filter to a dictionary
8.
What is the output of the following code?
print("Python" + str(3))
Python3
TypeError
Pythonstr(3)
SyntaxError
9.
How can you iterate over both the index and the elements of a list in Python?
for i, element in enumerate(list)
for i, element in range(len(list)):
for element in list:
for i, element in list
10.
What is the purpose of the `json` module in Python?
To interact with a JavaScript object notation
To encode and decode JSON data
To execute JavaScript code
To create JavaScript files
11.
How do you convert a list of strings to a single string in Python?
.join(list)
str.concat(list)
list.combine()
merge(list)
12.
What is the purpose of the `heapq` module in Python?
To implement heaps in Python
To sort elements in a list
To manage the heap memory
To perform mathematical operations
13.
What does the `continue` statement do in a loop in Python?
Exits the loop
Skips the rest of the code in the loop and moves to the next iteration
Skips the current iteration and moves to the next
Does nothing
14.
What is the purpose of the `sys` module in Python?
To interact with the system
To perform system-level operations
To manipulate strings
To manage system files
15.
How can you check if a file is readable in Python?
os.is_readable("filename")
file.is_readable()
os.access("filename", os.R_OK)
file.can_read()
16.
What is the purpose of the `set` data type in Python?
To store key-value pairs
To store an ordered collection of elements
To store unique elements
To store elements in a stack
17.
What is the correct way to open a file in Python and ensure it is closed properly?
with open("file.txt", "r") as file:
file = open("file.txt", "r")
open("file.txt", "r")
file.open("file.txt", "r")
18.
How do you check if a string ends with a specific substring in Python?
string.ends_with("substring")
string.endswith("substring")
string[-len("substring"):]== "substring"
ends(string, "substring")
19.
What is the output of the following code?
print(not True)
False
True
0
1
20.
How do you convert a string to a list of characters in Python?
string.to_list()
list(char for char in string)
convert_to_list(string)
list(string)