Question Module Tesst Adv Python
Question Module Tesst Adv Python
1
= 97 self.__cgpa = 8.7 def display(self): print(self.marks) obj = student()
print(obj._student__cgpa) “‘ - **Answer:** The program runs fine and ‘8.7‘
is printed.
13. **How many matches does the command ‘a{1,3}‘ give with the string ‘aab-
baaaa‘?** - **Question:** What is the output of ‘a{1,3}‘ on ‘aabbaaaa‘? -
**Answer:** 4 matches (the groups are ‘aaa‘, ‘aa‘, ‘a‘).
14. **What is the output of the following code?** - **Question:** What
does the following code output? “‘python class Student: def __init__(self):
print(”Sikander”, end=” ”) obj1 = Student() obj2 = obj1 “‘ - **Answer:**
‘Sikander‘.
15. **Is the following Python code correct?** - **Question:** What will be
printed from the following code? “‘python class test: def __init__(self):
self.variable = ’Old’ self.Change(self.variable) def Change(self, var): var = ’New’
obj = test() print(obj.variable) “‘ - **Answer:** ‘Old‘ is printed.
16. **What are the methods which begin and end with two underscore charac-
ters called?** - **Question:** What are these methods called? - **Answer:**
Special methods.
17. **Which of the following statements is true?** - **Question:** Which state-
ment is true about method overriding? - **Answer:** A non-private method in
a superclass can be overridden.
18. **How many instance members are there in the Student class?** - **Ques-
tion:** Given the code: “‘python class Student: regno = 1 name = ”Sikander”
marks = 98 obj1 = Student() “‘ - **Answer:** 0 instance members (only class
variables are defined).
19. **Which of the following is false with respect to Python code?** - **Ques-
tion:** What is false about the following code? “‘python class Student: def
__init__(self, id, age): self.id = id self.age = age std = Student(1, 20) “‘ -
**Answer:** Every class must have a constructor. (It’s false because Python
classes do not require an explicit constructor.)
20. **What is the correct syntax for calling an instance method on a class named
Game?** - **Question:** Which syntax correctly calls an instance method? -
**Answer:** ‘my_game = Game(); my_game.roll_dice()‘.
21. **What will be the output of the following code?** - **Question:** What
does the following code output? “‘python import re pattern = r’\d+’ text =
”There are 2 apples and 10 oranges.” print(re.findall(pattern, text)) “‘ - **An-
swer:** ‘[’2’, ’10’]‘.
22. **How do you create a virtual environment in Python?** - **Question:**
What command is used to create a virtual environment? - **Answer:** ‘python
-m venv env_name‘.
23. **What is the difference between a list and a tuple?** - **Question:** How
2
do lists and tuples differ? - **Answer:** Lists are mutable, while tuples are
immutable.
24. **What is a lambda function in Python?** - **Question:** Define a lambda
function. - **Answer:** A small anonymous function defined with the lambda
keyword.
25. **How do you handle exceptions in Python?** - **Question:** What is the
basic syntax for exception handling? - **Answer:** Using ‘try‘, ‘except‘ blocks.
26. **What does the @staticmethod decorator do?** - **Question:** What is
the purpose of the @staticmethod decorator? - **Answer:** It defines a method
that does not access the class or instance and can be called on the class itself.
27. **How can you read a file in Python?** - **Question:** What is the syntax
to read a file? - **Answer:** Using ‘with open(’filename.txt’, ’r’) as file:‘.
28. **What is the purpose of __init__.py in a package?** - **Question:**
What role does ‘__init__.py‘ play? - **Answer:** It indicates that the direc-
tory should be treated as a package.
29. **What will the following code output?** - **Question:** What does this
code output? “‘python for i in range(5): print(i, end=’ ’) “‘ - **Answer:** ‘0 1
2 3 4‘.
30. **What is a dictionary in Python?** - **Question:** Define a dictionary. -
**Answer:** A collection of key-value pairs, where keys are unique.
31. **How do you merge two dictionaries in Python?** - **Question:** What is
the syntax to merge two dictionaries? - **Answer:** Using ‘{**dict1, **dict2}‘
or ‘dict1.update(dict2)‘.
32. **What is a generator in Python?** - **Question:** How does a generator
differ from a regular function? - **Answer:** A generator uses ‘yield‘ to produce
a sequence of values lazily.
33. **How can you sort a list in Python?** - **Question:** What method is
used to sort a list? - **Answer:** The ‘sort()‘ method or ‘sorted()‘ function.
34. **What does the map() function do?** - **Question:** Describe the pur-
pose of ‘map()‘. - **Answer:** It applies a given function to all items in an
iterable.
35. **How do you create a class in Python?** - **Question:** What is the
syntax for defining a class?
- **Answer:** ‘class ClassName:‘ followed by an indented block.
36. **What is list comprehension?** - **Question:** What is list comprehen-
sion in Python? - **Answer:** List comprehension is a concise way to create
lists by applying an expression to each element in an iterable, typically using
the syntax ‘[expression for item in iterable]‘.
3
37. **What does the filter() function do?** - **Question:** Describe the pur-
pose of the ‘filter()‘ function. - **Answer:** The ‘filter()‘ function constructs
an iterator from elements of an iterable for which a function returns true.
38. **How can you check if a key exists in a dictionary?** - **Question:** What
is the syntax to check if a key exists in a dictionary? - **Answer:** You can
use the ‘in‘ keyword, like ‘if key in dictionary:‘.
39. **What does the self keyword represent in a class?** - **Question:** What
does ‘self‘ refer to in a class definition? - **Answer:** ‘self‘ refers to the instance
of the class itself, allowing access to its attributes and methods.
40. **What is the difference between __str__() and __repr__()?** -
**Question:** How do ‘__str__()‘ and ‘__repr__()‘ differ? - **Answer:**
‘__str__()‘ is meant to return a readable string for users, while ‘__repr__()‘
is intended to return an unambiguous string representation for developers,
typically used for debugging.
41. **What is the purpose of the id() function in Python?** - **Answer:** It
returns the identity of an object, which is unique during the object’s lifetime.
42. **What error is raised when an iterator has no more items to return?** -
**Answer:** ‘StopIteration‘.
43. **Which symbol is used in regular expressions to denote zero or more
occurrences?** - **Answer:** ‘*‘.
44. **How do you create a set in Python?** - **Answer:** Using curly braces
or the ‘set()‘ function, e.g., ‘my_set = {1, 2, 3}‘ or ‘my_set = set([1, 2, 3])‘.
45. **What does the re.search() function do in Python?** - **Answer:** It
searches a string for a match to a regular expression pattern.
46. **What is the keyword used to define a function in Python?** - **Answer:**
‘def‘.
47. **How can you inherit a class in Python?** - **Answer:** By using paren-
theses in the class definition, e.g., ‘class ChildClass(ParentClass):‘.
48. **What will be the output of the following code?** - **Question:** “‘python
print(2 ** 3) “‘ - **Answer:** ‘8‘.
49. **What is a lambda function in Python?** - **Answer:** An anonymous
function defined with the ‘lambda‘ keyword.
50. **How do you convert a string to an integer in Python?** - **Answer:**
Using ‘int()‘, e.g., ‘int(”123”)‘.
51. **What does the len() function do?** - **Answer:** It returns the number
of items in an object, like a list or a string.
52. **What is the output of type([])?** - **Answer:** ‘<class ’list’>‘.
4
53. **How do you define a class in Python?** - **Answer:** Using the ‘class‘
keyword, e.g., ‘class MyClass:‘.
54. **What does the __init__ method do?** - **Answer:** It initializes an
instance of a class.
55. **What is the output of bool(0)?** - **Answer:** ‘False‘.
56. **What is the purpose of self in class methods?** - **Answer:** It refers
to the instance of the class.
57. **How do you handle exceptions in Python?** - **Answer:** Using ‘try‘
and ‘except‘ blocks.
58. **What does the map() function do?** - **Answer:** It applies a function
to all items in an iterable.
59. **How can you create a list in Python?** - **Answer:** Using square
brackets, e.g., ‘my_list = [1, 2, 3]‘.
60. **What is a dictionary in Python?** - **Answer:** A collection of key-value
pairs, defined using curly braces.
61. **How do you access a value in a dictionary?** - **Answer:** Using the
key, e.g., ‘my_dict[’key’]‘.
62. **What does the filter() function do?** - **Answer:** It filters items in an
iterable based on a function that returns True or False.
63. **How can you concatenate two strings in Python?** - **Answer:** Using
the ‘+‘ operator, e.g., ‘”Hello” + ” World”‘.
64. **What is list comprehension?** - **Answer:** A concise way to create
lists using a single line of code, e.g., ‘[x for x in range(10)]‘.
65. **What does break do in a loop?** - **Answer:** It exits the loop immedi-
ately.
66. **What does continue do in a loop?** - **Answer:** It skips the current
iteration and continues with the next iteration.
67. **How do you read a file in Python?** - **Answer:** Using the ‘open()‘
function, e.g., ‘with open(’file.txt’, ’r’) as f:‘.
68. **What is the output of str(10)?** - **Answer:** ‘’10’‘.
69. **How can you sort a list in Python?** - **Answer:** Using the ‘sort()‘
method or ‘sorted()‘ function.
70. **What is a tuple in Python?** - **Answer:** An immutable sequence of
values, defined using parentheses, e.g., ‘(1, 2, 3)‘.
71. **What does the join() method do?** - **Answer:** It concatenates ele-
ments of an iterable into a single string, using a specified separator.
5
72. **How do you create a virtual environment in Python?** - **Answer:**
Using ‘python -m venv myenv‘.
73. **What is the purpose of the return statement?** - **Answer:** It exits a
function and optionally passes back a value.
74. **How can you check if a key exists in a dictionary?** - **Answer:** Using
the ‘in‘ keyword, e.g., ‘if ’key’ in my_dict:‘.
75. **What is the output of list(range(5))?** - **Answer:** ‘[0, 1, 2, 3, 4]‘.
76. **How do you convert a list to a set?** - **Answer:** Using the ‘set()‘
function, e.g., ‘set(my_list)‘.
77. **What does the enumerate() function do?** - **Answer:** It adds a
counter to an iterable and returns it as an enumerate object.
78. **What is the purpose of a docstring in Python?** - **Answer:** To provide
documentation for a function or module.
79. **How do you check the data type of a variable?** - **Answer:** Using the
‘type()‘ function.
80. **What is a list in Python?** - **Answer:** A mutable, ordered collection
of items, defined using square brackets.