-
web.groovymark@gmail.com
- November 29, 2024
Question 41
How do you access a value in a dictionary using a key?
a) dict[key]
b) dict.value(key)
c) dict.get(key)
d) Both a and c
Correct Answer: d) Both a and c
Explanation: You can access a value in a dictionary using either dict[key] or dict.get(key).
Question 42
What is the output of print(7 % 3)?
a) 3
b) 1
c) 2
d) 0
Correct Answer: b) 1
Explanation: The % operator returns the remainder of the division, so 7 % 3 equals 1.
Question 43
Which function is used to iterate over a sequence with an index in Python?
a) enumerate()
b) iter()
c) zip()
d) range()
Correct Answer: a) enumerate()
Explanation: The enumerate() function allows you to loop over a sequence and get both the index and the element.
Question 44
How do you check if all elements in a list are true?
a) any(list)
b) all(list)
c) true(list)
d) bool(list)
Correct Answer: b) all(list)
Explanation: The all() function returns True if all elements in the list are true.
Question 45
What is the purpose of the finally block in Python?
a) Executes code when an exception is raised
b) Executes code after a try block, regardless of whether an exception was raised
c) Stops the execution of the program
d) Restarts the try block
Correct Answer: b) Executes code after a try block, regardless of whether an exception was raised
Explanation: The finally block is always executed after the try and except blocks, whether an exception occurs or not.
Correct Answer: b) Executes code after a try block, regardless of whether an exception was raised
Explanation: The finally block is always executed after the try and except blocks, whether an exception occurs or not.
Question 46
How do you create a function with variable-length arguments in Python?
a) def my_function(*args):
b) def my_function(args*):
c) def my_function(**args):
d) def my_function(*arguments):
Correct Answer: a) def my_function(*args):
Explanation: The *args syntax allows you to pass a variable number of arguments to a function.
Question 47
What does the zip() function do in Python?
a) Combines two or more lists into one
b) Joins two or more lists element-wise
c) Zips a file for compression
d) Sorts a list in ascending order
Correct Answer: b) Joins two or more lists element-wise
Explanation: The zip() function combines two or more iterables element by element into tuples.
Question 48
How do you remove a key from a dictionary?
a) del dict[key]
b) remove(dict[key])
c) pop(dict[key])
d) clear(dict[key])
Correct Answer: a) del dict[key]
Explanation: The del statement removes a key-value pair from a dictionary.
Question 49
What is the correct way to call a method from a class instance in Python?
a) class.method()
b) object.method()
c) method(object)
d) object.class.method()
Correct Answer: b) object.method()
Explanation: To call a method from a class instance, you use the syntax object.method().
Question 50
What does the is operator do in Python?
a) Compares if two values are equal
b) Compares if two variables point to the same object
c) Checks if a value is in a list
d) Checks if a string starts with a specific character
Correct Answer: b) Compares if two variables point to the same object
Explanation: The is operator checks if two variables refer to the same object in memory.