Thursday 7 January 2021

Python List Interview questions - Multiple choice

 List Interview questions:

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

1. Which of the following commands will create a list?

a) list1 = list()

b) list1 = []

c) list1 = list([1, 2, 3])

d) all of the mentioned


Answer: d

Explanation: Execute in the shell to verify


2. What is the output when we execute list(“hello”)?

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

b) [‘hello’]

c) [‘llo’]

d) [‘olleh’]


Answer: a

Explanation: Execute in the shell to verify.


3. Suppose list_example is [‘h’,’e’,’l’,’l’,’o’]  what is len(list_example)?

a) 5

b) 4

c) None

d) Error


Answer: a

Explanation: Execute in the shell and verify.


4. Suppose list1 is [2445, 133, 12454, 123]  what is max(list1)?

a) 2445

b) 133

c) 12454

d) 123


Answer: c

Explanation: Max returns the maximum element in the list.


5. Suppose list_1 is [3, 5, 25, 1, 4]  what is min(list_1)?

a) 3

b) 5

c) 25

d) 1


Answer: d

Explanation: Min returns the minimum element in the list.


6. Suppose list_1 is [1, 5, 9]  what is sum(list_1)?

a) 1

b) 9

c) 15

d) Error


Answer: c

Explanation: Sum returns the sum of all elements in the list.


7. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?

a) 0

b) 4

c) 1

d) 2


Answer: d

Explanation: Execute in the shell to verify.


8. Suppose list_1 is [4, 2, 2, 4, 5, 2, 1, 0]

     Which of the following is the correct syntax for slicing operation?

a) print(list_1[0])

b) print(list_1[ :2])

c) print(list_1[ :-2])

d) all of the mentioned


Answer: d

Explanation: Slicing is allowed in lists just as in the case of strings.


9. Suppose list_1 is [2, 33, 222, 14, 25]  What is list_1[-1]?

a) Error

b) None

c) 25

d) 2


Answer: c

Explanation: -1 corresponds to the last index in the list.


10. Suppose list1 is [2, 33, 222, 14, 25] What is list1[ :-1] ?

a) [2, 33, 222, 14, 25] 

b) 25

c) [25, 14, 222, 33, 2]

d) [2, 33, 222, 14]


Answer: d

Explanation: Execute in the shell to verify


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

>>>names = ["Narendra", "Boyina", "raj", "Mahalakshmi"]

>>>print(names[-1][-1])

a) M

b) Mahalakshmi

c) Error

d) i


Answer: d

Explanation: Execute in the shell to verify.


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

    names_1 = ["Narendra", "Bhaskarao", "raj", "Mahalakshmi"]
    names_2 = names_1  # shallow copy
    names_3 = names_1[:] # deep copy
    names_2[0] = 'Surendra'
    names_3[1] = "Udaya Bhaskara rao"

    print(names_1)
    print(names_2)
    print(names_3)

    a)  ['Surendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
        ['Surendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
        ['Narendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
    b)  ['Surendra', 'Bhaskarao', 'raj', 'Mahalakshmi']
        ['Surendra', 'Bhaskarao', 'raj', 'Mahalakshmi']
        ['Narendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
    c) Error
    d) None of the above



Answer: b

Explanation:  names_1 and names_2 are using the same memory location reason: Shallow copy

        if you change in names_2 it will affect names_1 vice versa       reason: Shallow copy 

       But names_3 used a different memory location reason: Deep copy

      if you do changes in names_3 it will not affect the names_1reason: Deep copy


13. Suppose list_1 is [1, 3, 2] What is list_1 * 2?

a) [2, 6, 4]

b) [1, 3, 2, 1, 3]

c) [1, 3, 2, 1, 3, 2]

d) [1, 3, 2, 3, 2, 1]


Answer: c

Explanation: Execute in the shell and verify.


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

>>>list1 = [11, 2, 23]

>>>list2 = [11, 2, 2]

>>>list1 < list2 is

a) True

b) False

c) Error

d) None


Answer: b

Explanation: Elements are compared one by one.


15. To add a new element to a list, which command we have to use?

a) list1.add(5)

b) list1.append(5)

c) list1.addLast(5)

d) list1.addEnd(5)


Answer: b

Explanation: We use the function append() function, to add an element to the list.


16. To insert 5 to the third position in list1, which command do we have to use?

a) list1.add(3, 5)

b) list1.insert(2, 5)

c) list1.insert(3, 5)

d)  list1.append(3, 5)


Answer: c

Explanation: Execute in the shell to verify.


17. To remove the string “hello” from list1, which command do we have to use?

a) list1.remove(hello) 

