a) hello world b) helloworld c) Error d) None of the above
Correct Answer: a) hello world
Explanation: The + operator concatenates strings, and a space is added between x and y to form "hello world".
Question 02
What is the correct way to check if a string x starts with a specific substring in Python?
a) x.startswith(“substring”) b) x.startswith = “substring” c) x.start(“substring”) d) x.startwith(“substring”)
Correct Answer: a) x.startswith("substring")
Explanation: The startswith() method checks if a string starts with a specified substring.
Question 03
What is the output of this code?
x = “Python”
print(x[2:5])
a) Pyt b) tho c) tho d) yth
Correct Answer: b) tho
Explanation: The slice x[2:5] extracts characters starting from index 2 up to, but not including, index 5.
Question 04
What will this Python code output?
x = [1, 2, 3]
x.insert(1, 5)
print(x)
a) [1, 2, 3] b) [1, 5, 2, 3] c) [5, 1, 2, 3] d) Error
Correct Answer: b) [1, 5, 2, 3]
Explanation: The insert() method inserts an element at the specified index. In this case, 5 is inserted at index 1.
Question 05
What is the result of the following code?
x = [1, 2, 3]
x.extend([4, 5])
print(x)
a) [1, 2, 3, 4, 5] b) [4, 5] c) [1, 2, 3] d) Error
Correct Answer: a) [1, 2, 3, 4, 5]
Explanation: The extend() method adds all elements from the iterable to the end of the list.
Question 06
Which function can be used to combine all elements of a list into a string?
a) append() b) concat() c) join() d) merge()
Correct Answer: c) join()
Explanation: The join() method combines all elements of a list into a string, with a specified separator between the elements.
Question 07
What is the correct syntax to sort a list x in ascending order?
a) x.sort() b) x.sort(reverse=True) c) x.sorted() d) sort(x)
Correct Answer: a) x.sort()
Explanation: The sort() method sorts the list in-place in ascending order.
Question 08
What will be the output of this code?
x = [5, 1, 3, 2]
y = sorted(x)
print(y)
a) [5, 1, 3, 2] b) [1, 2, 3, 5] c) None d) Error
Correct Answer: b) [1, 2, 3, 5]
Explanation: The sorted() function returns a new sorted list, leaving the original list unchanged.
Question 09
What is the purpose of the reverse() method in Python lists?
a) It sorts the list in reverse order b) It returns a reversed copy of the list c) It reverses the order of elements in the list in place d) It returns a new reversed list
Correct Answer: c) It reverses the order of elements in the list in place
Explanation: The reverse() method reverses the elements of the list in place.
Question 10
What will be the output of this code?
x = [1, 2, 3, 4]
print(x[-2:])
a) [1, 2] b) [2, 3] c) [3, 4] d) [4]
Correct Answer: c) [3, 4]
Explanation: The slice x[-2:] returns the last two elements of the list.
Question 11
How do you remove an element from a list by its value in Python?
a) x.remove(value) b) x.pop(value) c) del x[value] d) x.delete(value)
Correct Answer: a) x.remove(value)
Explanation: The remove() method removes the first occurrence of the specified value from the list.
Question 12
What is the output of this Python code?
x = [1, 2, 3, 4]
y = x.pop(2)
print(x)
a) [1, 2, 3, 4] b) [1, 2, 4] c) [1, 2, 3] d) Error
Correct Answer: b) [1, 2, 4]
Explanation: The pop() method removes and returns the element at the specified index (in this case, index 2).
Question 13
Which function is used to determine if all elements of an iterable are true in Python?
a) any() b) check() c) all() d) test()
Correct Answer: c) all()
Explanation: The all() function returns True if all elements in the iterable are true.
Question 14
What will this Python code output?
x = [True, False, True]
print(any(x))
a) True b) False c) Error d) None
Correct Answer: a) True
Explanation: The any() function returns True if at least one element in the iterable is true.
Question 15
How can you check if a value exists in a list x?
a) value in x b) x.has(value) c) exists(value, x) d) x.find(value)
Correct Answer: a) value in x
Explanation: The in keyword is used to check if a value exists in a list.
Question 16
What is the output of this Python code?
x = [1, 2, 3, 4]
print(5 in x)
a) True b) False c) Error d) None
Correct Answer: b) False
Explanation: The in operator checks if the value 5 exists in the list x, which it does not.
Question 17
What will this Python code output?
x = {“a”: 1, “b”: 2}
x[“c”] = 3
print(x)
a) {“a”: 1, “b”: 2, “c”: 3} b) {“a”: 1, “b”: 2} c) Error d) None
Correct Answer: a) {"a": 1, "b": 2, "c": 3}
Explanation: A new key-value pair can be added to a dictionary by assigning a value to a new key.
Question 18
What does the keys() method return when called on a dictionary?
a) A list of all values b) A list of all keys c) A list of all key-value pairs d) None of the above
Correct Answer: b) A list of all keys
Explanation: The keys() method returns a view object that contains all the keys in the dictionary.
Question 19
How do you get a list of all values in a dictionary x?
a) x.values() b) x.getvalues() c) x.keys() d) x.all()
Correct Answer: a) x.values()
Explanation: The values() method returns a view object that contains all the values in the dictionary.
Question 20
What is the output of this Python code?
x = {“a”: 1, “b”: 2}
y = x.pop(“a”)
print(x)
a) {“a”: 1, “b”: 2} b) {“b”: 2} c) {} d) Error
Correct Answer: b) {"b": 2}
Explanation: The pop() method removes the specified key from the dictionary and returns its value.