0% found this document useful (0 votes)
22 views8 pages

MCQ Module 2

The document contains a series of questions and answers related to Python programming, specifically focusing on lists and dictionaries. It is divided into three difficulty levels: easy, medium, and difficult, with a total of 40 questions. Each question includes multiple-choice answers, with the correct answers marked.

Uploaded by

pavithra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

MCQ Module 2

The document contains a series of questions and answers related to Python programming, specifically focusing on lists and dictionaries. It is divided into three difficulty levels: easy, medium, and difficult, with a total of 40 questions. Each question includes multiple-choice answers, with the correct answers marked.

Uploaded by

pavithra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

MODULE 2:

Lists

Easy (10 Questions)

1. How do you create a list in Python?

A) {1, 2, 3}

B) (1, 2, 3)

C) [1, 2, 3] ✅

D) list(1, 2, 3)

2. What will len([1, 2, 3, 4]) return?

A) 3

B) 4 ✅

C) 5

D) Error

3. How do you access the first element of a list my_list?

A) my_list[0] ✅

B) my_list(0)

C) my_list{0}

D) my_list.first()

4. What will print([1, 2, 3] + [4, 5]) output?

A) [1, 2, 3, 4, 5] ✅

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

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

D) Error

5. How do you change the value of the second item in a list?

my_list = [10, 20, 30]

A) my_list(1) = 25

B) my_list[1] = 25 ✅

C) my_list{1} = 25

D) my_list.change(1, 25)

6. What does list(range(4)) return?

A) [0, 1, 2, 3] ✅
B) [1, 2, 3, 4]

C) (0, 1, 2, 3)

D) {0, 1, 2, 3}

7. What will print([1, 2, 3] * 2) output?

A) [1, 2, 3, 1, 2, 3] ✅

B) [2, 4, 6]

C) [[1, 2, 3], [1, 2, 3]]

D) Error

8. What method adds an element to the end of a list?

A) push()

B) add()

C) append() ✅

D) insert()

9. What is the output of print([1, 2, 3].index(2))?

A) 1 ✅

B) 2

C) 0

D) Error

10. What does my_list.pop() do?

A) Removes and returns the last element ✅

B) Removes and returns the first element

C) Deletes the list

D) Causes an error

Medium (6 Questions)

11. What will my_list = [1, 2, 3]; my_list.append([4, 5]) result in?

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

B) [1, 2, 3, [4, 5]] ✅

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

D) Error

12. What happens if you use my_list.remove(10) when 10 is not in my_list?


A) It removes the first element

B) It does nothing

C) It raises a ValueError ✅

D) It removes the last element

13. What does my_list.reverse() do?

A) Creates a new reversed list

B) Returns a reversed list

C) Reverses the list in-place ✅

D) Sorts the list

14. What will print([0, 1, 2][::-1]) output?

A) [0, 1, 2]

B) [2, 1, 0] ✅

C) [0, 2, 1]

D) Error

15. What does my_list.extend([4, 5]) do?

A) Adds [4, 5] as a single element

B) Extends my_list with individual elements ✅

C) Creates a new list

D) Returns a reversed list

16. What will print([1, 2, 3, 4][1:3]) output?

A) [1, 2, 3]

B) [2, 3] ✅

C) [3, 4]

D) Error

Difficult (4 Questions)

17. What happens if you modify a list inside a function?

def modify(lst):

lst.append(4)

my_list = [1, 2, 3]

modify(my_list)
print(my_list)

A) [1, 2, 3]

B) [1, 2, 3, 4] ✅

C) None

D) Error

Explanation: Lists are mutable and passed by reference, so my_list is modified inside the
function.

18. What will print([[0] * 3] * 3) output?

A) [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

B) [[0, 0, 0]] * 3

C) [[0, 0, 0], [0, 0, 0], [0, 0, 0]] but all rows reference the same list ✅

D) Error

Explanation: This creates three references to the same inner list, so modifying one row
affects all rows.

19. What does sorted([3, 2, 1], reverse=True) return?

A) [3, 2, 1] ✅

B) [1, 2, 3]

C) [3, 1, 2]

D) Error

Explanation: The reverse=True parameter sorts the list in descending order.