b)  list1.remove(“hello”)

c) list1.removeAll(“hello”)

d) list1.removeOne(“hello”)


Answer: b

Explanation:  hello is a variable, "hello" is a string, we have to remove the string, so we have to              give the element as a string


18. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?

a) 0

b) -1

c) 4

d) 2


Answer: d

Explanation: By default, the index is always considered from the left-hand side (0).


19. Suppose list_1 is [3, 4, 5, [5, 20, 15, 5],  5, 25, 1, 3], what is list_1.count(5)?

a) 4

b) 6

c) 1

d) 2


Answer: d

Explanation: Execute in the shell to verify.


20. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?

a) [3, 4, 5, 20, 5, 25, 1, 3]

b) [1, 3, 3, 4, 5, 5, 20, 25]

c) [3, 1, 25, 5, 20, 5, 4, 3] 

d) [25, 20, 5, 5, 4, 3, 3, 1]


Answer: c

Explanation: Execute in the shell to verify.


21. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?

a) [3,  4,  5,  20,  5,  25,  1,  3, [ 34, 5]]

b) [3,  4,  5,  20,  5,  25,  1,  3,  34,  5]

c) [25,  20,  5,  5,  4,  3,  3,  1,  34,  5]

d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]


Answer: b

Explanation: Execute in the shell to verify.

22. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is listExample after listExample.pop()?

a) [3, 4, 5, 20, 5, 25, 1]

b) [1, 3, 3, 4, 5, 5, 20, 25]

c) [3, 5, 20, 5, 25, 1, 3]

d) [1, 3, 4, 5, 20, 5, 25]


Answer: a

Explanation: pop() function will remove the last element in the list.


23. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?

a) [3, 4, 5, 20, 5, 25, 1, 3]

b) [1, 3, 3, 4, 5, 5, 20, 25]

c) [3, 5, 20, 5, 25, 1, 3]

d) [1, 3, 4, 5, 20, 5, 25]


Answer: c

Explanation: Generally pop() function will remove the last element in the list. 

        But inside the pop() function, if you provide an index then it will remove based on that index.


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

>>>"Welcome to Python".split()

a)  (“Welcome”, “to”, “Python”)

b)  [“Welcome”, “to”, “Python”]

c) {“Welcome”, “to”, “Python”}

d) “Welcome”, “to”, “Python”


Answer: b

Explanation: split() function returns the elements in a list.


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

>>>list_1 = [1, 3]

>>>list_2 = list_1

>>>list_1[0] = 4

>>>print(list_2)

a) [1, 3]

b) [4, 3]

c) [1, 4]

d) [1, 3, 4]


Answer: b

Explanation:  list_1 and list_2 are using the same memory location reason: Shallow copy

        if you change in list_1 it will affect list_2 vice versa       reason: Shallow copy 


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

numbers = [1, 2, 3, 4]

numbers.append([5,6,7,8])

 print(len(numbers))

a) 4

b) 5

c) 8

d) 12


Answer: b

Explanation: A list is passed in append().

            append() function considers the entire list as a single element so the length is 5.


27. To which of the following the “in” operator can be used to check if an item is in it?

a) Lists

b) Dictionary

c) Set

d) All of the mentioned


Answer: d

Explanation: membership operator (in) can be used in all data structures (Datatypes).


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

list1 = [1, 2, 3, 4]

list2 = [5, 6, 7, 8]

print(len(list1 + list2))

a) 2

b) 4

c) 5

d) 8


Answer: d

Explanation: + appends all the elements individually into a new list.

 

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

veggies = ['carrot', 'drumsticks', 'potato', 'asparagus']

veggies.insert(veggies.index('drumsticks'), 'ladyfinger')

print(veggies)

a) [‘carrot’, ‘ladyfinger’, ‘potato’, ‘asparagus’]

b)  [‘carrot’, ‘ladyfinger’, 'drumsticks', 'potato', 'asparagus'] 

c) [‘carrot’, ‘drumsticks’, ‘ladyfinger’, ‘potato’, ‘asparagus’]

d) [‘ladyfinger’, ‘carrot’, ‘drumsticks’, ‘potato’, ‘asparagus’]


Answer: b

Explanation: Execute in the shell to verify.


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

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

 print(data[1][1][0])

a) 1

b) 2

c) 7

d) 5


Answer: C

Explanation: Execute in the shell to verify.


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

points = [[1, 2], [3, 1.5], [0.5, 0.5]]

points.sort()

print(points)

a) [[1, 2], [3, 1.5], [0.5, 0.5]]

b) [[3, 1.5], [1, 2], [0.5, 0.5]]

