OA Exams
What does this Python code output?
print(5 // 2)
a) 2b) 2.5c) 3d) 1
Correct Answer: a) 2
Explanation: The // operator performs floor division, which returns the largest integer less than or equal to the result.
What does the following Python code output?
x = [“a”, “b”, “c”]
x.append(“d”)
print(x)
a) [“a”, “b”, “c”]b) [“a”, “b”, “c”, “d”]c) [“a”, “b”, “d”]d) Error
Correct Answer: b) ["a", "b", "c", "d"]
Explanation: The append() method adds an element to the end of the list.
What does the following Python code return?
print(len([1, 2, 3, 4, 5]))
a) 4b) 5c) 6d) Error
Correct Answer: b) 5
Explanation: The len() function returns the number of elements in the list, which is 5 in this case.
What does this Python code return?
print(“Hello, World!”.upper())
a) hello, world!b) HELLO, WORLD!c) Hello, World!d) Error
Correct Answer: b) HELLO, WORLD!
Explanation: The upper() method converts all the letters in the string to uppercase.
What will this Python code return?
x = [1, 2, 3, 4]
print(x.index(3))
a) 1b) 2c) 3d) 4
Correct Answer: b) 2
Explanation: The index() method returns the index of the first occurrence of the specified value. The value 3 is at index 2.
print(2 ** 3)
a) 5b) 6c) 8d) 9
Correct Answer: c) 8
Explanation: The ** operator is used for exponentiation. 2 ** 3 means 2 raised to the power of 3, which equals 8.
What will the following Python code output?
fruits = [“apple”, “banana”, “cherry”]
fruits.sort()
print(fruits)
a) [“apple”, “banana”, “cherry”]b) [“banana”, “apple”, “cherry”]c) [“cherry”, “banana”, “apple”]d) Error
Correct Answer: a) ["apple", "banana", "cherry"]
Explanation: The sort() method sorts the list in ascending alphabetical order.
What is the output of this Python code?
print(“abc” + “def”)
a) abcb) abcdefc) abc defd) Error
Correct Answer: b) abcdef
Explanation: The + operator concatenates two strings. "abc" + "def" results in "abcdef".
print([1, 2, 3].pop())
a) 1b) 2c) 3d) Error
Correct Answer: c) 3
Explanation: The pop() method removes and returns the last item from the list. In this case, 3 is the last item.
What will this Python code output?
print(10 % 3)
a) 1b) 2c) 3d) 0
Correct Answer: a) 1
Explanation: The % operator returns the remainder of the division. 10 % 3 equals 1 because 10 divided by 3 leaves a remainder of 1.
print(“Python”.startswith(“P”))
a) Trueb) Falsec) Errord) None
Correct Answer: a) True
Explanation: The startswith() method checks whether a string starts with a specified prefix. "Python".startswith("P") returns True.
print(list(range(3)))
a) [0, 1, 2, 3]b) [1, 2, 3]c) [0, 1, 2]d) [1, 2]
Correct Answer: c) [0, 1, 2]
Explanation: The range(3) function generates a sequence of numbers starting from 0 up to but not including 3.
print(3 == 3.0)
Explanation: In Python, 3 and 3.0 are considered equal because Python performs type coercion for numerical comparison.
print(“123”.isdigit())
Explanation: The isdigit() method returns True if all characters in the string are digits. "123" contains only digits, so it returns True.
print(2 != 3)
Explanation: The != operator checks whether two values are not equal. Since 2 is not equal to 3, the result is True.
print([1, 2, 3] == [1, 2, 3])
Explanation: The == operator compares the values of two lists. Since both lists contain the same values in the same order, the result is True.
What will be the result of this Python code?
x = [1, 2, 3]
print(len(x))
Explanation: The len() function returns the number of elements in a list. x contains three elements, so the result is 3.
x = 5
y = “5”
print(str(x) == y)
Explanation: The str() function converts the integer 5 to the string "5", making the comparison True.
y = x
y.append(4)
a) [1, 2, 3]b) [1, 2, 3, 4]c) [4]d) Error
Correct Answer: b) [1, 2, 3, 4]
Explanation: Both x and y refer to the same list in memory. Modifying y also affects x.
y = x.copy()
Correct Answer: a) [1, 2, 3]
Explanation: The copy() method creates a shallow copy of the list. Modifications to y do not affect the original list x.