Thursday, January 7, 2021

Python Tuple Interview questions - Multiple choice

 Tuple Interview questions:

========================================================================


1. Suppose t = (1, 2, 4, 3), which of the following is incorrect?

a) print(t[3])

b) t[3] = 45

c) print(max(t))

d) print(len(t))


Answer: b

Explanation: Values cannot be modified in the case of the tuple, that is, a tuple is immutable.


2. What will be the output of the following Python code?

>>>t=(1,2,4,3)

>>>t[1:3]

a) (1, 2)

b) (1, 2, 4)

c) (2, 4)

d) (2, 4, 3)


Answer: c

Explanation: Slicing in tuples takes place just as it does in strings.


3. What will be the output of the following Python code?

>>>t=(1,2,4,3)

>>>t[1:-1]

a) (2, 4, 3)

b) (1, 2, 4)

c) [2, 4]

d) (2, 4) 


Answer: d

Explanation: Slicing in tuples takes place just as it does in strings.


4. What will be the output of the following Python code?

>>>t = (1, 2)

>>> t*2

a) (1, 2, 1, 2)

b) [1, 2, 1, 2]

c) (1, 1, 2, 2)

d) [1, 1, 2, 2]


Answer: a

Explanation: * operator concatenates tuple.


5. What will be the output of the following Python code?

>>>t1 = (1, 2, 4, 3)

>>>t2 = (1, 2, 3, 4)

>>>t1 < t2

a) True

b) False

c) Error

d) None


Answer: b

Explanation: Elements are compared one by one in this case.


6. What will be the output of the following Python code?

>>>my_tuple = (1, 2, 3, 4)

>>>my_tuple.append( (5, 6, 7) )

>>>print len(my_tuple)

a) 1

b) 2

c) 5

d) Error


Answer: d

Explanation: Tuples are immutable and don’t have an append method. An exception is thrown in this case.


7. What is the data type of (1)?

a) Tuple

b) Integer

c) List

d) Both tuple and integer


Answer: b

Explanation: A tuple of one element must be created as (1,).


12. If a=(1,2,3,4), a[1:-1] is _________

a) Error, tuple slicing doesn’t exist

b) [2,3]

c) (2,3,4)

d) (2,3)


Answer: d

Explanation: Tuple slicing exists and a[1:-1] returns (2,3).


13. What will be the output of the following Python code?

>>> a=(1,2,(4,5))

>>> b=(1,2,(3,4))

>>> a<b

a) False

b) True

c) Error, < operator is not valid for tuples

d) Error, < operator is valid for tuples but not if there are sub-tuples


Answer: a

Explanation: Since the first element in the sub-tuple of a is larger than the first element in the sub-tuple of b, False is printed.


14. What will be the output of the following Python code?

>>> a=("Check")*3

>>> a

a) (‘Check’,’Check’,’Check’)

b) * Operator not valid for tuples

c) (‘CheckCheckCheck’)

d) Syntax error


Answer: c

Explanation: Here (“Check”) is a string, not a tuple because there is no comma after the element.


15. What will be the output of the following Python code?

>>> a=(1,2,3,4)

>>> del(a[2])

a) Now, a=(1,2,4)

b) Now, a=(1,3,4)

c) Now a=(3,4)

d) Error as tuple is immutable


Answer: d

Explanation: ‘tuple’ object doesn’t support item deletion.


16. What will be the output of the following Python code?

>>> a=(2, 3, 4)

>>> sum(a, 3)

a) Too many arguments for sum() method

b) The method sum() doesn’t exist for tuples

c) 12

d) 9


Answer: c

Explanation: In the above case, 3 is the starting value to which the sum of the tuple is added.


17. Is the following Python code valid?

>>> a=(1,2,3,4)

>>> del a

a) No because tuple is immutable

b) Yes, first element in the tuple is deleted

c) Yes, the entire tuple is deleted

d) No, invalid syntax for del method


Answer: c

Explanation: The command del a deletes the entire tuple.


18. What type of data is: a=[(1,1),(2,4),(3,9)]?

a) Array of tuples

