300-635 · Question #16
Refer to the exhibit. Assuming a new ACI instance, what is the result when this script is run? ``python def add_tenant(): token = apic_login.aaaLogin() for tenant in range(1,10): try: response =…
The correct answer is B. Nine objects are created. Option B is correct because Python's range(1, 10) is exclusive of the stop value, generating the integers 1 through 9 - exactly nine iterations. Each iteration POSTs a fvTenant object with "status": "created" to the APIC, resulting in nine new tenant objects (tn-exam1 through…
Question
def add_tenant():
token = apic_login.aaaLogin()
for tenant in range(1,10):
try:
response = requests.post(
url=constant.APIC_URL + '/api/node/mo/uni-tn%s.json' % (tenant),
headers={
"Cookie": "APIC-cookie=" + token,
"Content-Type": "application/json; charset=utf-8"
},
data=json.dumps({
"fvTenant": {
"attributes": {
"status": "created",
"dn": "uni/tn-exam%s" % (tenant),
"name": "tn-exam%s" % (tenant),
"rn": "tn-exam%s" % (tenant)
},
"children": [
]
}
})
)
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
except requests.exceptions.RequestException:
print('HTTP Request failed')
add_tenant()
Options
- ATen objects are created and subsequently deleted.
- BNine objects are created.
- CAn exception is thrown.
- DTen objects are created.
How the community answered
(33 responses)- A6% (2)
- B85% (28)
- D9% (3)
Explanation
Option B is correct because Python's range(1, 10) is exclusive of the stop value, generating the integers 1 through 9 - exactly nine iterations. Each iteration POSTs a fvTenant object with "status": "created" to the APIC, resulting in nine new tenant objects (tn-exam1 through tn-exam9).
Why the distractors are wrong:
- A is wrong because the
statusattribute is"created", not"deleted"or"modified"- no deletion occurs at any point. - C is wrong because on a fresh ACI instance the tenants don't already exist, so the POST requests should succeed without triggering the
RequestExceptionhandler. - D is wrong for the same core reason B is correct:
range(1, 10)stops at 9, never reaching 10. To create ten objects you'd needrange(1, 11).
Memory tip: When you see range(start, stop) in Python, remember the stop value is a fence post that's never touched - the range goes up to but never including stop. A quick mental check: range(1, 10) has 10 - 1 = 9 elements. On ACI exams, always count your loop bounds before counting your API calls.
Topics
Community Discussion
No community discussion yet for this question.