Python Basics: Mutable vs Immutable Objects
Ventsislav Yordanov
Feb 3, 2019·5 min read
Ventsislav Yordanov
Feb 3, 2019·5 min read
After reading this blog post you’ll know:
This section focuses on the "Data Types" of the Python programming. These Multiple Choice Questions (mcq) should be practiced to improve the Python programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1.Which statement is correct?
A. List is immutable && Tuple is mutable
B. List is mutable && Tuple is immutable
C. Both are Mutable.
D. Both are Immutable
View Answer
Explanation: List is mutable and Tuple is immutable. A mutable data type means that a python object of this type can be modified. An immutable object can’t. So, Option B is correct.
2.Suppose a list with name arr, contains 5 elements. You can get the 2nd element from the list using:
A. arr[-2]
B. arr[2]
C. arr[-1]
D. arr[1]
View Answer
Explanation: arr[0] will give first element while second element is given by arr[1] So, Option D is correct.
3.How to get last element of list in python? Suppose we have list with name arr, contains 5 elements.
A. arr[0]
B. arr[5]
C. arr[last]
D. arr[-1]
View Answer
Explanation: The arr[-n] syntax gets the nth-to-last element. So arr[-1] gets the last element, arr[-2] gets the second to last, etc. So, Option D is correct.
4.How to copy one list to another in python?
A. l1[] = l2[]
B. l1[] = l2
C. l1[] = l2[:]
D. l1 = l2
View Answer
Explanation: Option A and B syntax is incorrect while D will point both name to same list. Hence C is the best way to copy the one list to another. So, Option C is correct.
5.Suppose a tuple arr contains 10 elements. How can you set the 5th element of the tuple to 'Hello'?
A. arr[4] = 'Hello'
B. arr(4) = 'Hello'
C. arr[5] = 'Hello'
D. Elements of tuple cannot be changed
View Answer
Explanation: Tuples are immutable i.e the value cannot be changed. So, Option D is correct.
6.What type of data is: arr = [(1,1),(2,2),(3,3)]?
A. Array of tuples
B. Tuples of lists
C. List of tuples
D. Invalid type
View Answer
Explanation: Variable arr has tuples enclosed in a list. Hence it is list of tuples. So, Option C is correct.
7.Can tuple be used as dictionary key in python?
A. True
B. False
C. Tuple is not used in python
D. None of the above
View Answer
Explanation: A dictionary key must be immutable. You can use a tuple as a key if all of the elements contained in the tuple are immutable. If the tuple contains mutable objects, it cannot be used as a key. Hence a tuple to be used as a key can contain strings, numbers, and other tuples containing references to immutable objects. So, Option A is correct.
8.What is the output of the following program : print((1, 2) + (3, 4))
A. (1, 2), (3, 4)
B. (4, 6)
C. (1, 2, 3, 4)
D. Invalid Syntax
View Answer
Explanation: + operator concatenate tuples, it can be used to combine tuples to form a new tuple. So, Option C is correct.
9.Which one of the following is mutable data type?
A. set
B. int
C. str
D. tupl
View Answer
Explanation: set is one of the following is mutable data type. So, option A is correct.
10.Which one of the following is immutable data type?
A. list
B. set
C. int
D. dict
View Answer
Explanation: int one of the following is immutable data type. So, Option C is correct.
Also check :
* You must be logged in to add comment.
A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists and strings — and other things that behave like ordered sets — are called sequences.