Courseiva
Python And PyEZeasyMultiple ChoiceObjective-mapped

JNCIA-DevOps Python And PyEZ Practice Question

An automation engineer is writing a Python script using Juniper PyEZ to connect to a Junos device. Which context manager approach is best practice to ensure the Device session is properly closed even if an exception occurs?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

with Device(host="192.168.1.1", user="admin", passwd="secret") as dev:

Using the 'with Device(host=...) as dev:' context manager guarantees that the open() and close() methods are handled automatically, even if runtime errors occur inside the block.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • dev = Device(host="192.168.1.1"); dev.start_session()

    Why it's wrong here

    start_session is not a standard PyEZ Device method.

  • with Device(host="192.168.1.1", user="admin", passwd="secret") as dev:

    Why this is correct

    The context manager automatically opens the connection upon entry and safely closes it upon exit.

  • dev = Device(host="192.168.1.1"); dev.open(autoclose=True)

    Why it's wrong here

    autoclose is not a valid keyword argument for the open() method in PyEZ.

  • dev = Device(host="192.168.1.1").connect()

    Why it's wrong here

    The connect() method does not exist on the Device class; open() is used instead.

About these practice questions

One of 307 original JNCIA-DevOps practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed August 2026 · checked against the official Juniper Networks exam blueprint

This JNCIA-DevOps practice question is part of Courseiva's free Juniper Networks certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the JNCIA-DevOps exam.