Docsity

WGU D522 Python for IT Automation WEN1 Task 1 & Task 2 Exam Practice Complete Python Code,, Exams of Nursing

Partial preview of the text

Download WGU D522 Python for IT Automation WEN1 Task 1 & Task 2 Exam Practice Complete Python Code, and more Exams Nursing in PDF only on Docsity!

WGU D522 Python for IT Automation

WEN1 Task 1 & Task 2 Exam Practice

Complete Python Code, Questions & Answers with Rationales

DNS Remediation & Network Monitoring

2026 / 2027 Updated

Task 1 DNS Remediation

Task 2 Network Monitoring

Python Automation Code

MUST-KNOW TABLE • WHAT IT DOES • #1 CHECK • #1 TRAP

Concept What It Does #1 Check #1 Trap

socket.gethostbyname() Resolves hostname to IP Wrap in try/except gaierror Crashes if DNS fails

subprocess.run() Runs nslookup / ping / ipconfig capture_output=True, text=True

os.system returns only exit code

dnspython Resolver Queries A / MX / TXT records Set lifetime = 2-5 sec No timeout = hang

ipaddress.ip_address() Validates IPv4 / IPv6 Try block + ValueError Manual regex misses edge cases

json.load / dump Parses / writes JSON logs Use with open() load vs dump swapped

time.sleep(60) Pauses monitoring loop Import time first time.wait does not exist

os.getenv() Reads env var with default Provide default value os.environ[] raises KeyError

logging Levels + timestamps + file Use INFO / ERROR levels print() has no persistence

SAMPLE Q1 HIGHLIGHTED — A script must read a hostname and print it in uppercase only if it contains a

dot. Answer: A — if '.' in hostname: print(hostname.upper()) — 'in' tests substring membership; contains()

is Java, not Python.

DNS FLOW: Input → Validate → Resolve → Ping → Log → Remediate → Re-check → Report

LABS • VALUES • PORTS • MNEMONICS

Value / Port Meaning Normal / Expected Mnemonic

Port 53 DNS UDP/TCP 53 open 53 = DNS

Port 80 HTTP 200 OK 80 = Web

Port 443 HTTPS 200 OK, TLS 443 = Secure Web

Port 22 SSH Connection OK 22 = Secure Shell

Exit 0 Ping / subprocess success Success 0 = OK

Exit 1 / 2 Ping / command failure Investigate Non-zero = Fail

HTTP 200 Success Proceed 200 = Good

HTTP 404 / 500 Not Found / Server Error Log + retry 4xx client, 5xx server

Latency <100ms Healthy LAN Log + continue High = investigate

DNS Lifetime 2-5s Query timeout Do not hang Timeout saves loop

MNEMONICS: TRY-EXCEPT-FINALLY = Try risky code, Except handle, Finally always clean up. CRUD =

Create Read Update Delete for files. AAA = Arrange Act Assert for tests.

SAMPLE Q3 HIGHLIGHTED — Which expression is True when ping result contains 'Reply' regardless of

case? Answer: A — 'reply' in result.lower() — lower() normalizes case, 'in' finds substring anywhere.

PRIORITY FRAMEWORK • ASSESS FIRST • HOLD? • CALL? • TEACH?

Priority Assess First Hold? Next Action

1. Safety / Outage Is all network down? Hold deploy if outage Escalate + log 2. Scope One host or all hosts? Hold remediation if unclear Isolate scope 3. DNS vs Network Ping IP vs hostname? Hold if DNS only Flush / change DNS 4. Verify Fix Re-run lookup + ping Do not close ticket yet Re-check after fix 5. Document Log time, host, fix Do not skip log Write remediation log 6. Teach / Prevent What caused it? — Add monitoring + alert

SAMPLE Q4 HIGHLIGHTED — Function must return both IP and latency. Answer: A — return ip, latency —

Python returns a tuple, unpack as ip, latency = func(). Code after return never runs.

DECISION TREE: Can you ping IP? → Yes = DNS problem → No = Network problem → Can you ping gateway? → No = Local network → Yes = Upstream / DNS server

  1. A script must read a hostname from user input and print it in uppercase

only if it contains a dot. Which code correctly implements the conditional

check?

A) if '.' in hostname: print(hostname.upper())

B) if hostname.contains('.'): print(hostname.upper())

C) if hostname has '.': print(hostname.upper())

D) if hostname = '.': print(hostname.upper())

Answer: A

Rationale:

Correct is A because the 'in' operator tests substring membership

in Python and upper() returns the uppercase string. B is wrong

because Python strings have no contains() method; that is Java. C

is wrong because 'has' is not Python syntax for membership

testing. D is wrong because single '=' is assignment, not

comparison, and it would compare the whole string to a single dot.

  1. A technician needs to store 5 DNS server IPs in order and allow

