Explanation: The replace() method replaces occurrences of a substring in a string. All occurrences of "l" are replaced with "r", resulting in "Herro"
Question 42
What is the result of this code?
print([1, 2, 3] * 2)
a) [1, 2, 3, 1, 2, 3] b) [2, 4, 6] c) [1, 2, 3] d) Error
Correct Answer: a) [1, 2, 3, 1, 2, 3]
Explanation: The * operator can be used to repeat lists. [1, 2, 3] * 2 results in a new list that repeats the original list twice: [1, 2, 3, 1, 2, 3].
Question 43
What will be the result of this Python code?
numbers = [1, 2, 3, 4, 5]
print(numbers[1:3])
a) [1, 2] b) [2, 3] c) [3, 4] d) [1, 3]
Correct Answer: b) [2, 3]
Explanation: The slice [1:3] includes elements from index 1 to 3, but not including index 3. This gives the sublist [2, 3].
Question 44
What will this Python code return?
x = [“apple”, “banana”, “cherry”]
x.pop(1)
print(x)
a) [“apple”, “cherry”] b) [“banana”, “cherry”] c) [“apple”, “banana”] d) Error
Correct Answer: a) ["apple", "cherry"]
Explanation: The pop() method removes an element at a specific index. x.pop(1) removes "banana" from the list.
Question 45
What does the following Python code output?
for i in range(1, 6):
if i == 3:
continue
print(i, end=” “)
a) 1 2 4 5 b) 1 2 3 4 5 c) 1 2 3 d) Error
Correct Answer: a) 1 2 4 5
Explanation: The continue statement skips the rest of the loop for the current iteration. When i == 3, the loop continues without printing 3.
Question 46
What does this code return?
print([i**2 for i in range(3)])
a) [0, 1, 4] b) [1, 4, 9] c) [0, 1, 9] d) [1, 4]
Correct Answer: a) [0, 1, 4]
Explanation: The list comprehension [i**2 for i in range(3)] squares each number in the range from 0 to 2. The result is [0**2, 1**2, 2**2], which is [0, 1, 4].
Question 47
What does this Python code return?
print(5 == 5.0)
a) True b) False c) Error d) None
Correct Answer: a) True
Explanation: In Python, integers and floats are considered equal if they represent the same numerical value. 5 and 5.0 are equal, so the result is True.
Question 48
What does the following Python code return?
print(“,”.join([“a”, “b”, “c”]))
a) a,b,c b) abc c) [a,b,c] d) Error
Correct Answer: a) a,b,c
Explanation: The join() method concatenates the elements of the list with the specified delimiter. In this case, the elements "a", "b", and "c" are joined by commas.
Question 49
What will this code output?
print({1, 2, 3}.union({3, 4}))
a) {1, 2, 3, 4} b) {1, 2, 3} c) {3, 4} d) Error
Correct Answer: a) {1, 2, 3, 4}
Explanation: The union() method returns a set that contains all unique elements from both sets. The result is {1, 2, 3, 4}.
Question 50
What will the following Python code output?
print(type(range(5)))
a) <class ‘list’> b) <class ‘tuple’> c) <class ‘range’> d) <class ‘set’>
Correct Answer: c) <class 'range'>
Explanation: The range() function returns an object of type <class 'range'>, which represents a sequence of numbers.