Uploaded by M.ali.28809

pythonmcqs

advertisement
def calculate_sum(numbers):
result = 0
for num in numbers:
result += num
return result
data = [3, 7, 2, 8, 5]
output = calculate_sum(data)
What is the purpose of the calculate_sum function in the given code?
a) Multiply all numbers in the list.
b) Find the average of the numbers in the list.
c) Calculate the sum of all numbers in the list.
d) Return the length of the list.
Explanation:
Correct Answer (c): The calculate_sum function iterates through each number in the list and adds them up, returning the sum of all numbers.
Incorrect Answer (a): The function is not multiplying the numbers; it is adding them.
Incorrect Answer (b): The function is finding the sum, not the average.
Incorrect Answer (d): The function is not returning the length of the list; it is returning the sum of its elements.
Question:
Consider the following Python code snippet:
python
Copy code
def find_max_value(numbers):
max_val = max(numbers)
return max_val
data = [15, 27, 8, 42, 11]
result = find_max_value(data)
What does the find_max_value function in the given code do?
a) Find the minimum value in the list.
b) Find the maximum value in the list.
c) Calculate the average of the numbers in the list.
d) Return the length of the list.
Explanation:
Correct Answer (b): The find_max_value function uses the max() function to determine and return the maximum value in the list.
Incorrect Answer (a): The function is finding the maximum value, not the minimum.
Incorrect Answer (c): The function is not calculating the average; it is finding the maximum value.
Incorrect Answer (d): The function is not returning the length of the list; it is returning the maximum value.
Question:
Consider the following Python code snippet:
python
Copy code
def count_vowels(word):
vowel_count = 0
vowels = "aeiouAEIOU"
for char in word:
if char in vowels:
vowel_count += 1
return vowel_count
text = "Python Programming"
result = count_vowels(text)
What is the output of the count_vowels function for the given text?
a) 3
b) 4
c) 5
d) 6
Explanation:
Correct Answer (c): The count_vowels function counts the number of vowels in the given text. There are five vowels ('o', 'o', 'a', 'i', 'o') in "Python Programming."
Incorrect Answers (a, b, d): These options do not represent the correct count of vowels in the given text.
Question:
Consider the following Python code snippet:
python
Copy code
def multiply_by_index(numbers):
result = [num * index for index, num in enumerate(numbers)]
return result
data = [2, 4, 6, 8, 10]
output = multiply_by_index(data)
What does the multiply_by_index function in the given code do?
a) Multiply each number by its index in the list.
b) Find the sum of the numbers in the list.
c) Return the list unchanged.
d) Calculate the average of the numbers in the list.
Explanation:
Correct Answer (a): The multiply_by_index function multiplies each number in the list by its corresponding index.
Incorrect Answer (b): The function is not finding the sum; it is multiplying each number by its index.
Incorrect Answer (c): The function is performing a transformation by multiplying each number by its index, not returning the list unchanged.
Incorrect Answer (d): The function is not calculating the average; it is multiplying each number by its index.
Question:
Consider the following Python code snippet:
python
Copy code
def reverse_string(text):
reversed_text = text[::-1]
return reversed_text
input_str = "Hello, World!"
result = reverse_string(input_str)
What is the output of the reverse_string function for the given input_str?
a) "!dlroW ,olleH"
b) "dlroW ,olleH!"
c) "World, Hello!"
d) "H! ,WORLD"
Explanation:
Correct Answer (a): The reverse_string function uses slicing ([::-1]) to reverse the characters in the given string.
Incorrect Answers (b, c, d): These options do not represent the correct reversed form of the given input string.
Download