A & C are correct.
Technically speaking you can try to assign more than one constructor. However, other constructors will be removed at runtime and you will always end up with the last one. Python does not support overloading.
You CAN invoke the constructor directly. Nothing stops you in python from calling any available functions/methods directly:
class C:
Var = data = 1
def __init__(self):
pass
def __init__(self, carat):
pass
c = object()
C.__init__(c, 2)
AD. Explanation:
A. In Python, a class can have only one constructor method, which is typically denoted by __init__() method.
D. The first parameter of the constructor method in Python is conventionally named self, which refers to the instance of the class.
The other options are incorrect:
B. The constructor can be invoked directly using the class name (ClassName()) to create an instance of the class.
C. The constructor in Python can return values other than None if explicitly specified, although it's not common practice. However, the conventional use of constructors is to initialize instance variables and no explicit return statement is necessary.
A. False. In Python, there can be multiple constructors using the method overloading technique with default arguments, although only one is typically used.
C is true because the constructor in Python is a special method named __init__() that is called when an object is created
D is true because the first parameter of a constructor in Python must always be named self.
It should be AC. Like the user lkn2993 said, nothing stops you in python from calling any available functions/methods directly. And the constructor's first parameter don't necessarily always be named self, can be named anything you want.
B & C are TRUE
A: FALSE - the second ctor overrides the first and does not cause an error
B: TRUE - but you can call a super class
C: TRUE - can only return 'None'
D: FALSE - first parameter is only called 'self' by convention - it is not mandatory
C: Constructors cannot return anything else than None. Otherwise it raises TypeError
D: Constructors must include at least one argument named 'self' as the first argument
- Only 1 ctor allowed (if more than 1, the last 1 overrides all)
- You can invoke the base’s ctor (from within a class)
class Car(Vehicle):
def __init__(self, make):
self.make = make
super().__init__('car')
Vehicle.__init__(self, "and another way to invoke the base’s ctor)
- ctors can only return None, (and they do by default)
- ctors must include the self parameter
upvoted 4 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
lkn2993
Highly Voted 1 year, 9 months agoCoinUmbrella
Most Recent 8 months, 1 week agokstr
9 months, 2 weeks agomoteruky
9 months, 2 weeks agoDamon54
9 months, 3 weeks agoSarppp
11 months, 2 weeks agoMickey321
1 year, 2 months agoandr3
1 year, 8 months agotanhuynh10
1 year, 8 months agoivanbicalho
1 year, 9 months agoRizos
1 year, 9 months agoivanbicalho
1 year, 9 months ago9prayer
1 year, 9 months agoMover
1 year, 10 months agoMallie
1 year, 10 months agorotimislaw
2 years agorbishun
2 years, 6 months ago