OA Exams
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.
What is the result of 5 == ‘5’ in Python?
a) Trueb) Falsec) Errord) 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.
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.
What is the result of 4 ** 2 in Python?
a) 8b) 16c) 4d) 12
Correct Answer: b) 16
Explanation: The ** operator raises the number to the power, so 4 ** 2 equals 16.
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.
What does the max() function return?
a) The maximum value in a list or sequenceb) The sum of all values in a listc) The average of values in a listd) 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.
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.
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.
What is the output of print(2 + 3 * 4)?
a) 14b) 20c) 10d) 24
Correct Answer: a) 14
Explanation: In Python, multiplication has higher precedence than addition, so 3 * 4 is evaluated first, followed by 2 + 12.
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.
What will be the output of print(bool(0)) in Python?
a) Trueb) Falsec) 0d) None
Explanation: In Python, 0 is considered False in a Boolean context
What does the lower() method do in Python?
a) Converts a string to lowercaseb) Converts a string to uppercasec) Removes whitespaced) Replaces characters
Correct Answer: a) Converts a string to lowercase
Explanation: The lower() method converts all characters in a string to lowercase.
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.
What is the result of len(‘hello’)?
a) 4b) 5c) 6d) 3
Correct Answer: b) 5
Explanation: The len() function returns the number of characters in a string, including spaces.
What is the purpose of the elif statement in Python?
a) To terminate a loopb) To check another condition after an if statementc) To handle exceptionsd) 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.
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.
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.
What will be the result of 2 + 2 == 4 in Python?
a) Trueb) Falsec) 4d) None
Correct Answer: a) True
Explanation: The expression 2 + 2 == 4 evaluates to True because 2 + 2 equals 4.
What is the purpose of the for loop in Python?
a) To execute code repeatedlyb) To define a functionc) To declare a variabled) 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
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.