General Python III
1.
What is the output of the following code?
print("Python"[:-2])
Py
Pyt
P
Pyth
2.
What is the purpose of the `time` module in Python?
To manipulate date and time
To measure the execution time of code
To sleep or pause the program
To convert time zones
3.
What does the `pass` statement do in a loop in Python?
Terminates the loop
Skips the current iteration and continues to the next
Breaks out of the loop
Does nothing and continues to the next iteration
4.
How do you remove all occurrences of a specific element from a list in Python?
list.remove(element)
list.discard(element)
list.remove_all(element)
list = [x for x in list if x != element]
5.
What is the purpose of the `chr()` function in Python?
Converts a character to its ASCII value
Converts an ASCII value to a character
Returns the Unicode code point of a character
Converts a string to uppercase
6.
How can you concatenate two dictionaries in Python?
dict1 + dict2
dict1.concat(dict2)
dict1.extend(dict2)
{**dict1, **dict2}
7.
What is the purpose of the `os` module in Python?
To manipulate strings
To interact with the operating system
To perform mathematical operations
To manage databases
8.
How do you find the maximum value in a list in Python?
max(list)
list.max()
list.maximum()
max_value(list)
9.
How do you check if a variable is of a specific type in Python?
type(variable) == "specific_type"
isinstance(variable, specific_type)
variable.is_type(specific_type)
check_type(variable, specific_type)
10.
What does the `__str__` method do in Python classes?
Converts an object to a string
Defines a string constant
Concatenates two strings
Checks if the object is a string
11.
What is the purpose of the `global` keyword in Python?
To declare a variable within a function as global
To make a variable accessible throughout the program
To define a global constant
To declare a global function
12.
How do you check if a number is a float in Python?
num.is_float()
float(num)
type(num) == float
isinstance(num, float)
13.
What does the `else` clause do in a for loop in Python?
Executes if an exception occurs
Executes if the loop is interrupted
Executes if the loop completes without a `break`
Executes if the loop encounters a `continue`
14.
How can you convert a string to a list of characters in Python?
list(string)
split(string)
string.to_list()
list(char for char in string)
15.
What is the purpose of the `any()` function in Python?
Returns True if all elements in an iterable are true
Returns True if any element in an iterable is true
Returns True if all elements in an iterable are false
Returns True if any element in an iterable is false
16.
What is the correct way to handle exceptions in Python?
try { /* code */ } except Exception as e:
catch (Exception e) { /* code */ }
try: /* code */ except Exception as e:
except Exception as e: /* code */
17.
How do you reverse a list in Python?
list.reverse()
reversed(list)
list.sort(reverse=True)
list[::-1]
18.
What is the purpose of the `lambda` keyword in Python?
To declare anonymous functions
To create a new variable
To define a constant
To specify a block of code
19.
What is the output of the following code?
print(2 ** 3)
6
8
12
2
20.
How do you check if a string starts with a specific substring in Python?
string.startsWith("substring")
string[:len("substring")] == "substring"
string.startswith("substring")
string.contains("substring")