0% found this document useful (0 votes)
7 views6 pages

Python Programming MCQ Question Bank

Uploaded by

amoghmaddela38
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)
7 views6 pages

Python Programming MCQ Question Bank

Uploaded by

amoghmaddela38
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

SIVA SIVANI DEGREE COLLEGE

PYTHON PROGRAMMING FOR MANAGERS

BBA-2(CH&K)[QUESTION BANK]

MULTIPLE CHOICE

1. Which method is used to insert an element at a specific position in a list?


a) add()
b) insert()
c) append()
d) extend()
Answer: b) insert()
2. What is the output of len([1, 2, [3, 4], 5])?
a) 4
b) 5
c) 3
d) 6
Answer: a) 4
3. Which method is used to reverse the elements of a list in place?
a) reverse()
b) reversed()
c) flip()
d) invert()
Answer: a) reverse()
4. What does [x for x in range(10) if x%2==0] do?
a) Creates a list of numbers 0 to 9
b) Creates a list of even numbers from 0 to 9
c) Creates a list of odd numbers from 0 to 9
d) Returns a tuple of even numbers
Answer: b) Creates a list of even numbers from 0 to 9
5. Which operator is used to concatenate two lists?
a) +
b) *
c) &
d) |
Answer: a) +
6. Which of the following is used to create a tuple in Python?
a) ()
b) tuple()
c) Both a and b
d) []
Answer: c) Both a and b
7. Which of the following operations is not allowed on a tuple?
a) Indexing
b) Slicing
c) Updating an element
d) Concatenation
Answer: c) Updating an element
8. What will be the output of len((1, 2, 3, 4))?
a) 3
b) 4
c) 5
d) Error
Answer: b) 4
9. Which tuple method returns the index of the first occurrence of a value?
a) index()
b) find()
c) locate()
d) position()
Answer: a) index()
10. Which of the following is a correct statement about tuples compared to lists?
a) Tuples are mutable and lists are immutable
b) Tuples are immutable and lists are mutable
c) Both are mutable
d) Both are immutable
Answer: b) Tuples are immutable and lists are mutable
11. Which of the following is used to create an empty dictionary?
a) {}
b) dict()
c) Both a and b
d) []
Answer: c) Both a and b
12. How do you access the value associated with a key in a dictionary?
a) [Link]
b) dict[key]
c) [Link](key)
d) [Link]()
Answer: b) dict[key]
13. Which dictionary method removes a key-value pair and returns its value?
a) remove()
b) pop()
c) delete()
d) discard()
Answer: b) pop()
14. Which of the following is mutable in Python?
a) Tuple
b) Dictionary
c) String
d) Integer
Answer: b) Dictionary
15. Which method returns a view object of all key-value pairs in a dictionary?
a) items()
b) keys()
c) values()
d) pairs()
Answer: a) items()
16. Which mode is used to open a file for reading only?
a) 'r'
b) 'w'
c) 'a'
d) 'x'
Answer: a) 'r'
17. method reads all lines of a file as a list?
a) read()
b) readline()
c) readlines()
d) fetchall()
Answer: c) readlines()
18. What does the 'a' mode do when opening a file?
a) Reads the file
b) Overwrites the file
c) Appends data to the end of the file
d) Deletes the file
Answer: c) Appends data to the end of the file
19. Which module is used to read and write CSV files in Python?
a) json
b) csv
c) pandas
d) os
Answer: b) csv
20. Which method writes a list of strings to a file, adding newlines automatically?
a) write()
b) writelines()
c) append()
d) readlines()
Answer: b) writelines()

.
FILL IN THE BLANKS