duplicates for testing. Which structure is most appropriate?

A) A tuple of 5 strings

B) A set of 5 strings

C) A list of 5 strings

D) A dictionary with 5 keys

Answer: C

Rationale:

Correct is C because a list preserves order and allows duplicate

values, which fits storing IPs in order for testing. A is wrong

because a tuple is immutable, so you cannot append or change

test IPs easily. B is wrong because a set removes duplicates and

does not guarantee order. D is wrong because a dictionary

requires unique keys and key-value pairs, adding unnecessary

structure for a simple ordered collection.

  1. In Task 1, a function must return both the resolved IP and the latency.

How should it return two values?

A) return ip, latency

B) return ip + latency

C) return [ip] and [latency]

D) return ip; latency

Answer: A

Rationale:

Correct is A because Python functions can return a tuple of

multiple values separated by commas, unpacked as ip, latency =

func(). B is wrong because + would try to add incompatible types

and collapse them into one value. C is wrong because 'and'

returns only one operand based on truthiness, not both values. D

is wrong because code after return never executes, so latency

would never be returned.

  1. What is the output of print(10 % 3, 10 // 3)?

A) 1 3

B) 1 3.

C) 0 3

D) 3 1

Answer: A

Rationale:

Correct is A because % gives the remainder 1 and // gives floor

division 3. B is wrong because // never returns a float with decimal

in this case. C is wrong because 10 % 3 is not 0; 3 divides 10 with

remainder 1. D is wrong because it swaps the two operators'

results.

  1. A CSV file has header 'hostname,ip,status'. Which code correctly opens

it for reading and automatically closes it?

A) with open('hosts.csv') as f: data = f.read()

B) f = open('hosts.csv'); data = f.read()

C) open('hosts.csv').read() without close

D) with open('hosts.csv','w') as f: data = f.read()

Answer: A

Rationale:

Correct is A because the with statement guarantees the file is

closed when the block exits. B is wrong because it leaves the file

open until manually closed, risking leaks. C is wrong because it

also leaves the handle unclosed and relies on garbage collection.

D is wrong because 'w' truncates the file and is for writing,

destroying data before reading.

  1. Which method correctly removes whitespace from both ends of a

user-entered hostname?

A) hostname.strip()

B) hostname.trim()

C) hostname.cut()

D) hostname.remove()

Answer: A

Rationale:

Correct is A because strip() removes leading and trailing

whitespace in Python. B is wrong because trim() is

Java/JavaScript, not Python. C is wrong because cut() does not

exist for strings. D is wrong because remove() is a list method, not

a string whitespace method.

  1. Which code correctly checks if port 53 is open using socket with a

2-second timeout?

A) s = socket.socket(); s.settimeout(2); s.connect_ex((host,53)) == 0

B) socket.connect(host,53) == True

C) socket.open(host,53, timeout=2)

D) ping(host,53) == 0

Answer: A

Rationale:

Correct is A because connect_ex() returns 0 on success,

settimeout(2) limits waiting, and socket creation is required. B is

wrong because socket has no direct connect() function that

returns True in that form. C is wrong because socket.open is not a

valid API. D is wrong because ping uses ICMP, not TCP port

checking, and does not test port 53.

  1. What does the 'with' statement do when writing a remediation log file?

A) Ensures file is flushed and closed even if an error occurs

B) Deletes the file after writing

C) Makes the file read-only

D) Encrypts the file automatically

Answer: A

Rationale:

Correct is A because with calls exit to close and flush even

on exceptions. B is wrong because with never deletes files. C is

wrong because file mode, not with, controls read-only access. D is

wrong because with provides no encryption.

  1. A script must execute 'nslookup example.com' and capture output.

Which is correct?

A) subprocess.run(['nslookup','example.com'], capture_output=True,

text=True)

B) os.system('nslookup example.com') returns output string

C) subprocess.run('nslookup example.com') without list

D) exec('nslookup example.com')

Answer: A

Rationale:

Correct is A because subprocess.run with a list and

capture_output=True safely captures stdout as text. B is wrong

because os.system returns only the exit code, not output. C is

wrong because without shell=True, a single string is treated as

one program name and fails. D is wrong because exec() runs

Python code, not shell commands.

  1. Which regular expression matches a valid IPv4 octet pattern like 192 in

an IP?

A) r'\b(?:\d{1,3}.){3}\d{1,3}\b'

B) r'[0-9]+' alone

C) r'\d{4}'

D) r'[a-z]+'

Answer: A

Rationale:

Correct is A because it matches three groups of 1-3 digits followed

by a dot, then a final octet, with word boundaries. B is wrong

because it matches any digits anywhere, not an IP structure. C is

wrong because it requires exactly 4 digits. D is wrong because it

matches letters, not digits.