Programming in Python Mcqs With Answers (2022) Csd
Programming in Python Mcqs With Answers (2022) Csd
Thiruchirrappalli
QUESTION BANK
PROGRAMMING IN PYTHON
UNIT I
b) Static typing
c) Interpreted language
d) Compiler-based language
a) python script.py
b) run script.py
c) execute script.py
d) start script.py
a) 1variable
b) variable_1
c) global
d) break
4. Which of the following is a reserved keyword in Python?
a) for
b) while
c) if
a) var x = 5
b) x = 5
c) int x = 5
d) variable x = 5
c) They are used to explain the code and make it more readable.
a) Spaces
b) Tabs
a) input()
b) get_input()
c) user_input()
d) read()
a) print()
b) output()
c) display()
d) show()
a) ^
b) *
c) %
d) **
a) Integer
b) Float
c) String
d) Boolean
a) str()
b) int()
c) float()
d) bool()
Answers:
1. c) Interpreted language
2. a) python script.py
3. b) variable_1
5. b) x = 5
6. c) They are used to explain the code and make it more readable.
7. a) Spaces
11. a) input()
12. a) print()
13. d) **
14. c) String
15. b) int()
‐---‐-------‐-------------------------------
1. Which of the following is an example of a Python numeric data type?
a) list
b) tuple
c) string
d) integer
a) string
b) tuple
c) set
d) dictionary
my_list = [1, 2, 3]
print(my_list[1])
a) 1
b) 2
c) 3
d) Error
a) tuple
b) set
c) dictionary
d) list
print(len(my_string))
a) 6
b) 12
c) 13
d) Error
7. Which of the following data types does not allow duplicate values in Python?
a) list
b) set
c) dictionary
d) tuple
8. How do you access the value associated with a specific key in a dictionary?
print(my_dict.get('d'))
a) 1
b) 2
c) 3
d) None
10. Which function is used to convert the data type of a value in Python?
a) type()
b) convert()
c) cast()
d) str()
11. How do you add elements to a set in Python?
12. Which of the following is not a valid way to remove an element from a list
in Python?
my_tuple = (1, 2, 3)
print(my_tuple[1:])
a) (1, 2)
b) (2, 3)
c) (1, 2, 3)
d) Error
14. Which operator is used to merge two dictionaries in Python?
a) +
b) -
c) *
d) |
Answers:
1. d) integer
3. c) set
4. b) 2
5. d) list
6. c) 13
7. b) set
9. d) None
10. c) cast()
11. b) Using the add() method
13. b) (2, 3)
14. d) |
UNIT II
a) for
b) while
c) if
d) switch
x=5
if x > 3:
else:
a) Greater than 3
b) Less than or equal to 3
c) Invalid syntax
d) No output
a) &&
b) ||
c) &
d) |
a) for loop
b) while loop
c) do-while loop
d) switch loop
6. What is the output of the following code snippet?
for i in range(3):
print(i)
a) 0 1 2
b) 1 2 3
c) 3 2 1
d) 2 1 0
a) continue
b) break
c) return
d) pass
for i in range(2):
for j in range(2):
print(i, j)
a) 0 0, 0 1, 1 0, 1 1
b) 0 0, 1 1
c) 0 0, 0 1
d) 1 1
9. Which loop is executed at least once, even if the condition is initially false?
a) for loop
b) while loop
c) do-while loop
d) nested loop
a) skip
b) pass
c) continue
d) break
a) A reserved keyword
a) def my_function():
b) function my_function():
c) define my_function():
d) func my_function():
def greet(name):
greet("Alice")
a) Hello, Alice
b) Hello,
c) Error
d) No output
a) function_name()
b) call function_name()
c) invoke function_name()
d) function_name
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
a) 1
b) 5
c) 10
d) 120
18. How do you define a function with multiple return values in Python?
a) Global scope
Recursive Functions:
def countdown(n):
if n <= 0:
return
print(n)
countdown(n-1)
countdown(5)
a) 5 4 3 2 1
b) 1 2 3 4 5
c) 0
d) Error
a) Linear search
b) Binary search
c) Bubble sort
d) Selection sort
25. Can a Python function return more than one value simultaneously?
a) Yes
b) No
def square_and_cube(x):
result = square_and_cube(2)
print(result)
a) (4, 8)
b) [4, 8]
c) (4)
d) [4]
27. How do you access individual elements from the return value of a function
with multiple return values?
result = calculate(10, 5)
print(result[2])
a) 15
b) 5
c) 50
d) Error
a) Yes
b) No
if y == 0:
else:
return x/y
result = divide(10, 2)
print(result)
a) 5
b) 10/2
d) None
1. c) if
2. a) Greater than 3
4. d) |
5. a) for loop
6. a) 0 1 2
7. b) break
8. a) 0 0, 0 1, 1 0, 1 1
9. c) do-while loop
10. c) continue
14. a) function_name()
17. d) 120
Recursive Functions:
22. a) 5 4 3 2 1
25. a) Yes
26. a) (4, 8)
28. c) 50
29. a) Yes
30. a) 5
UNIT III
2. Built-in Modules:
1. `math`
2. `random`
3. `os`
4. `sys`
3. Creating Modules:
1. `import my_module`
2. `import module.my_module`
4. Locating Modules:
1. `reload`
2. `importlib`
3. `imp`
4. `sys`
8. Packages in Python:
1. `time`
2. `datetime`
3. `calendar`
4. `date`
1. `os`
2. `sys`
3. `math`
4. `datetime`
1. `file.close()`
2. `close(file)`
3. `file.exit()`
4. `exit(file)`
a) Answer: 4. A file containing Python code that can be imported and used
2. Built-in Modules:
a) Answer: 1. `math`
3. Creating Modules:
4. Locating Modules:
a) Answer: 2. `importlib`
8. Packages in Python:
a) Answer: 2. `datetime`
a) Answer: 1. `os`
a) Answer: 1. `file.close()`
1. `re`
2. `os`
3. `sys`
4. `math`
3. Creating Modules:
4. Locating Modules:
1. Local scope
2. Global scope
3. Class scope
4. Built-in scope
1. `reload`
2. `importlib`
3. `imp`
4. `sys`
8. Packages in Python:
1. `datetime`
2. `time`
3. `calendar`
4. `date`
1. `os`
2. `sys`
3. `shutil`
4. `pathlib`
1. `argparse`
2. `sys`
3. `os`
4. `math`
14. Namespaces and Scope:
2. It will be accessible and have the same value as inside the function.
3. It will be accessible but have a different value than inside the function.
1. `os.path.exists()`
2. `os.path.isfile()`
3. `os.path.isdir()`
4. `os.path.getsize()`
2. Built-in Modules:
a) Answer: 1. `re`
3. Creating Modules:
4. Locating Modules:
a) Answer: 3. `imp`
8. Packages in Python:
a) Answer: 1. `datetime`
10. File Handling:
a) Answer: 1. `os`
a) Answer: 1. `argparse`
1. Object-Oriented Programming:
2. Class Definition:
3. Creating Objects:
1. `__str__()`
2. `__init__()`
3. `__repr__()`
4. `__len__()`
a) Which built-in class attribute returns the name of the class as a string?
1. `__name__`
2. `__class__`
3. `__doc__`
4. `__module__`
6. Destructors in Python:
8. Data Hiding:
1. By using private variables and methods with names starting with double
underscores
3. By using public variables and methods with names starting with a single
underscore
9. Inheritance:
2. The process of reusing code and creating a new class from an existing
class
1. The process of creating multiple methods with the same name in a class
3. The process of calling a method from another method in the same class
11. Polymorphism:
1. Code reusability
2. Simplicity of syntax
1. `new`
2. `create`
3. `instantiate`
4. No keyword is required
1. `__init__()`
2. `__str__()`
3. `__repr__()`
4. `__len__()`
16. Built-in Class Attributes:
1. `__init__()`
2. `__del__()`
3. `__str__()`
4. `__repr__()`
18. Encapsulation:
20. Inheritance:
2. The process of reusing code and creating a new class from an existing
class
1. The process of creating multiple methods with the same name in a class
3. The process of calling a method from another method in the same class
1. Code reusability
2. Simplicity of syntax
1. `new`
2. `create`
3. `instantiate`
4. No keyword is required
1. `__init__()`
2. `__str__()`
3. `__repr__()`
4. `__len__()`
1. To initialize an object
29. Encapsulation:
a) Which concept in OOP allows bundling of data and methods into a single
unit?
1. Inheritance
2. Encapsulation
3. Polymorphism
4. Abstraction
30. Polymorphism:
2. Class Definition:
3. Creating Objects:
a) Answer: 1. `__str__()`
a) Answer: 1. `__name__`
6. Destructors in Python:
a) Answer: 2. `__del__()`
7. Encapsulation:
9. Inheritance:
a) Answer: 2. The process of reusing code and creating a new class from an
existing class
11. Polymorphism:
a) Answer: 1. `__init__()`
a) Answer: 2. `__del__()`
18. Encapsulation:
20. Inheritance:
a) Answer: 2. The process of reusing code and creating a new class from an
existing class
a) Answer: 1. `__init__()`
a) Answer: 2. Encapsulation
30. Polymorphism:
UNIT V
a) try
b) catch
c) except
d) handle
a) ValueError
b) TypeError
c) FileNotFoundException
d) IndexError
3. What is the purpose of exception handling in Python?
a) throw
b) catch
c) assert
d) raise
a) check
b) assert
c) verify
d) validate
7. What is the purpose of assertions in Python?
a) findall()
b) match()
c) search()
d) replace()
a) findall()
b) match()
c) search()
d) replace()
a) findall()
b) match()
c) search()
d) replace()
a) i
b) g
c) m
d) s
13. Which character class matches any digit (0-9) in regular expressions?
a) \w
b) \s
c) \d
d) \b
14. Which character class matches any whitespace character in regular
expressions?
a) \w
b) \s
c) \d
d) \b
15. Which character class matches any word character (alphanumeric and
underscore) in regular expressions?
a) \w
b) \s
c) \d
d) \b
17. Which method is used to find all occurrences of a pattern in a string using
regular expressions?
a) findall()
b) match()
c) search()
d) replace()
a) findall()
b) match()
c) search()
d) compile()
19. True or False: Regular expressions can only be used to search for patterns
in text.
a) True
b) False
a) True
b) False
21. True or False: Regular expressions can be used to validate email addresses.
a) True
b) False
22. True or False: Regular expressions can be used to extract information
from HTML tags.
a) True
b) False
23. True or False: Regular expressions can be used to replace specific parts of
a string with another value.
a) True
b) False
24. True or False: Regular expressions can be used to split a string into a list
based on a pattern.
a) True
b) False
25. True or False: Regular expressions can match patterns that occur at the
beginning of a line.
a) True
b) False
26. True or False: Regular expressions can match patterns that occur at the
end of a line.
a) True
b) False
27. True or False: Regular expressions can match patterns that span multiple
lines.
a) True
b) False
28. True or False: Regular expressions can match patterns that contain
multiple characters.
a) True
b) False
29. True or False: Regular expressions can match patterns that include special
characters.
a) True
b) False
30. True or False: Regular expressions can be used to validate phone numbers.
a) True
b) False
1. c) except
2. c) FileNotFoundException
3. b) To handle unexpected situations gracefully
4. d) raise
6. b) assert
8. b) match()
9. c) search()
10. d) replace()
11. a) i
13. c) \d
14. b) \s
15. a) \w
17. a) findall()
18. d) compile()
19. b) False
20. b) False
21. a) True
22. a) True
23. a) True
24. a) True
25. a) True
26. b) False
27. a) True
28. a) True
29. a) True
30. a) True