b) List of tuples

c) Tuples of lists

d) Invalid type


Answer: b

Explanation: The variable has tuples enclosed in a list making it a list of tuples.


19. What will be the output of the following Python code?

>>> a=(0,1,2,3,4)

>>> b=slice(0,2)

>>> a[b]

a) Invalid syntax for slicing

b) [0,2]

c) (0,1)

d) (0,2)


Answer: c

Explanation: The method illustrated in the above piece of code is that of the naming slices.


20. Is the following Python code valid?

>>> a = (1, 2, 3)

>>> b = ('A', 'B', 'C')

>>> c = tuple(zip(a, b))

a) Yes, c will be ((1,2,3),(‘A’,’B’,’C’))

b) Yes, c will be ((1, 'A'), (2, 'B'), (3, 'C'))

c) No because tuples are immutable

d) No because the syntax for zip function isn’t valid


Answer: b

Explanation: The zip function combines individual elements of two iterables into tuples. 


21. Is the following Python code valid?

>>> a, b, c=1, 2, 3

>>> a, b, c

a) Yes, [1,2,3] is printed

b) No, invalid syntax

c) Yes, (1,2,3) is printed

d) 1 is printed


Answer: c

Explanation: A tuple needn’t be enclosed in parenthesis.


 

23. Is the following Python code valid?

>>> a,b=1,2,3

a) Yes, this is an example of tuple unpacking. a=1 and b=2

b) Yes, this is an example of tuple unpacking. a=(1,2) and b=3

c) No, too many values to unpack

d) Yes, this is an example of tuple unpacking. a=1 and b=(2,3)


Answer: c

Explanation: For unpacking to happen, the number of values on the right-hand side must be equal to the number of variables on the left-hand side.


24. What will be the output of the following Python code?

>>> a = (1,2)

>>> b = (3,4)

>>> c = a+b

>>> c

a) (4,6)

b) (1, 2, 3, 4)

c) Error as tuples are immutable

d) None


Answer: b

Explanation: In the above piece of code, the values of the tuples aren’t being changed. 

Both the tuples are simply concatenated.


25. What will be the output of the following Python code?

>>> a, b = 6, 7

>>> a, b = b, a

>>> a, b

a) (6,7)

b) Invalid syntax

c) (7,6)

d) Nothing is printed


Answer: c

Explanation: The above piece of code illustrates the unpacking of variables.


27. Tuples can’t be made keys of a dictionary.

a) True

b) False


Answer: b

Explanation: Tuples can be made keys of a dictionary because they are hashable.


28. Is the following Python code valid?

>>> a=2,3,4,5

>>> a

a) Yes, 2 is printed

b) Yes, [2,3,4,5] is printed

c) No, too many values to unpack

d) Yes, (2,3,4,5) is printed


Answer: d

Explanation: A tuple needn’t be enclosed in parenthesis.


29. What will be the output of the following Python code?

>>> a=(2,3,1,5)

>>> a.sort()

>>> print(a)

a) (1,2,3,5)

b) (2,3,1,5)

c) None

d) Error, tuple has no attribute sort


Answer: d

Explanation: A tuple is immutable thus it doesn’t have a sort attribute.


30. Is the following Python code valid?

>>> a = (1, 2, 3)

>>> b = a.update(4,)

a) Yes, a=(1,2,3,4) and b=(1,2,3,4)

b) Yes, a=(1,2,3) and b=(1,2,3,4)

c) No because tuples are immutable

d) No because wrong syntax for update() method


Answer: c

Explanation: Tuple doesn’t have any update() attribute because it is immutable.


31. What will be the output of the following Python code?

>>> a=[(2,4),(1,2),(3,9)]

>>> a.sort()

>>> print(a)

a) [(1, 2), (2, 4), (3, 9)]

b) [(2,4),(1,2),(3,9)]

c) Error because tuples are immutable

d) Error, tuple has no sort attribute


Answer: d

Explanation: A list of tuples is a list itself. Hence items on a list can be sorted.

Request: If you find this information useful, please provide your valuable comments.

No comments:

Post a Comment