300-435 Question 24
Single answerYou are working with a Python script to automate network configurations on a Cisco device. The script contains the following code snippet:
class Device:
def __init__(self, hostname, ip_address):
self.hostname = hostname
self.ip_address = ip_address
def is_reachable(self):
if self.ip_address.startswith('192.'):
return True
return False
def display_info(self):
print(f"Device {self.hostname} has IP address {self.ip_address}")
# Create a list of devices
devices = [
Device('Router1', '192.168.1.1'),
Device('Switch1', '10.1.1.1'),
Device('Firewall1', '192.168.2.1')
]
for device in devices:
if device.is_reachable():
device.display_info()
What will be the output of the above script?
- A
Device Router1 has IP address 192.168.1.1 Device Firewall1 has IP address 192.168.2.1
- B
Device Router1 has IP address 192.168.1.1 Device Switch1 has IP address 10.1.1.1 Device Firewall1 has IP address 192.168.2.1
- C
Device Switch1 has IP address 10.1.1.1
- D
The script will throw an error because the 'is_reachable' method is not implemented correctly.
Show answer and explanation
Correct answer: A
Explanation
The script iterates over a list of Device objects and calls the is_reachable method to check if the IP address starts with '192.'. For devices that meet this condition, their information is displayed using the display_info method. Router1 and Firewall1 satisfy this condition, so their details are printed.
- A. Correct.
Correct - The
is_reachablemethod checks if the IP address starts with '192.', so only Router1 and Firewall1 meet this condition. Their information is printed using thedisplay_infomethod. - B. Incorrect.
Incorrect - Switch1's IP address does not start with '192.', so it is not reachable, and its information will not be displayed.
- C. Incorrect.
Incorrect - Only Switch1's information is mentioned in this option, but the script includes multiple devices. Switch1 is also not reachable because its IP address does not start with '192.'.
- D. Incorrect.
Incorrect - The script will not throw any errors. The
is_reachablemethod is correctly implemented and checks the condition usingstartswith.