Python Questions and Answers – Lists – 4

This set of Python Programming Interview Questions & Answers focuses on “Lists”.

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

  1. def f(i, values = []):
  2.     values.append(i)
  3.     return values
  4.  
  5. f(1)
  6. f(2)
  7. v = f(3)
  8. print(v)

a) [1] [2] [3]
b) [1] [1, 2] [1, 2, 3]
c) [1, 2, 3]
d) 1 2 3
View Answer

Answer: c
Explanation: Execute in the shell to verify
advertisement
advertisement

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

  1. names1 = ['Amir', 'Bala', 'Chales']
  2.  
  3. if 'amir' in names1:
  4.     print(1)
  5. else:
  6.     print(2)

a) None
b) 1
c) 2
d) Error
View Answer

Answer: c
Explanation: Execute in the shell to verify.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
  1. names1 = ['Amir', 'Bala', 'Charlie']
  2. names2 = [name.lower() for name in names1]
  3.  
  4. print(names2[2][0])

a) None
b) a
c) b
d) c
View Answer

Answer: d
Explanation: List Comprehension are a shorthand for creating new lists.
advertisement

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

  1. numbers = [1, 2, 3, 4]
  2.  
  3. numbers.append([5,6,7,8])
  4.  
  5. print(len(numbers))

a) 4
b) 5
c) 8
d) 12
View Answer

Answer: b
Explanation: A list is passed in append so the length is 5.

5. 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
View Answer

Answer: d
Explanation: In can be used in all data structures.

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

  1. list1 = [1, 2, 3, 4]
  2. list2 = [5, 6, 7, 8]
  3.  
  4. print(len(list1 + list2))

a) 2
b) 4
c) 5
d) 8
View Answer

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

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

  1. def addItem(listParam):
  2.     listParam += [1]
  3.  
  4. mylist = [1, 2, 3, 4]
  5. addItem(mylist)
  6. print(len(mylist))

a) 1
b) 4
c) 5
d) 8
View Answer

Answer: c
Explanation: + will append the element to the list.

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

  1. def increment_items(L, increment):
  2.     i = 0
  3.     while i < len(L):
  4.         L[i] = L[i] + increment
  5.         i = i + 1
  6.  
  7. values = [1, 2, 3]
  8. print(increment_items(values, 2))
  9. print(values)

a)

   None
   [3, 4, 5]

b)

   None
   [1, 2, 3]

c)

   [3, 4, 5]
   [1, 2, 3]

d)

   [3, 4, 5]
   None
View Answer
Answer: a
Explanation: Execute in the shell to verify.
 
 

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

  1. def example(L):
  2.     ''' (list) -> list
  3.     '''
  4.     i = 0
  5.     result = []
  6.     while i < len(L):
  7.         result.append(L[i])
  8.         i = i + 3
  9.     return result

a) Return a list containing every third item from L starting at index 0
b) Return an empty list
c) Return a list containing every third index from L starting at index 0
d) Return a list containing the items from L starting from index 0, omitting every third item
View Answer

Answer: a
Explanation: Run the code to get a better understanding with many arguments.

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

  1. veggies = ['carrot', 'broccoli', 'potato', 'asparagus']
  2. veggies.insert(veggies.index('broccoli'), 'celery')
  3. print(veggies)

a) [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’] Correct 1.00
b) [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]

c) [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]

d) [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]
View Answer

Answer: a
Explanation: Execute in the shell to verify.

Sanfoundry Global Education & Learning Series – Python.

To practice all programming interview questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.