c) [[0.5, 0.5], [1, 2], [3, 1.5]]

d) [[0.5, 0.5], [3, 1.5], [1, 2]]


Answer: c

Explanation: Execute in the shell to verify.


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

s="a@b@c@d"

a=list(s.partition("@"))

print(a)

b=list(s.split("@"))

print(b)

a) [‘a’, ’b’, ’c’, ’d’]

[‘a’, ’b’, ’c’, ’d’]

b) [‘a’, ’@’, ’b’, ’@’, ’c’, ’@’, ’d’]

[‘a’, ’b’, ’c’, ’d’]

c) [‘a’,’@’,’b@c@d’]

[‘a’, ’b’, ’c’, ’d’]

d) [‘a’,’@’,’b@c@d’]

[‘a’, ’@’, ’b’, ’@’, ’c’, ’@’, ’d’]


Answer: c

Explanation: The partition function only splits for the first parameter along with 

the separator while the split function splits for the number of times given in the second argument but without the separator.



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

a=[[]]*3

a[1].append(7)

print(a)

a) Syntax error

b) [[7], [7], [7]]

c) [[7], [], []]

d) [[],7, [], []]


Answer: b

Explanation: The first line of the code creates multiple reference copies of the sublist. 

Hence when 7 is appended, it gets appended to all the sublists.


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

lst=[3,4,6,1,2]

lst[1:2]=[7,8]

print(lst)

a) [3, 7, 8, 6, 1, 2]

b) Syntax error

c) [3,[7,8],6,1,2]

d) [3,4,6,7,8]


Answer: a

Explanation: In the piece of code,  the slice assignment has been implemented. 

The sliced list is replaced by the assigned elements in the list. Type in Python shell to verify.


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

a=[1,2,3]

b = a.append(4)

print(a)

print(b)

a) [1,2,3,4]

[1,2,3,4]

b) [1, 2, 3, 4]

None

c) Syntax error

d) [1,2,3]

[1,2,3,4]


Answer: b

Explanation: The append function on lists doesn’t return anything. Thus the value of b is None.

  

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

>>> a = [14,52,7]

>>>> b = a.copy()  # deep copy

>>> b is a

         a) True

b) False


Answer: b

Explanation: here a data will be copied to b(deep copy) so it will create a separate memory location


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

a=[13,56,17]

a.append([87])

a.extend([45,67])

print(a)

a) [13, 56, 17, [87], 45, 67]

b) [13, 56, 17, 87, 45, 67]

c) [13, 56, 17, 87,[ 45, 67]]

d) [13, 56, 17, [87], [45, 67]]


Answer: a

Explanation: The append function simply adds its arguments to the list as it is 

while extend function extends its arguments and later appends it.


42. What is the output of the following piece of code?

a=list((45,)*4)

print((45)*4)

print(a)

a) 180

[(45),(45),(45),(45)]

b) (45,45,45,45)

[45,45,45,45]

c) 180

[45,45,45,45]

d) Syntax error


Answer: c

Explanation: (45) is an int while (45,) is a tuple of one element. Thus when a tuple is multiplied, 

it created references of itself which are later converted into a list.


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

word_1 = "Apple"

word_2 = "Apple"

list_1 = [1,2,3]

list_2 = [1,2,3]

print(word_1 is word_2)

print(list_1 is list_2)

a) True

True

b) False

True

c) False

False

d) True

False


Answer: d

Explanation: In the above case, both lists are equivalent but not identical as they have different objects.


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

places = ['Bangalore', 'Mumbai', 'Delhi']

places1 = places      # shallow copy

        places1[1]="Pune"

places2 = places[ : ]   # deep copy

places2[2]="Hyderabad"

print(places)

a) [‘Bangalore’, ‘Pune’, ‘Hyderabad’]

b) [‘Bangalore’, ‘Pune’, ‘Delhi’]

c) [‘Bangalore’, ‘Mumbai’, ‘Delhi’]

d) [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’]


Answer: b

Explanation: places1 is an alias for the list of places. Hence, any change made to places1 is reflected in places. 

places2 is a copy of the list of places. Thus, any change made to places2 isn’t reflected in places.


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

a=["Apple","Ball","Cobra"]

a.sort(key = len)

print(a)

a) [‘Apple’, ‘Ball’, ‘Cobra’]

b) [‘Ball’, ‘Apple’, ‘Cobra’]

c) [‘Cobra’, ‘Apple’, ‘Ball’]

d) Invalid syntax for sort()


Answer: b

Explanation: The syntax isn’t invalid and the list is sorted according to the length of 

the strings in the list since the key is given as length.



No comments:

Post a Comment