JNCIA-DevOps: Juniper Networks Certified Associate – Automation (2021) — Question 5
Which Python code block is an example of a context manager?
Answer options
- A. while True: device "" Device(host-"vmx-1", user-"lab", passwd-"lab123") ...
- B. try: device "" Device(host-"vmx-1", user-"lab", passwd-"lab123") ... except: print("Unable to connect to the vMX1")
- C. with Device(host="vmx-1", user="lab", passwd="lab123") as device: ...
- D. for host in ["vmx-1", "vmx-2"]: device "" Device(host-host, user-"lab", passwd-"lab123") ...
Correct answer: C
Explanation
The correct answer is C, as it correctly uses the 'with' statement, which is a hallmark of context managers in Python. Options A and D do not utilize context management features, while option B employs a try-except block, which is not the same as a context manager.