OA Exams
Which of the following creates a dictionary in Python?
a) []b) {}c) {key: value}d) ()
Correct Answer: c) {key: value}
Explanation: Dictionaries in Python are created using curly braces {} with key-value pairs inside, separated by colons.
Which Python method is used to add an item to the end of a list?
a) insert()b) append()c) add()d) extend()
Correct Answer: b) append()
Explanation: The append() method is used to add an element to the end of a list.
Which operator is used to check if an element exists in a list?
a) existsb) inc) hasd) contains
Correct Answer: b) in
Explanation: The in operator is used to check if an element exists in a list or any other iterable.
What is the output of the following code?
print(“Hello” + “World”)
a) HelloWorldb) Hello Worldc) Errord) HelloWorld without space
Correct Answer: a) HelloWorld
Explanation: The + operator concatenates strings, so "Hello" + "World" results in "HelloWorld".
How would you access the value associated with the key “name” in the dictionary below?
person = {“name”: “John”, “age”: 30}
a) person[“John”]b) person[“name”]c) person(“name”)d) name
Correct Answer: b) person["name"]
Explanation: In Python dictionaries, you access values using the key inside square brackets.
Which Python method is used to combine two lists?
a) add()b) combine()c) extend()d) merge()
Correct Answer: c) extend()
Explanation: The extend() method adds the elements of one list to the end of another list.
What is the output of this Python code?
numbers = [1, 2, 3]
numbers[0] = 10
print(numbers)
a) [1, 2, 3]b) [10, 2, 3]c) [1, 2, 10]d) Error
Correct Answer: b) [10, 2, 3]
Explanation: The first element of the list (at index 0) is reassigned to 10, so the list becomes [10, 2, 3].
What will this code return?
my_str = “Hello World”
print(my_str[0:5])
a) Hello Worldb) Helloc) Worldd) Hell
Correct Answer: b) Hello
Explanation: The slicing my_str[0:5] returns the first 5 characters of the string, which is "Hello".
What does this Python code output?
print(2 * 3 ** 2)
a) 12b) 18c) 36d) 16
Correct Answer: b) 18
Explanation: According to the order of operations, 3 ** 2 = 9, and then 2 * 9 = 18.
How can you create a tuple in Python?
a) [1, 2, 3]b) (1, 2, 3)c) {1, 2, 3}d) tuple[1, 2, 3]
Correct Answer: b) (1, 2, 3)
Explanation: A tuple is created by using parentheses () with comma-separated values inside.
Which of the following is not a valid Python data type?
a) listb) setc) arrayd) dictionary
Correct Answer: c) array
Explanation: Arrays are not a built-in data type in Python. Python has lists, sets, and dictionaries as built-in data structures.
Which keyword is used to define a class in Python?
a) classb) defc) objectd) func
Correct Answer: a) class
Explanation: The class keyword is used to define a class in Python.
What does the following Python expression return?
len([10, 20, 30])
a) 10b) 20c) 3d) 30
Correct Answer: c) 3
Explanation: The len() function returns the number of items in the list, which is 3.
Which of the following Python keywords is used to create a loop?
a) ifb) elsec) ford) break
Correct Answer: c) for
Explanation: The for keyword is used to create a loop that iterates over a sequence.
How do you remove an element from a list in Python?
a) del()b) remove()c) discard()d) pop()
Correct Answer: b) remove()
Explanation: The remove() method removes the first occurrence of a specified element from the list.
What does the continue statement do in Python loops?
a) Exits the loopb) Skips the current iteration and moves to the nextc) Repeats the current iterationd) Terminates the loop
Correct Answer: b) Skips the current iteration and moves to the next
Explanation: The continue statement skips the current iteration and proceeds with the next iteration of the loop.
What is the output of this code?
my_str = “Hello”
print(my_str[-1])
a) Hb) ec) od) l
Correct Answer: c) o
Explanation: Negative indexing starts from the end, so my_str[-1] returns the last character, which is "o".
What does this Python code return?
x = 7
y = 5
print(x % y)
a) 2b) 5c) 1d) 0
Correct Answer: a) 2
Explanation: The modulus operator % returns the remainder when 7 is divided by 5, which is 2.
How do you declare an empty set in Python?
a) {}b) []c) set()d) empty()
Correct Answer: c) set()
Explanation: set() is used to create an empty set in Python. {} is used for dictionaries.
What is the output of the following Python code?
x = 5
y = 10
print(x == y)
a) Trueb) Falsec) 5d) 10
Correct Answer: b) False
Explanation: The == operator checks if x is equal to y. Since 5 is not equal to 10, the result is False.