-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_OOP_Assignment.txt
More file actions
117 lines (83 loc) · 5.2 KB
/
Copy pathPython_OOP_Assignment.txt
File metadata and controls
117 lines (83 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
## Python OOP Assignment
Q1. What is the purpose of Python's OOP?
A) Python's OOP enables the programs to be modularized into classes and objects with the concepts of Inheritance, Polymorphism, Encapsulation & Abstraction.
Q2. Where does an inheritance search look for an attribute?
A) Inheritance first looks in it's class later in it's superclasses level by level.
Q3. How do you distinguish between a class object and an instance object?
A) Class is blueprint of object and instance is a virtual copy of the object
Q4. What makes the first argument in a class’s method function special?
A) It refers to the object itself.
Q5. What is the purpose of the __init__ method?
A) It is used to initialize the class
Q6. What is the process for creating a class instance?
A) To create instance of a class you call the class using class name and pass whatever the arguments it's __init__ method accepts.
Q7. What is the process for creating a class?
A) To create a class "class" keyword is used followed by the class name.
Q8. How would you define the superclasses of a class?
A) By mentonined the superclasses names next to child class name.
Q9. What is the relationship between classes and modules?
A) Module is a way to break down python code into managable parts it a module may contain several classes and function but a Class is a blueprint of a realword entity which encapsulates data and functions.
Q10. How do you make instances and classes?
A) o create instance of a class you call the class using class name and pass whatever the arguments it's __init__ method accepts whereas, To create a class "class" keyword is used followed by the class name.
Q11. Where and how should be class attributes created?
A) Class attributes are declared directly in the class these are shared across all objects
Q12. Where and how are instance attributes created?
A) Instance attributes are created in the __init__ method these are specific to object
Q13. What does the term "self" in a Python class mean?
A) Self refers to the current object
Q14. How does a Python class handle operator overloading?
A) Special function are invoked based on the objects on which the operation is performed
Q15. When do you consider allowing operator overloading of your classes?
A) When the objects on which the operation to be performed are different.
Q16. What is the most popular form of operator overloading?
A) + is the most popular form of operator overloading
Q17. What are the two most important concepts to grasp in order to comprehend Python OOP code?
A) Classes and Objects
Q18. Describe three applications for exception processing.
A) a. Exceptions cannot be ignored, they must be caught otherwise the program will terminate
b. It provides a means to sperate error handling code from program code
c. Continue processing as if no error has occured.
Q19. What happens if you don't do something extra to treat an exception?
A) Exceptions cannot be ignored, they must be caught otherwise the program will terminate
Q20. What are your options for recovering from an exception in your script?
A) Using except clause and else clauses
Q21. Describe two methods for triggering exceptions in your script.
A) try-except are used.
Q22. Identify two methods for specifying actions to be executed at termination time, regardless of
whether or not an exception exists.
A) Else and Finally
Q23. What is the purpose of the try statement?
A) Try is where the exception possible statements are placed
Q24. What are the two most popular try statement variations?
A) try and except or try,except and else
Q25. What is the purpose of the raise statement?
A) Is used to raise exceptions or errors
Q26. What does the assert statement do, and what other statement is it like?
A) Assert keyword is used during debugging it raises an error if code condition returns False
Q27. What is the purpose of the with/as argument, and what other statement is it like?
A) with is used to open file and close handling file.close automatically. Other statement like file.close can be used other wise as is for alias name
Q28. What are *args, **kwargs?
A) *args for a variable number of arguments and **kwargs for *kwargs for a variable number of keyword arguments
Q29. How can I pass optional or keyword parameters from one function to another?
A) Using *args and **kwargs
Q30. What are Lambda Functions?
A) lambda function is a nameless function
Q31. Explain Inheritance in Python with an example?
A) Inheritance is inherting the properties and methods from parent to child(s).
Eg:
class A():
def __init__(self):
self.type="Some"
def age(self):
....
class B(A):
def __init__(self):
self.name="S"
Q32. Suppose class C inherits from classes A and B as class C(A,B).Classes A and B both have their own versions of method func(). If we call func() from an object of class C, which version gets invoked?
A) Class C's func method itself
Q33. Which methods/functions do we use to determine the type of instance and inheritance?
A) isinstance() is used
Q34.Explain the use of the 'nonlocal' keyword in Python.
A) The nonlocal keyword is used in nested functions to reference a variable in the parent function.
Q35. What is the global keyword?
A) Used for creating variable with gloabl scope