300-435 Question 21
Single answerYou are tasked with automating a network inventory system. Below is a Python script snippet that processes device information. What will be the output of this script?
class Device:
def __init__(self, hostname, ip, status):
self.hostname = hostname
self.ip = ip
self.status = status
def get_active_devices(devices):
active_devices = []
for device in devices:
if device.status == 'active':
active_devices.append(device.hostname)
return active_devices
devices = [
Device('Router1', '192.168.1.1', 'active'),
Device('Switch1', '192.168.1.2', 'inactive'),
Device('Firewall1', '192.168.1.3', 'active')
]
print(get_active_devices(devices))
- A
['Router1', 'Switch1', 'Firewall1']
- B
['Router1', 'Firewall1']
- C
['192.168.1.1', '192.168.1.3']
- D
['Switch1']
Show answer and explanation
Correct answer: B
Explanation
The Python script defines a class Device and a function get_active_devices that filters devices with 'active' status. The for loop iterates through the devices list, and the condition if device.status == 'active' ensures that only devices with 'active' status are added to the active_devices list. Based on the input, only 'Router1' and 'Firewall1' meet this condition, so the output is ['Router1', 'Firewall1'].
- A. Incorrect.
This is incorrect because not all devices are 'active'. Only 'Router1' and 'Firewall1' meet the condition of having a status of 'active'.
- B. Correct.
This is correct because the script filters devices based on their 'status' attribute and only appends the 'hostname' of devices with 'active' status to the list.
- C. Incorrect.
This is incorrect because the script appends the 'hostname', not the 'IP', of devices with 'active' status to the output list.
- D. Incorrect.
This is incorrect because 'Switch1' has a status of 'inactive', so it will not be included in the output list.