General Python II
1.
What is the purpose of the `zip()` function in Python?
To compress files
To zip folders
To create a zip file
To combine iterables element-wise
2.
How can you check if a number is odd in Python?
num % 2 == 0
is_odd(num)
num.is_odd()
num % 2 != 0
3.
What is the output of the following code?
print("Python"[::-1])
nohtyP
Python
nythoP
tyhnoP
4.
How do you open a file for writing in Python and create it if it doesn't exist?
open("file.txt", "w")
open("file.txt", "r+")
open("file.txt", "a")
open("file.txt", "wx")
5.
Which keyword is used to define a function in Python?
method
define
function
def
6.
What is the purpose of the `strip()` method in Python?
To remove leading and trailing spaces
To split a string into a list
To replace characters in a string
To concatenate two strings
7.
How do you convert a list to a tuple in Python?
tuple(list)
list.to_tuple()
convert(list, tuple)
(tuple)list
8.
What is the purpose of the `enumerate()` function in Python?
To create an enumeration object
To iterate over a sequence while keeping track of the index
To count occurrences of elements in a list
To filter elements in a list
9.
What is the correct syntax for a list comprehension in Python?
[x for x in range(10)]
(x for x in range(10))
{x for x in range(10)}
{x: x for x in range(10)}
10.
How do you convert a string to an integer in Python?
int(string)
string.to_int()
str.convert_to_int()
parse_int(string)
11.
What is the purpose of the `else` clause in a try-except block?
To handle exceptions
To specify the type of exception
To define the code that should be executed if no exception occurs
To terminate the program
12.
What is the output of the following code?
print("Hello" * 3)
HelloHelloHello
Hello3
Hello + Hello + Hello
HelloHello
13.
How do you remove duplicates from a list in Python?
list.unique()
set(list)
list.remove_duplicates()
list(set(list))
14.
What does the `__doc__` attribute represent in Python?
The current module's documentation
The class's documentation
The function's documentation
The object's documentation string
15.
How do you check if a file exists in Python?
file.exists("filename")
os.file_exists("filename")
os.path.isfile("filename")
check_file("filename")
16.
What is the purpose of the `is` keyword in Python?
To compare two strings
To check if two variables refer to the same object
To test for equality in a conditional statement
To create an alias for a variable
17.
How can you raise a custom exception in Python?
raise Exception("Custom message")
throw Exception("Custom message")
exception("Custom message")
custom_exception("Custom message")
18.
What does the `ord()` function do in Python?
Returns the ASCII value of a character
Converts a character to lowercase
Returns the Unicode code point of a character
Finds the position of a character in the alphabet
19.
How do you check if a key exists in a dictionary without raising an error?
key.exists(dict)
dict.hasKey(key)
key in dict
dict[key] is not None
20.
What is the purpose of the `with` statement in Python?
To create a new scope for variables
To define a context manager
To handle exceptions in a block of code
To create a new thread