PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 132


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

Show Answer
Correct Answer: AC

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

14 comments
Sign in to comment
lkn2993Options: AC
Feb 27, 2023

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)

rbishunOptions: AC
May 22, 2022

- 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

andr3Options: CD
Mar 21, 2023

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
Mar 30, 2023

I think CD is the most suitable answers.

MallieOptions: BC
Jan 12, 2023

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

MoverOption: C
Jan 15, 2023

c. the constructor cannot return a result other than None

ivanbicalhoOptions: AC
Mar 2, 2023

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.

rotimislawOptions: CD
Nov 14, 2022

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

9prayerOptions: BC
Feb 5, 2023

B & C are TRUE

Rizos
Mar 1, 2023

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

ivanbicalho
Mar 2, 2023

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

Mickey321Options: CD
Sep 2, 2023

CD is the most suitable answer

SarpppOptions: BC
Dec 19, 2023

B and C

Damon54
Feb 9, 2024

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

kstrOptions: AD
Feb 15, 2024

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
Feb 18, 2024

Have you taken the exam yet

CoinUmbrellaOptions: CD
Mar 24, 2024

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