Python Quizs Question and Answers – Lists – 2

Q 1. Suppose list1 is [1, 3, 2], What is list1 * 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]

Show Answer Answer:-C. [1, 3, 2, 1, 3, 2]
Explanation Execute in the shell and verify.

Q 2. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:
A. [0, 1, 2, 3]
B. [0.0, 0.5, 1.0, 1.5]
C. [0, 1, 2, 3, 4]
D. [0.0, 0.5, 1.0, 1.5, 2.0]

Show Answer Answer:-B. [0.0, 0.5, 1.0, 1.5]
Explanation Execute in the shell to verify.

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

  1. >>>list1 = [11, 2, 23]
  2. >>>list2 = [11, 2, 2]
  3. >>>list1 < list2

A. True
B. False
C. Error
D. None

Show Answer Answer:-B. False
Explanation Elements are compared one by one.

Q 4. To add a new element to a list we use which command?
A. list1.add(5)
B. list1.addEnd(5)
C. list1.addLast(5)
D. list1.append(5)

Show Answer Answer:-D. list1.append(5)
Explanation We use the function append to add an element to the list.

Q 5. To insert 5 to the third position in list1, we use which command?
A. list1.insert(3, 5)
B. list1.insert(2, 5)
C. list1.add(3, 5)
D. list1.append(3, 5)

Show Answer Answer:-B. list1.insert(2, 5)
Explanation Execute in the shell to verify.

Q 6. To remove string “hello” from list1, we use which command?
A. list1.remove(hello)
B. list1.remove(“hello”)
C. list1.removeAll(“hello”)
D. list1.removeOne(“hello”)

Show Answer Answer:-B. list1.remove(“hello”)
Explanation Execute in the shell to verify.

Q 7. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?
A. 0
B. 1
C. 4
D. 2

Show Answer Answer:-D. 2
Explanation Execute help(list.index) to get details.

Q 8. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?
A. 0
B. 4
C. 1
D. 2

Show Answer Answer:-D. 2
Explanation Execute in the shell to verify.

Q 9. What will be the output of the following Python code?

  1. names1 = [‘Amir’, ‘Bear’, ‘Charlton’, ‘Daman’]
  2. names2 = names1
  3. names3 = names1[:]
  4.  
  5. names2[0] = ‘Alice’
  6. names3[1] = ‘Bob’
  7.  
  8. sum = 0
  9. for ls in (names1, names2, names3):
  10. if ls[0] == ‘Alice’:
  11. sum += 1
  12. if ls[1] == ‘Bob’:
  13. sum += 10
  14.  
  15. print sum

A. 11
B. 12
C. 21
D. 22

Show Answer Answer:-B. 12
Explanation When assigning names1 to names2, we create a second reference to the same list. Changes to names2 affect names1. When assigning the slice of all elements in names1 to names3, we are creating a full copy of names1 which can be modified independently.

Q 10. What will be the output of the following Python code?

  1. >>>names = [‘Amir’, ‘Bear’, ‘Charlton’, ‘Daman’]
  2. >>>print(names[-1][-1])

A. A
B. Daman
C. Error
D. n

Show Answer Answer:-D. n
Explanation Execute in the shell to verify.

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page