The following class definition is given. We want the show () method to invoke the get () method, and then output the value the get () method returns. Which of the invocations should be used instead of XXX?

Suggested Answer: B
Get Premium PCAP-31-03 Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
32 comments · newest first
PO
Portia
12 months ago
Are we sure B is the best option? What about A?
KA
Kate
1 year ago
Wait, why would you need 'val' in D? Seems off.
EZ
Ezekiel
1 year ago
C makes no sense, it won't access the instance method.
RE
Ressie
1 year ago
I think A could work too, but B is cleaner.
SH
Shayne
1 year ago
B is definitely the right choice!
DA
Daron
1 year ago
I’m confused about D since it has an extra parameter. I don't think we need to pass anything to get(), so that seems wrong.
EL
Eleonore
1 year ago
I practiced a similar question, and I feel like print(self.get()) is the standard way to call a method in Python, so I'm leaning towards B.
KA
Kattie
1 year ago
I'm not entirely sure, but I remember something about needing to call the method on the instance, so maybe A could work too?
HE
Helaine
1 year ago
I think the answer might be B because it uses self, which is how we usually access instance methods.
RE
Rene
1 year ago
I'm not entirely sure about this one. The wording of the question is a bit tricky, and I'm not 100% confident in my understanding of how instance methods work in this context. I'll need to review my notes on class definitions and method invocation before I can give a definitive answer.
SU
Susana
1 year ago
Okay, I think I've got it. The key here is that the question wants us to use the "show()" method to invoke the "get()" method and output the result. Since "get()" is an instance method, we need to call it on the self object, so the answer is B - print(self.get()).
MA
Mariann
1 year ago
Hmm, I'm a bit confused here. The question mentions invoking the get() method, but it doesn't specify where that method is defined. Is it a method of the class in the image, or is it a separate function? I'll need to think this through carefully before answering.
MY
Myra
1 year ago
This looks like a pretty straightforward question. I think the answer is B - print(self.get()). The question states that we want the show() method to invoke the get() method, and then output the value it returns. Since get() is an instance method, we need to call it on the self object.
SA
Sage
2 years ago
I'm not sure, but I think C is also a possible answer.
JU
Judy
2 years ago
Hmm, this is a tricky one. But I think B) print(self.get()) is the right answer. Gotta love those self-referential method calls!
PE
Pearlie
2 years ago
I would choose C) print(get()) for this one.
EI
Eileen
2 years ago
I'm not sure, but I would go with B) print(self.get()).
SI
Simona
2 years ago
I think A) print(get(self)) might work too.
PO
Portia
2 years ago
I agree, B) print(self.get()) is the correct choice.
JE
Jesusa
2 years ago
B) print(self.get()) is the way to go. I mean, come on, it's basic object-oriented programming 101.
ER
Erin
2 years ago
Agreed, using self to access the method within the class is the right approach.
LI
Lindsey
2 years ago
Yeah, that makes sense. It's the standard way to call a method on an object.
CA
Catina
2 years ago
I think B) print(self.get()) is the correct invocation.
DE
Devorah
2 years ago
Haha, I'm going to go with C) print(get()). Just kidding, that's obviously wrong. It's gotta be B) print(self.get()).
BR
Brett
2 years ago
Definitely B) print(self.get()). The 'self' keyword is used to refer to the instance of the class, so that's the way to access the get() method.
JI
Jillian
2 years ago
I agree with Sheridan, because self.get() is the correct way to invoke the get() method.
MA
Margot
2 years ago
I think the correct answer is B) print(self.get()). That's the standard way to call a method on an object in Python.
KE
Kerry
2 years ago
I see, thanks for clarifying. B) print(self.get()) it is then.
MA
Maia
2 years ago
No, A) print(get(self)) would not work because get() is a method of the object, so we need to use self.get().
RO
Roselle
2 years ago
But what about A) print(get(self))? Wouldn't that work as well?
RO
Roselle
2 years ago
I agree, B) print(self.get()) is the correct way to call a method on an object.
SH
Sheridan
2 years ago
I think the answer is B.