a) [1, 2, 3] b) [2, 3, 4] c) [2, 3, 4, 5] d) [1, 2, 3, 4]
Correct Answer: b) [2, 3, 4]
Explanation: The slice x[1:4] returns a sublist starting from index 1 up to, but not including, index 4.
Question 02
Which of the following is used to create a function in Python?
a) def b) func c) lambda d) method
Correct Answer: a) def
Explanation: The def keyword is used to define a function in Python.
Question 03
How do you reverse a list x in Python?
x = [1, 2, 3, 4, 5]
a) x.reverse() b) x[::-1] c) reversed(x) d) All of the above
Correct Answer: d) All of the above
Explanation: All options can be used to reverse a list in Python. x.reverse() modifies the list in place, x[::-1] creates a reversed copy, and reversed(x) returns an iterator.
Question 04
What will this Python code output?
print(“Python” + ” is fun”)
a) Python is fun b) Pythonisfun c) Syntax Error d) None of the above
Correct Answer: a) Python is fun
Explanation: The + operator concatenates two strings with no space between them unless explicitly added.
Question 05
Which Python data type is used to store ordered, mutable collections of items?
a) set b) tuple c) list d) dictionary
Correct Answer: c) list
Explanation: A list in Python is an ordered collection of elements that can be modified after creation (mutable).
Question 06
How would you remove duplicates from a list x in Python?
x = [1, 2, 3, 1, 2, 4]
a) set(x) b) x.unique() c) list(set(x)) d) x.remove_duplicates()
Correct Answer: c) list(set(x))
Explanation: Converting the list to a set removes duplicates, and converting it back to a list gives a list without duplicates.
Question 07
What is the result of this code?
a = [1, 2, 3]
a.append([4, 5])
print(len(a))
a) 3 b) 4 c) 5 d) Error
Correct Answer: b) 4
Explanation: The append method adds a single element (in this case, a list [4, 5]) to the end of the list, so the length of the list becomes 4.
Question 08
What is the difference between remove() and pop() in Python?
a) remove() deletes by index, pop() by value b) remove() deletes by value, pop() by index c) Both do the same thing d) remove() does not exist
Correct Answer: b) remove() deletes by value, pop() by index
Explanation:remove() deletes the first occurrence of the specified value, while pop() removes an element at the specified index.
Question 09
What is the output of this code?
x = [10, 20, 30, 40, 50]
print(x.pop(2))
a) 20 b) 30 c) 40 d) Error
Correct Answer: b) 30
Explanation: The pop() function removes the element at index 2, which is 30.
Question 10
What is the output of the following Python code?
x = “Hello World”
print(x.lower())
a) HELLO WORLD b) hello world c) Hello World d) Error
Correct Answer: b) hello world
Explanation: The lower() method converts all the characters in the string to lowercase.
Question 11
How do you check the length of a list x in Python?
a) len(x) b) x.size() c) x.count() d) length(x)
Correct Answer: a) len(x)
Explanation: The len() function returns the number of elements in a list.
Question 12
What will be the output of this code?
x = [1, 2, 3, 4, 5]
print(sum(x))
a) 15 b) 10 c) 20 d) Error
Correct Answer: a) 15
Explanation: The sum() function returns the sum of all elements in the list.
Question 13
How do you sort a list x in ascending order?
a) x.sort() b) sorted(x) c) Both a and b d) None of the above
Correct Answer: c) Both a and b
Explanation: The x.sort() method sorts the list in place, while sorted(x) returns a new sorted list without modifying the original.
Question 14
What is the result of this code?
x = [10, 20, 30, 40]
y = x.copy()
print(x == y and x is y)
a) True b) False c) Error d) None
Correct Answer: b) False
Explanation:x == y checks if the lists have the same elements, while x is y checks if they are the same object in memory. The lists have the same elements, but are different objects.
Question 15
Which of the following methods can add an element to the end of a list in Python?
a) append() b) insert() c) extend() d) add()
Correct Answer: a) append()
Explanation: The append() method adds an element to the end of the list. insert() adds an element at a specific index.
Question 16
What will this Python code output?
x = [1, 2, 3]
x.insert(1, 5)
print(x)
a) [1, 5, 2, 3] b) [1, 2, 5, 3] c) [5, 1, 2, 3] d) [1, 2, 3, 5]
Correct Answer: a) [1, 5, 2, 3]
Explanation: The insert() method inserts an element at a specified index. In this case, it inserts 5 at index 1.
Question 17
What will this Python code output?
x = [1, 2, 3]
print(x.index(2))
a) 0 b) 1 c) 2 d) Error
Correct Answer: b) 1
Explanation: The index() method returns the index of the first occurrence of the specified value, which is 2, located at index 1.
Question 18
How do you find the maximum value in a list x in Python?
a) max(x) b) x.max() c) max_value(x) d) x.max_value()
Correct Answer: a) max(x)
Explanation: The max() function returns the maximum value in a list.
Question 19
What is the output of this code?
x = [1, 2, 3, 4, 5]
x.remove(3)
print(x)
a) [1, 2, 4, 5] b) [1, 2, 3, 4] c) [1, 2, 5] d) [1, 2, 3, 4, 5]
Correct Answer: a) [1, 2, 4, 5]
Explanation: The remove() method removes the first occurrence of the specified value from the list, which is 3 in this case.
Question 20
Which of the following statements is true about Python dictionaries?
a) They are ordered b) They are indexed by position c) They store key-value pairs d) They are immutable
Correct Answer: c) They store key-value pairs
Explanation: Python dictionaries are collections of key-value pairs. They are unordered and mutable.