Python Programming Questions & Answers focuses on “Lists”.

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

def f(i, values = []):
values.append(i)
return values
 
f(1)
f(2)
v = f(3)
print(v)

A. [1] [2] [3]
B. [1] [1, 2] [1, 2, 3]
C. [1, 2, 3]
D. 1 2 3

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

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

names1 = ['Amir', 'Bala', 'Chales']
 
if 'amir' in names1:
print(1)
else:
print(2)

A. 1
B. 2
C. Error
D. None

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

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

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

A. None
B. a
C. b
D. c

Show Answer Answer:-D. c
Explanation List Comprehension are a shorthand for creating new lists.

Q 4. 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

Show Answer Answer:-B. 5
Explanation A list is passed in append so the length is 5.

Q 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

Show Answer Answer:-D. All of the mentioned
Explanation In can be used in all data structures.

Q 6. 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

Show Answer Answer:-D. 8
Explanation + appends all the elements individually into a new list.

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

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

A. 1
B. 4
C. 5
D. 8

Show Answer Answer:-C. 5
Explanation + will append the element to the list.

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

def increment_items(L, increment):
i = 0
while i < len(L):
L[i] = L[i] + increment
i = i + 1
 
values = [1, 2, 3]
print(increment_items(values, 2))
print(values)

A. None
[3, 4, 5]

B. None
[1, 2, 3]

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

D. [3, 4, 5]
None

Show Answer Answer:-A. None [3, 4, 5]
Explanation Execute in the shell to verify.

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

def example(L):
''' (list) -> list
'''
i = 0
result = []
while i < len(L):
result.append(L[i])
i = i + 3
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

Show Answer Answer:-A. Return a list containing every third item from L starting at index 0
Explanation Run the code to get a better understanding with many arguments.

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

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

A. [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’]
B. [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]
C. [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]
D. [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]

Show Answer Answer:-A. [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’]
Explanation Execute in the shell to verify.

Leave a Reply

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

Author

quizsquestion@gmail.com

Related Posts

Python Questions and Answers – Exception Handling – 3

Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 3”. Q 1. What happens if the file is not...

Read out all

Python Questions and Answers – Exception Handling – 2

Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”. Q 1. The following Python code will result in...

Read out all

C++ Programming Questions and Answers – Exception Handling – 2

C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”. Q 1. Where should we place catch block...

Read out all

C++ Programming Questions and Answers – Exceptions That Are Not Errors

C++ MCQs (multiple choice questions) focuses on “Exceptions That Are Not Errors”. One shall practice these MCQs to improve their C++ programming...

Read out all

Python Questions and Answers – Encapsulation

Python Multiple Choice Questions & Answers (MCQs) focuses on “Encapsulation”. Q 1. Which of these is not a fundamental features of OOP?A....

Read out all

You cannot copy content of this page