OA Exams
Which of the following is a valid way to create a list in Python?
a) my_list = {1, 2, 3}b) my_list = (1, 2, 3)c) my_list = [1, 2, 3]d) my_list = <1, 2, 3>
Correct Answer: c) my_list = [1, 2, 3]
Explanation: A list in Python is created using square brackets, like [1, 2, 3].
What does the pop() method do in Python?
a) Adds an element to the listb) Removes the last element from the listc) Sorts the listd) Reverses the list
Correct Answer: b) Removes the last element from the list
Explanation: The pop() method removes and returns the last element from a list.
Which of the following statements about dictionaries in Python is true?
a) Keys in dictionaries must be uniqueb) Values in dictionaries must be uniquec) Dictionaries are immutabled) Dictionaries cannot contain lists
Correct Answer: a) Keys in dictionaries must be unique
Explanation: In a dictionary, keys must be unique, but values do not have to be.
What is the output of len([1, 2, 3, 4])?
a) 3b) 4c) 5d) 2
Correct Answer: b) 4
Explanation: The len() function returns the number of elements in the list, which is 4 in this case.
What is a tuple in Python?
a) A mutable collectionb) An unordered collectionc) A mutable collection of key-value pairsd) An immutable ordered collection
Correct Answer: d) An immutable ordered collection
Explanation: A tuple is an immutable sequence of values that cannot be changed once defined.
How can you check if a key exists in a dictionary?
a) if key in dict:b) if dict.has_key(key):c) if dict.exists(key):d) if key == dict:
Correct Answer: a) if key in dict:
Explanation: The in keyword checks whether a key exists in a dictionary.
What is the result of 5 % 3?
a) 1b) 2c) 3d) 0
Correct Answer: b) 2
Explanation: The modulus operator % returns the remainder when dividing 5 by 3, which is 2.
How do you create an empty dictionary in Python?
a) dict = []b) dict = {}c) dict = ()d) dict = “”
Correct Answer: b) dict = {}
Explanation: An empty dictionary is created using curly braces {}.
What will be the result of print(3 * ‘abc’)?
a) abcabcabcb) abcc) abcabcd) abcabcabcabc
Correct Answer: a) abcabcabc
Explanation: The * operator repeats the string 'abc' three times, resulting in 'abcabcabc'.
What is the correct syntax for defining a set in Python?
a) my_set = [1, 2, 3]b) my_set = {1, 2, 3}c) my_set = (1, 2, 3)d) my_set = <1, 2, 3>
Correct Answer: b) my_set = {1, 2, 3}
Explanation: A set in Python is created using curly braces, like {1, 2, 3}.
What is the purpose of the is operator in Python?
a) To compare two values for equalityb) To check if two variables point to the same objectc) To add two numbersd) To multiply two numbers
Correct Answer: b) To check if two variables point to the same object
Explanation: The is operator checks whether two variables point to the same object in memory.
How can you remove an element from a set in Python?
a) set.pop()b) set.remove()c) set.delete()d) set.drop()
Correct Answer: b) set.remove()
Explanation: The remove() method removes a specified element from a set.
What is the purpose of the range() function in Python?
a) To return a list of valuesb) To return a sequence of numbersc) To print numbersd) To sort numbers
Correct Answer: b) To return a sequence of numbers
Explanation: The range() function generates a sequence of numbers, which is often used in loops.
Which of the following can be used to generate a random number in Python?
a) random.randrange()b) math.random()c) sys.random()d) input.random()
Correct Answer: a) random.randrange()
Explanation: The random module provides functions such as randrange() to generate random numbers.
How do you access the value associated with a key in a dictionary?
a) dict.key()b) dict(key)c) dict[key]d) dict.get(key)
Correct Answer: c) dict[key]
Explanation: You can access the value associated with a key using the dict[key] syntax.
Which of the following methods can be used to split a string into a list?
a) split()b) join()c) replace()d) find()
Correct Answer: a) split()
Explanation: The split() method breaks a string into a list based on a delimiter.
Which function converts a string to a floating-point number in Python?
a) int()b) str()c) float()d) list()
Correct Answer: c) float()
Explanation: The float() function converts a string or integer to a floating-point number.
What is the default return value of a function that has no return statement?
a) 0b) Nonec) Trued) False
Correct Answer: b) None
Explanation: A function that does not specify a return value returns None by default.
How can you check if a list is empty in Python?
a) if list == {}b) if len(list) == 0c) if list is Noned) if list == ()
Correct Answer: b) if len(list) == 0
Explanation: The len() function returns the length of the list, and if it is 0, the list is empty.
What is the difference between a list and a tuple in Python?
a) Lists are mutable, tuples are immutableb) Tuples are mutable, lists are immutablec) Both are immutabled) Both are mutable
Correct Answer: a) Lists are mutable, tuples are immutable
Explanation: Lists can be modified, while tuples cannot be changed once created.