-
web.groovymark@gmail.com
- November 29, 2024
Question 21
How do you iterate over the keys in a dictionary in Python?
a) for key in dict.keys()
b) for key in dict.values()
c) for key in dict.items()
d) for key in dict:
Correct Answer: d) for key in dict:
Explanation: Iterating directly over a dictionary in a for loop accesses the keys by default.
Question 22
What is the result of 5 == ‘5’ in Python?
a) True
b) False
c) Error
d) None
Correct Answer: b) False
Explanation: The == operator checks for value equality, and since an integer is not equal to a string, the result is False.
Question 23
Which method can be used to add an element to a set?
a) append()
b) insert()
c) add()
d) extend()
Correct Answer: c) add()
Explanation: The add() method is used to insert elements into a set.
Question 24
What is the result of 4 ** 2 in Python?
a) 8
b) 16
c) 4
d) 12
Correct Answer: b) 16
Explanation: The ** operator raises the number to the power, so 4 ** 2 equals 16.
Question 25
How do you remove whitespace from both ends of a string in Python?
a) strip()
b) remove()
c) trim()
d) split()
Correct Answer: a) strip()
Explanation: The strip() method removes leading and trailing whitespace from a string.
Question 26
What does the max() function return?
a) The maximum value in a list or sequence
b) The sum of all values in a list
c) The average of values in a list
d) The length of a list
Correct Answer: a) The maximum value in a list or sequence
Explanation: The max() function returns the largest item in a list or other iterable.
Question 27
How can you convert a list into a string in Python?
a) str()
b) join()
c) split()
d) map()
Correct Answer: b) join()
Explanation: The join() method combines elements of a list into a single string.
Question 28
Which of the following is a valid Python dictionary?
a) {1, 2, 3}
b) {‘a’: 1, ‘b’: 2}
c) [1: ‘a’, 2: ‘b’]
d) (1, 2, 3)
Correct Answer: b) {'a': 1, 'b': 2}
Explanation: A valid dictionary consists of key-value pairs enclosed in curly braces.
Question 29
What is the output of print(2 + 3 * 4)?
a) 14
b) 20
c) 10
d) 24
Correct Answer: a) 14
Explanation: In Python, multiplication has higher precedence than addition, so 3 * 4 is evaluated first, followed by 2 + 12.
Question 30
What is the correct syntax for defining a function in Python?
a) def my_function[]:
b) def my_function():
c) define my_function():
d) function my_function():
Correct Answer: b) def my_function():
Explanation: The def keyword is used to define a function in Python.
Question 31
What will be the output of print(bool(0)) in Python?
a) True
b) False
c) 0
d) None
Correct Answer: b) False
Explanation: In Python, 0 is considered False in a Boolean context
Question 32
What does the lower() method do in Python?
a) Converts a string to lowercase
b) Converts a string to uppercase
c) Removes whitespace
d) Replaces characters
Correct Answer: a) Converts a string to lowercase
Explanation: The lower() method converts all characters in a string to lowercase.
Question 33
How do you check if a list contains an element in Python?
a) if element in list:
b) if list.has(element):
c) if list.contains(element):
d) if element == list:
Correct Answer: a) if element in list:
Explanation: The in keyword is used to check for membership in a list.
Question 34
What is the result of len(‘hello’)?
a) 4
b) 5
c) 6
d) 3
Correct Answer: b) 5
Explanation: The len() function returns the number of characters in a string, including spaces.
Question 35
What is the purpose of the elif statement in Python?
a) To terminate a loop
b) To check another condition after an if statement
c) To handle exceptions
d) To create a function
Correct Answer: b) To check another condition after an if statement
Explanation: The elif statement allows you to check additional conditions after an if statement.
Question 36
How do you define a class in Python?
a) define class MyClass:
b) def class MyClass:
c) class MyClass:
d) create class MyClass:
Correct Answer: c) class MyClass:
Explanation: The class keyword is used to define a class in Python.
Question 37
Which method can be used to find the position of an element in a list?
a) index()
b) find()
c) locate()
d) position()
Correct Answer: a) index()
Explanation: The index() method returns the position of the first occurrence of an element in a list.
Question 38
What will be the result of 2 + 2 == 4 in Python?
a) True
b) False
c) 4
d) None
Correct Answer: a) True
Explanation: The expression 2 + 2 == 4 evaluates to True because 2 + 2 equals 4.
Question 39
What is the purpose of the for loop in Python?
a) To execute code repeatedly
b) To define a function
c) To declare a variable
d) To handle exceptions
Correct Answer: a) To execute code repeatedly
Explanation: The for loop is used to iterate over a sequence and execute a block of code multiple times
Question 40
Which function converts a string to an integer in Python?
a) str()
b) int()
c) float()
d) list()
Correct Answer: b) int()
Explanation: The int() function converts a string or float to an integer.