300-435 Question 20
Single answerYou are working on automating a network monitoring script using Python. The following script is provided:
class Device:
def __init__(self, name, ip, status):
self.name = name
self.ip = ip
self.status = status
def check_devices(devices):
active_devices = []
for device in devices:
if device.status == 'up':
active_devices.append(device.name)
return active_devices
devices = [
Device('Switch1', '192.168.1.1', 'up'),
Device('Router1', '192.168.1.2', 'down'),
Device('AP1', '192.168.1.3', 'up')
]
result = check_devices(devices)
print(result)
What will be the output of the script?
- A
['Switch1', 'Router1', 'AP1']
- B
['Switch1', 'AP1']
- C
['Router1']
- D
[]
Show answer and explanation
Correct answer: B
Explanation
The script defines a Device class and uses the check_devices function to iterate through a list of devices. It checks the status of each device, and if the status is 'up', it appends the device name to the active_devices list. The final result contains only the names of devices with a status of 'up', which in this case are 'Switch1' and 'AP1'.
- A. Incorrect.
Incorrect. This would include all devices regardless of their status. The script filters devices based on their status being 'up'.
- B. Correct.
Correct. The script checks the status of each device and only appends devices with a status of 'up' to the active_devices list. 'Switch1' and 'AP1' meet this condition.
- C. Incorrect.
Incorrect. 'Router1' has a status of 'down' and is not included in the active_devices list.
- D. Incorrect.
Incorrect. The script does return results, specifically devices with a status of 'up'. An empty result would only occur if no devices had a status of 'up'.