exam questions

Exam PCAP-31-03 All Questions

View all questions & answers for the PCAP-31-03 exam

Exam PCAP-31-03 topic 1 question 54 discussion

Actual exam question from Python Institute's PCAP-31-03
Question #: 54
Topic #: 1
[All PCAP-31-03 Questions]

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

  • A. the constructor's first parameter must always be named self
  • B. there can be only one constructor in a Python class
  • C. the constructor cannot be invoked directly under any circumstances
  • D. the constructor cannot return a result other than None
Show Suggested Answer Hide Answer
Suggested Answer: AB 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Horsefeathers
6 days, 9 hours ago
Selected Answer: BD
Correct answers B & D. A. is incorrect - the first parameter of __init__ is conventionally named self, but it can be named anything (though it's strongly recommended to use self for readability) C. is incorrect - the constructor (__init__) can be invoked directly, though it's not common; for example, MyClass.__init__(obj, args...) is technically valid but not recommended
upvoted 1 times
...
kino_1994
5 months, 2 weeks ago
I correct my previous answer: option C is also correct.
upvoted 1 times
...
kino_1994
5 months, 2 weeks ago
B and D are correct answers. A) False class MyClass: def __init__(this, value): # Using 'this' instead of 'self' this.value = value obj = MyClass(10) print(obj.value) # Output: 10 B) True class MyClass: def __init__(self): pass def __init__(self, value): self.value = value obj1 = MyClass() # Error obj2 = MyClass(20) print(obj2.value) # Output: 20 C) False class MyClass: def __init__(self, value): self.value = value obj = MyClass.__init__(MyClass, 10) # Error obj = MyClass.__init__(10) # Error D) True class MyClass: def __init__(self, value): self.value = value return "This will cause an error" obj = MyClass(10) # This will raise a TypeError: __init__() should return None, not 'str'
upvoted 2 times
...
kstr
6 months, 3 weeks ago
BD the correct answer
upvoted 2 times
...
DKAT2023
6 months, 3 weeks ago
Selected Answer: AB
A and B are corrects
upvoted 2 times
...
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.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago