Exam PCAP All QuestionsBrowse all questions from this exam
Question 132

What is true about Python class constructors? (Choose two.)

    Correct Answer: A, C

    A Python class can only have one constructor, typically defined using the __init__() method. This is because if multiple constructors are defined, the last one will override the previous ones, effectively keeping only one. Additionally, the constructor cannot return a result other than None. By default, the __init__() method initializes the object and does not return a value. Any attempt to return a value will result in a TypeError.

Discussion
lkn2993Options: AC

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)

andr3Options: CD

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.

tanhuynh10

I think CD is the most suitable answers.

rbishunOptions: AC

- 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

MallieOptions: BC

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

ivanbicalhoOptions: AC

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.

MoverOption: C

c. the constructor cannot return a result other than None

CoinUmbrellaOptions: CD

C.D. https://www.shiksha.com/online-courses/articles/constructors-in-python-definition-types-and-rules/#Rules-of-Python-Constructor

kstrOptions: AD

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.

moteruky

Have you taken the exam yet

Damon54

A. False. In Python, there can be multiple constructors using the method overloading technique with default arguments, although only one is typically used.

SarpppOptions: BC

B and C

Mickey321Options: CD

CD is the most suitable answer

Rizos

Anyone know what the true answer is? Everyone has their own answer..

ivanbicalho

haha, it's funny, everyone has a different opinion here. But it's AC in my opinion.

9prayerOptions: BC

B & C are TRUE

rotimislawOptions: CD

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