20. What does copy.deepcopy() do for a list?

A) Copies only references

B) Creates a new reference

C) Creates a deep copy ✅

D) Creates a shallow copy

Explanation: A deep copy creates a completely independent copy of a nested list.

Dictionaries and Structuring Data

Easy (10 Questions)

1. How do you define a dictionary in Python?

A) { 'name': 'Alice', 'age': 25 } ✅

B) [ 'name': 'Alice', 'age': 25 ]


C) ( 'name': 'Alice', 'age': 25 )

D) dict( 'name': 'Alice', 'age': 25 )

2. What will len({ 'a': 1, 'b': 2, 'c': 3 }) return?

A) 2

B) 3 ✅

C) 1

D) Error

3. What will my_dict = {'a': 1}; print(my_dict['a']) output?

A) 1 ✅

B) 'a'

C) None

D) Error

4. How do you check if a key exists in a dictionary?

A) if 'key' in my_dict: ✅

B) if my_dict.has('key'):

C) if 'key' exists my_dict:

D) if my_dict['key']:

5. What does my_dict.get('missing_key', 'default') return if 'missing_key' is not found?

A) None

B) 'default' ✅

C) 0

D) Error

6. What method removes a key-value pair from a dictionary?

A) delete()

B) remove()

C) pop() ✅

D) discard()

7. What does my_dict.keys() return?

A) A list of keys

B) A dictionary

C) A view object of keys ✅


D) An error

8. What is the output of print(list({'x': 1, 'y': 2}))?

A) ['x', 'y'] ✅

B) [1, 2]

C) [['x', 1], ['y', 2]]

D) Error

9. How do you merge two dictionaries in Python 3.9+?

A) dict1 + dict2

B) dict1.merge(dict2)

C) dict1 | dict2 ✅

D) dict1.append(dict2)

10. What will my_dict = {'a': 1, 'b': 2}; my_dict.clear(); print(my_dict) output?

A) {'a': 1, 'b': 2}

B) {} ✅

C) None

D) Error

Medium (6 Questions)

11. What does my_dict.values() return?

A) A list of values

B) A dictionary

C) A view object of values ✅

D) An error

12. What happens if you try to access a key that doesn’t exist using my_dict['missing']?

A) Returns None

B) Returns 0

C) Raises a KeyError ✅

D) Creates the key automatically

13. What does my_dict.items() return?

A) A list of key-value pairs

B) A dictionary
C) A view object of key-value pairs ✅

D) A tuple

14. What is the output of dict.fromkeys(['a', 'b'], 0)?

A) {'a': None, 'b': None}

B) {'a': 0, 'b': 0} ✅

C) {'a': '', 'b': ''}

D) Error

15. Which method can update multiple key-value pairs at once?

A) add()

B) append()

C) update() ✅

D) extend()

16. Given d = {'a': 1, 'b': 2}, what will print(d.popitem()) output?

A) ('a', 1)

B) ('b', 2) ✅

C) ('a', 2)

D) Error

Difficult (4 Questions)

17. What happens if you modify a dictionary while iterating over it?

A) The loop continues normally

B) It raises a RuntimeError ✅

C) It updates in real-time

D) It skips modified items

Explanation: Modifying a dictionary while iterating raises a RuntimeError to prevent


unexpected behavior.

18. What is the difference between defaultdict and a normal dictionary?

A) defaultdict provides a default value for missing keys ✅

B) defaultdict is immutable

C) defaultdict does not allow deletion

D) There is no difference
Explanation: defaultdict (from collections) assigns a default value for missing keys.

19. What will dict1 = {'x': 10}; dict2 = dict1; dict2['x'] = 20; print(dict1['x']) output?

A) 10

B) 20 ✅

C) None

D) Error

Explanation: dict1 and dict2 reference the same dictionary in memory.

20. What will d = {'a': 1, 'b': 2}; d.setdefault('c', 3); print(d) output?

A) {'a': 1, 'b': 2, 'c': 3} ✅

B) {'a': 1, 'b': 2}

C) {'a': 1, 'b': 2, 'c': None}

D) Error

Explanation: .setdefault('c', 3) adds 'c': 3 only if 'c' is not already in the dictionary.

You might also like