1. The extend() method is used to add multiple elements from another list to the end of a
list.
2. mylist[-1] accesses the last element of the list.
3. List comprehensions can include conditions to filter elements while creating a new list.
4. The count() method returns the number of times a specific element appears in a list.
5. Nested lists can be used to represent multi-dimensional structures like matrices in
Python.
6. Tuples are immutable sequences, meaning their elements cannot be changed after
creation.
7. Tuple slicing tup[start:end] returns a new tuple containing elements from index start to
end-1.
8. The count() method returns the number of times a specified value occurs in a tuple.
9. A tuple can contain elements of different data types, including other tuples.
10. Unlike lists, tuples use less memory and are faster for iteration due to their immutability.
11. Dictionaries store data in key-value pairs, allowing fast access by key.
12. The get() method returns the value for a key if it exists, otherwise returns None or a
default value.
13. The update() method adds key-value pairs from another dictionary or modifies existing
keys.
14. Keys in a dictionary must be immutable types such as strings, numbers, or tuples.
15. Unlike tuples, dictionaries are mutable and do not maintain order in versions prior to
Python 3.7.
16. The 'w' mode is used to create a new file or overwrite an existing file.
17. The read() method reads the entire content of a file as a single string.
18. The with statement is used in file handling to automatically close the file after
operations.
19. In CSV handling, [Link](file) reads the contents of the CSV file as a list of rows.
20. To write data to a CSV file, [Link](file).writerow(row) writes a single row to the
CSV file.

SHORT QUESTIONS

1. How can you create a list in Python?


Lists are created using square brackets []. Example: my_list = [1, 2, 3, "apple"]. Lists can
hold different data types and are mutable, meaning you can add, remove, or change
elements after creation.
2. What are basic list operations in Python?
Common list operations include concatenation (+), repetition (*), membership testing
(in), and length (len(list)). Example: [1, 2] + [3, 4] gives [1, 2, 3, 4].
3. What is a tuple and how is it different from a list?
A tuple is an immutable sequence created using parentheses ( ), e.g., t = (1, 2, 3). Unlike
lists, tuples cannot be modified after creation. They are generally faster and used when
fixed data is needed.
4. How does indexing and slicing work in tuples?
Tuples support indexing and slicing similar to lists. Example: t = (10, 20, 30, 40) → t[1]
gives 20 and t[1:3] gives (20, 30). Since tuples are immutable, you cannot change
elements.

5. How do you create and access elements in a dictionary?


Dictionaries store data as key-value pairs inside curly braces {}. Example: student =
{"name": "Rama", "age": 20}. Values are accessed by keys, like student["name"] →
"Rama".
6. What is the difference between tuples and dictionaries?
Tuples are ordered collections accessed using numeric indexes, while dictionaries are
unordered collections accessed using keys. Tuples are immutable, but dictionary values
can be updated or deleted.

7. What are the types of files in Python?


Python mainly works with text files (store data in human-readable form, e.g., .txt, .csv)
and binary files (store data in byte format, e.g., images, videos). Both can be handled
using the open() function with different modes.
8. How do you read data from a text file in Python?
Use the open() function with mode "r". Example:

f = open("[Link]", "r")
content = [Link]()
[Link]()

This reads the entire file content as a string.

9. How can you read and write CSV files in Python?


Python’s csv module is used. Writing:

import csv
with open("[Link]", "w", newline="") as f:
writer = [Link](f)
[Link](["Name", "Age"])

Similarly, [Link] is used for reading CSV files.


10. What are the differences between list, tuple and dictionary.

Feature List Tuple Dictionary


Definition Ordered collection of Ordered, immutable Unordered collection of
elements collection key–value pairs
Syntax list = [1, 2, 3] tuple = (1, 2, 3) dict = {"a": 1, "b": 2}
Mutability Mutable (can be Immutable (cannot be Mutable (values can be
changed) changed) changed)
Indexing Accessed by index Accessed by index (e.g., Accessed by key (e.g.,
(e.g., list[0]) tuple[0]) dict["a"])
Duplicates Allows duplicate Allows duplicate Keys must be unique
elements elements (values can repeat)
Order Preserves insertion Preserves insertion order Preserves insertion order
order (Python 3.7+)
Performance Slower than tuples Faster than lists Fast lookups using hash
(immutable) keys

You might also like