nerdexam
CiscoCisco

200-901 · Question #401

200-901 Question #401: Real Exam Question with Answer & Explanation

The user needs to correctly complete a Python add_member method within a Team class using provided code snippets to manage a list of members and return appropriate boolean values, particularly for a 'happy path' scenario in a Pytest context.

Understanding and Using APIs

Question

Drag and Drop Question Drag and drop the code snippets from the bottom onto the blanks in the code to complete the happy path scenario. Not all options are used. Answer:

Explanation

The user needs to correctly complete a Python add_member method within a Team class using provided code snippets to manage a list of members and return appropriate boolean values, particularly for a 'happy path' scenario in a Pytest context.

Approach. The add_member method intends to add a member to the self.members list if the member is not already present, and return True for success. If the member is already present, it should not add the member and return False.

  1. For the first blank (inside the if member not in self.members: block): This is the 'happy path' where a new member needs to be added. The correct Python idiom to add an item to a list (which self.members is) is list.append(item). Therefore, the snippet self.members.append(member) should be dragged to the first blank. This action ensures the member is added to the team's internal list.
  2. For the second blank (inside the else: block): This block is executed if member is already in self.members. The method should indicate that the member was not added (because it already exists) by returning False. The original code structure has return False following the blank. In drag-and-drop questions where a specific statement is intended for a blank, even if a return is implicitly there, dragging the explicit return False snippet to the blank is the correct interaction, effectively making the blank the return statement for that branch. This completes the logic for the case where the member already exists, ensuring the method correctly returns False.

Common mistakes.

  • common_mistake. 1. Dragging team.add_member(member) to the first blank: This is a common mistake related to misunderstanding the self keyword in Python OOP. Inside an instance method like add_member, self refers to the current instance of the Team class. team is a variable name used in the Pytest fixture, not a general reference to the current object within the method's scope. Making this call would either result in a NameError or, if team somehow existed in a closure (which it doesn't here), it would call a method on a different Team object. The correct way to modify the current object's members list is self.members.append(member).
  1. Dragging yield member: yield is used in generator functions or Pytest fixtures to provide values iteratively or set up/tear down resources, not for adding elements to a list in a regular method.
  2. Dragging member.add_member(): The member object (an instance of m.Member) does not have an add_member method defined in the provided code, and this action would not contribute to adding the member to the Team's list of members.
  3. Dragging return True or return False to the wrong blank: While return True is part of the happy path and return False is part of the unhappy path, they must be placed in the correct logical branch. Using return True in the else block, or return False in the if block after successfully adding a member, would completely break the method's intended logic.

Concept tested. Python object-oriented programming (OOP) principles, instance methods and the self keyword, list manipulation (appending and checking membership), conditional logic (if-else statements), and method return values.

Topics

#API Interaction#Python#Code Completion#REST API

Community Discussion

No community discussion yet for this question.

Full 200-901 PracticeBrowse All 200-901 Questions