Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Question Module Tesst Adv Python

Uploaded by

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

Question Module Tesst Adv Python

Uploaded by

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

1. **Which of the following gives an error?

** - **Question:** What will


‘re.search(’ab’, ’aaca’).group()‘ return? - **Answer:** This will give an error
because ’ab’ is not found in ’aaca’.
2. **According to PEP 8 coding style guidelines, how should constant values
be named in Python?** - **Question:** How should constant values be named
in Python? - **Answer:** In all caps with underscores separating words (e.g.,
‘MAX_VALUE = 255‘).
3. **Which of the following code uses the inheritance feature of Python?**
- **Question:** Which code demonstrates inheritance? - **Answer:** ‘class
Hoo(Foo): pass‘ (where Hoo inherits from Foo).
4. **What type of constructor is defined in the following code?** - **Ques-
tion:** The code ‘class Fruit: def __init__(self, taste): self.taste = taste;
print(Fruit(”Sweet”).taste)‘ defines what? - **Answer:** A parameterized con-
structor.
5. **What will the following code output?** - **Question:** What does
‘print(re.findall(”w”, sequence))‘ output given ‘sequence = ”6yrs@hkbk-
16yrs@cranes”‘? - **Answer:** This code will give an empty list ‘[]‘ since ”w”
is not found.
6. **Which statement about static methods is true?** - **Question:** What
is true about static methods? - **Answer:** Static methods serve mostly as
utility or helper methods, since they cannot access or modify a class’s state.
7. **Fill in the blank to complete the code.** - **Question:** Complete the
code: ‘fun = lambda _____: x + 2; print(fun(2)); print(fun(-5))‘ - **An-
swer:** ‘x:‘.
8. **Which methods are used to create custom iterators?** - **Question:**
What are the methods used to create custom iterators? - **Answer:**
‘__iter__()‘ and ‘__next__()‘.
9. **What will be the output of the following Python code?** - **Question:**
Given the code: “‘python class demo(): def __repr__(self): return ’__repr__
built-in function called’ def __str__(self): return ’__str__ built-in function
called’ s = demo() print(s) “‘ - **Answer:** ‘__str__ built-in function called‘.
10. **What is the output of the following code using OrderedDict?** - **Ques-
tion:** What is the output of the following code? “‘python from collections
import OrderedDict oDict = OrderedDict() oDict[2] = 4 oDict[3] = 9 oDict[1]
= 1 print(oDict.popitem()) “‘ - **Answer:** ‘(1, 1)‘.
11. **What will be the output of the following Python code?** - **Question:**
What does ‘re.split(r’\s+’, ’Chrome is better than explorer’, maxsplit=3)‘ re-
turn? - **Answer:** ‘[’Chrome’, ’is’, ’better’, ’than explorer’]‘.
12. **What will be the output of the following Python code?** - **Ques-
tion:** Given the code: “‘python class student: def __init__(self): self.marks

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.

You might also like