MCQs on Python List, Tuple, Dictionary, and String
List – 5 Questions
1. What is the output of the following code?
a = [1, 2, 3]
a.append([4, 5])
print(len(a))
a) 3
b) 4
c) 5
d) Error
Answer: b) 4
2. Which of the following creates a copy of a list L?
a) L.copy()
b) L[:]
c) list(L)
d) All of these
Answer: d) All of these
3. What will be the output of the following?
L = [10, 20, 30, 40]
print(L[-2])
a) 10
b) 20
c) 30
d) 40
Answer: c) 30
4. Which method removes and returns the last element from a list?
a) remove()
b) pop()
c) delete()
d) discard()
Answer: b) pop()
5. What is the result of the following?
L = [1, 2, 3] * 2
print(L)
a) [1, 2, 3, 1, 2, 3]
b) [2, 4, 6]
c) [1, 2, 3, 2, 4, 6]
d) [1, 1, 2, 2, 3, 3]
Answer: a) [1, 2, 3, 1, 2, 3]
Tuple – 5 Questions
6. Tuples are:
a) Mutable
b) Immutable
c) Both a and b
d) None of these
Answer: b) Immutable
7. What is the output of the following?
T = (1, 2, 3)
T[0] = 5
a) (5, 2, 3)
b) (1, 2, 3)
c) Error
d) None
Answer: c) Error
8. Which of the following creates an empty tuple?
a) T = ()
b) T = tuple()
c) Both a and b
d) None
Answer: c) Both a and b
9. What will be the output?
T = (10, 20, 30)
print(T[1:])
a) (10,)
b) (10, 20)
c) (20, 30)
d) Error
Answer: c) (20, 30)
10. What will len((1, (2, 3), 4)) return?
a) 3
b) 4
c) 5
d) 2
Answer: a) 3
Dictionary – 5 Questions
11. What is the output of the following code?
d = {'a':1, 'b':2, 'a':3}
print(d['a'])
a) 1
b) 2
c) 3
d) Error
Answer: c) 3
12. Which method returns all the keys in a dictionary?
a) keys()
b) values()
c) items()
d) get()
Answer: a) keys()
13. What is the output of:
d = {'x':10, 'y':20}
print('z' in d)
a) True
b) False
c) None
d) Error
Answer: b) False
14. Which of the following is not allowed in a dictionary key?
a) String
b) Integer
c) List
d) Tuple
Answer: c) List
15. What does d.get('key', 0) do?
a) Returns the value of 'key' if it exists, otherwise returns 0
b) Deletes 'key'
c) Creates 'key' with value 0
d) Throws an error
Answer: a) Returns the value of 'key' if it exists, otherwise returns 0
String – 5 Questions
16. Strings in Python are:
a) Mutable
b) Immutable
c) Both a and b
d) None of these
Answer: b) Immutable
17. What is the output of:
s = "python"
print(s[::-1])
a) python
b) nohtyp
c) error
d) None
Answer: b) nohtyp
18. Which of the following methods checks if all characters are digits?
a) isalpha()
b) isdigit()
c) isalnum()
d) isnumeric()
Answer: b) isdigit()
19. What is the result of "Hello".upper()?
a) hello
b) HELLO
c) Hello
d) Error
Answer: b) HELLO
20. What will be the output?
s = "abc"
print(s * 3)
a) abcabcabc
b) abc3
c) Error
d) abc abc abc
Answer: a) abcabcabc
Python Lists, Tuples, Dictionaries & Strings – Set 2 (20
MCQs)
List – 6 Questions
1. What is the output of the following code?
L = [10, 20, 30]
L.insert(1, 15)
print(L)
a) [10, 15, 20, 30]
b) [15, 10, 20, 30]
c) [10, 20, 30, 15]
d) Error
Answer: a) [10, 15, 20, 30]
2. What does the following code output?
L = [1, 2, 3, 4]
print(L.index(3))
a) 2
b) 3
c) 1
d) Error
Answer: a) 2
3. Which method can be used to remove all items from a list?
a) delete()
b) remove()
c) clear()
d) discard()
Answer: c) clear()
4. What is the output of sorted([3, 1, 4, 2])?
a) [1, 2, 3, 4]
b) [4, 3, 2, 1]
c) (1, 2, 3, 4)
d) None
Answer: a) [1, 2, 3, 4]
5. What is the output?
L = [5, 2, 8, 1]
print(sorted(L, reverse=True))
a) [1, 2, 5, 8]
b) [8, 5, 2, 1]
c) [1, 8, 2, 5]
d) Error
Answer: b) [8, 5, 2, 1]
6. What happens when we call L.index(10) on a list that doesn’t contain 10?
a) Returns -1
b) Returns None
c) Raises ValueError
d) Prints nothing
Answer: c) Raises ValueError
Tuple – 4 Questions
7. What is the output?
t = (1, 2, 3, 2)
print(t.index(2))
a) 1
b) 2
c) 3
d) Error
Answer: a) 1
8. Which statement is true for tuples?
a) They are mutable
b) They can contain duplicate elements
c) They cannot be nested
d) They use {} brackets
Answer: b) They can contain duplicate elements
9. Which function can convert a tuple to a list?
a) list()
b) tuple()
c) set()
d) dict()
Answer: a) list()
10. What is the result of len((10,))?
a) 0
b) 1
c) 2
d) Error
Answer: b) 1
Dictionary – 5 Questions
11. What is the output?
d = {'a': 1, 'b': 2}
d['c'] = 3
print(d)
a) {'a':1, 'b':2}
b) {'a':1, 'b':2, 'c':3}
c) {'c':3}
d) Error
Answer: b) {'a':1, 'b':2, 'c':3}
12. What does d.pop('x', 0) do?
a) Removes 'x' and returns its value or 0 if not found
b) Adds 'x' with value 0
c) Raises KeyError
d) Returns list of keys
Answer: a) Removes 'x' and returns its value or 0 if not found
13. Which function returns the number of key-value pairs in a dictionary?
a) count()
b) size()
c) len()
d) sum()
Answer: c) len()
14. What is the output?
d = {'a':10, 'b':20, 'c':30}
print(sorted(d))
a) [10, 20, 30]
b) ['a', 'b', 'c']
c) [('a',10),('b',20),('c',30)]
d) Error
Answer: b) ['a', 'b', 'c']
15. What is returned by d.get('z') if 'z' does not exist?
a) 0
b) False
c) None
d) Error
Answer: c) None
String – 5 Questions
16. What will be the output of:
s = "apple orange banana"
print(s.split())
a) ['apple orange banana']
b) ['apple', 'orange', 'banana']
c) ['apple,orange,banana']
d) Error
Answer: b) ['apple', 'orange', 'banana']
17. What will be the output of:
s = "hello:world"
print(s.partition(":"))
a) ['hello', ':', 'world']
b) ('hello', ':', 'world')
c) ('hello', 'world')
d) Error
Answer: b) ('hello', ':', 'world')
18. What will "python".index('t') return?
a) 2
b) 3
c) 1
d) Error
Answer: a) 2
19. What is the output of:
s = "Banana"
print(sorted(s))
a) ['a', 'a', 'a', 'B', 'n', 'n']
b) ['B', 'a', 'n', 'a', 'n', 'a']
c) ('B', 'a', 'n', 'a', 'n', 'a')
d) Error
Answer: a) ['B', 'a', 'a', 'n', 'n', 'a'] (Sorted by Unicode order,
uppercase first)
20. What will the following return?
s = "I love Python"
print(s.split('o'))
a) ['I l', 've Pyth', 'n']
b) ['I love Python']
c) ['I', 'love', 'Python']
d) ['I l', 've Python']
Answer: a) ['I l', 've Pyth', 'n']