General Python I
1.
How can you check the type of a variable in Python?
typeOf(variable)
typeof variable
variable.type()
type(variable)
2.
What is the purpose of the `pass` statement in Python?
To break out of a loop
To do nothing and avoid syntax errors
To skip the current iteration in a loop
To terminate the program
3.
How do you round a floating-point number to the nearest integer in Python?
round(num)
int(num)
math.round(num)
{:.0f}.format(num)
4.
Which module is used for regular expressions in Python?
re
regex
pyregex
regexpy
5.
What is the correct way to concatenate two lists in Python?
list1.concat(list2)
list1.extend(list2)
list1 + list2
list1.append(list2)
6.
What is the output of the following code?
print(3 + 4 * 2)
10
14
21
11
7.
How do you comment multiple lines in Python?
# This is a comment
// This is a comment
/* This is a comment */
''' This is a comment '''
8.
What is the correct way to check if a key exists in a dictionary?
key in dict
dict.key()
dict.hasKey()
key.exists(dict)
9.
How can you convert a string to lowercase in Python?
str.lower()
string.toLower()
str.casefold()
string.lowercase()
10.
Which of the following is a mutable data type in Python?
int
tuple
list
string
11.
What does the `len()` function do in Python?
Returns the length of a string
Returns the number of elements in a list
Returns the size of a file
Returns the square root of a number
12.
How can you remove an element from a list in Python?
list.remove(element)
list.delete(element)
del list[element]
list.pop(element)
13.
What is the purpose of the `__init__` method in Python classes?
To initialize the class attributes
To create a new instance of the class
To define the constructor of the class
To destroy the class objects
14.
What is the output of the following code?
print("Hello" + 3)
TypeError
Hello3
Hello+3
SyntaxError
15.
How do you open a file in binary mode in Python?
open("file.txt", "r")
open("file.txt", "w")
open("file.txt", "rb")
open("file.txt", "wb")
16.
What is the purpose of the `super()` function in Python?
To call the superclass's constructor
To access the superclass's methods
To create an instance of the superclass
To inherit from multiple classes
17.
How can you format a floating-point number to a specific number of decimal places?
format(num, ".2f")
num.toFixed(2)
float.format(num, 2)
{:.2f}.format(num)
18.
What does the `__name__` variable represent in a Python script?
The script's filename
The current module's name
The script's execution status
The name of the main function
19.
How do you create an empty set in Python?
set.empty()
set()
emptySet = set()
{}
20.
What is the purpose of the `map()` function in Python?
To apply a function to each element of an iterable
To create a mapping between two lists
To map keys to values in a dictionary
To filter elements in a list