Docsity

WGU E010 FOUNDATIONS OF PROGRAMMING (PYTHON) - 2026 UPDATED OA, Exams of Information and Communications Technology (ICT)

Partial preview of the text

Download WGU E010 FOUNDATIONS OF PROGRAMMING (PYTHON) - 2026 UPDATED OA and more Exams Information and Communications Technology (ICT) in PDF only on Docsity!

Q1 ANALYZE AND PREDICT THE BEHAVIOR OF PYTHON CODE INVOLVING MUTABLE VS

Consider the following code:

x = [1, 2, 3]

y = x

y += [4]

z = x[:]

z += [5]

print(x, z)

What is printed?

A. [1, 2, 3, 4] [1, 2, 3, 4, 5] CORRECT

B. [1, 2, 3] [1, 2, 3, 4, 5]

C. [1, 2, 3, 4] [1, 2, 3, 5]

D. [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

RATIONALE: y = x makes y an alias; y += [4] mutates the list in place, so x becomes [1,2,3,4]. z = x[:] creates a shallow copy; z += [5] modifies only z, leaving x unchanged. Thus x is [1,2,3,4] and z is [1,2,3,4,5].

Q2 ANALYZE AND PREDICT THE BEHAVIOR OF PYTHON CODE INVOLVING MUTABLE VS

A dictionary d = {'a': 1, 'b': 2} is passed to a function that executes d['c'] = 3 and

then d = {'x': 9}. What is the state of the original dictionary after the function

returns?

A. {'a': 1, 'b': 2} B. {'a': 1, 'b': 2, 'c': 3} CORRECT C. {'x': 9} D. {'a': 1, 'b': 2, 'c': 3, 'x': 9} RATIONALE: The parameter d initially references the caller's dictionary. d['c'] = 3 mutates that object. Reassigning d = {'x': 9} only rebinds the local name, leaving the original dictionary with the added key 'c'. Thus the caller sees {'a':1,'b':2,'c':3}.

Q4 ANALYZE AND PREDICT THE BEHAVIOR OF PYTHON CODE INVOLVING MUTABLE VS

What is the result of the following set operation?

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

print(A ^ B)

A. {1, 2, 5, 6} CORRECT

B. {3, 4}

C. {1, 2, 3, 4, 5, 6}

D. set() RATIONALE: The ^ operator performs symmetric difference, returning elements in either set but not both. A ^ B = (A | B) - (A & B) = {1,2,3,4,5,6} - {3,4} = {1,2,5,6}. Option B is intersection, C is union, D is empty set.

Q5 ANALYZE AND PREDICT THE BEHAVIOR OF PYTHON CODE INVOLVING MUTABLE VS

Consider the function:

def f(n):

if n <= 1:

return n

return f(n-1) + f(n-2)

What is the value of f(6)?

A. 8 CORRECT

B. 5

C. 13

D. 21

RATIONALE: This computes the Fibonacci sequence: f(0)=0, f(1)=1, f(2)=1, f(3)=2, f(4)=3, f(5)=5, f(6)=8. Thus f(6)=8. Option B is f(5), C is f(7), D is f(8).

Q6 DESIGN AND EVALUATE FUNCTIONS, RECURSION, AND HIGHER-ORDER CONSTRUCTS

TO SOLVE COMPUTATIONAL PROBLEMS

What is the output of the following code?

try:

print(1/0)

except ZeroDivisionError:

print('Error')

finally:

print('Done')

A. Error\nDone CORRECT B. Error C. Done D. 1/ RATIONALE: The try block raises ZeroDivisionError, which is caught by the except block printing 'Error'. The finally block always executes, printing 'Done'. Thus output is 'Error' then 'Done'.

Q7 DESIGN AND EVALUATE FUNCTIONS, RECURSION, AND HIGHER-ORDER CONSTRUCTS

TO SOLVE COMPUTATIONAL PROBLEMS

Which code snippet correctly opens a file and ensures it is closed even if an

exception occurs?

A. f = open('file.txt')\ntry:\n data = f.read()\nfinally:\n f.close() B. with open('file.txt') as f:\n data = f.read() CORRECT C. f = open('file.txt')\ndata = f.read()\nf.close() D. try:\n f = open('file.txt')\n data = f.read()\nexcept:\n f.close() RATIONALE: The with statement (context manager) guarantees the file is closed even if an exception occurs. Option A also works but is more verbose and not idiomatic. Option C lacks exception safety. Option D may fail to close if an exception occurs before f is assigned or if close itself raises.

Q10 DESIGN AND EVALUATE FUNCTIONS, RECURSION, AND HIGHER-ORDER CONSTRUCTS

TO SOLVE COMPUTATIONAL PROBLEMS

What is the purpose of the yield keyword in Python?

A. It pauses a function and returns a value, making it a generator. CORRECT B. It returns a value and terminates the function. C. It raises an exception. D. It defines a variable scope. RATIONALE: yield is used in generator functions to produce a sequence of values lazily, pausing execution and resuming on subsequent calls. return terminates the function. raise raises exceptions. yield does not define scope.

Q11 APPLY APPROPRIATE DATA STRUCTURES (LISTS, TUPLES, DICTIONARIES, SETS) AND

FILE I/O TO MANAGE AND PROCESS DATA EFFICIENTLY

Consider the expression: result = [x**2 for x in range(5) if x % 2 == 0]. What is the

value of result?

A. [0, 4, 16] CORRECT

B. [0, 1, 4, 9, 16]

C. [0, 4]

D. [1, 9, 25]

RATIONALE: The comprehension filters even x (0,2,4) then squares them: 0,4,16. Option C omits 4 (which is even and <5), B includes odd squares, D squares odd numbers instead.

Q12 APPLY APPROPRIATE DATA STRUCTURES (LISTS, TUPLES, DICTIONARIES, SETS) AND

FILE I/O TO MANAGE AND PROCESS DATA EFFICIENTLY

Given the code: def f(a, b=[]): b.append(a); return b. What is the output of

print(f(1), f(2), f(3))?

A. [1] [1, 2] [1, 2, 3] CORRECT

B. [1] [2] [3]

C. [1, 2, 3] [1, 2, 3] [1, 2, 3]

D. TypeError: 'list' object is not callable RATIONALE: Mutable default arguments persist across calls, so b accumulates: [1], then [1,2], then [1,2,3]. B assumes new list each call, C misorders, D is unrelated.

Q13 APPLY APPROPRIATE DATA STRUCTURES (LISTS, TUPLES, DICTIONARIES, SETS) AND

FILE I/O TO MANAGE AND PROCESS DATA EFFICIENTLY

Which statement about Python's exception handling is FALSE?

A. The else block of a try/except/else runs only if no exception occurs in the try block. B. A finally block always executes, even if an exception is not caught. C. You can have multiple except clauses to handle different exception types. D. The except block can catch exceptions raised in the else block. CORRECT RATIONALE: Exceptions raised in the else block are not caught by the preceding except clauses; they propagate. A, B, and C are true statements about try/except/else/finally.

Q16 IMPLEMENT ROBUST ERROR HANDLING AND OBJECT-ORIENTED DESIGNS USING

EXCEPTIONS, CLASSES, AND INHERITANCE

What is the result of the expression: bool([]) == bool(0) == bool('')?

A. True CORRECT B. False C. TypeError D. ValueError RATIONALE: All three are falsy, so bool() returns False for each; False == False == False is True. No type or value errors occur.

Q17 IMPLEMENT ROBUST ERROR HANDLING AND OBJECT-ORIENTED DESIGNS USING

EXCEPTIONS, CLASSES, AND INHERITANCE

Given nums = [3, 1, 4, 1, 5], what does nums.sort(reverse=True) return and what is

nums after the call?

A. Returns None; nums becomes [5, 4, 3, 1, 1] CORRECT B. Returns [5, 4, 3, 1, 1]; nums unchanged C. Returns None; nums becomes [1, 1, 3, 4, 5] D. Returns [1, 1, 3, 4, 5]; nums unchanged RATIONALE: list.sort() sorts in place and returns None; reverse=True gives descending order. B and D confuse with sorted(), C ignores reverse=True.

Q18 IMPLEMENT ROBUST ERROR HANDLING AND OBJECT-ORIENTED DESIGNS USING

EXCEPTIONS, CLASSES, AND INHERITANCE

What is the output of: print(list(map(lambda x: x*2, filter(lambda x: x%2==0,

range(6)))))?

A. [0, 4, 8] CORRECT

B. [0, 2, 4]

C. [2, 4, 6]

D. [0, 4, 6]

RATIONALE: filter keeps evens 0,2,4; map doubles to 0,4,8. B fails to double, C starts at 2 and misses 0, D incorrectly includes 6 (odd filter would exclude 6? 6 is even but range(6) stops at 5).

Q19 INTERPRET AND DEBUG COMPLEX PYTHON PROGRAMS USING SLICING,

COMPREHENSIONS, GENERATORS, AND STANDARD LIBRARY MODULES

Which statement about Python's pass-by-assignment (often called

pass-by-object-reference) is correct?

A. Reassigning a parameter inside a function always affects the caller's variable. B. Mutating a mutable argument inside a function affects the caller's object. CORRECT C. Immutable arguments are copied before being passed to a function. D. Python uses pass-by-value for all arguments. RATIONALE: Mutation of a mutable object (e.g., list.append) changes the caller's object; reassignment only rebinds local name. A is false, C is false (no copy), D mischaracterizes Python's model.

Q22 INTERPRET AND DEBUG COMPLEX PYTHON PROGRAMS USING SLICING,

COMPREHENSIONS, GENERATORS, AND STANDARD LIBRARY MODULES

Which statement about Python's exception handling is correct when an exception

is raised inside a try block that also has an else clause?

A. The else block executes before the except block. B. The else block executes only if no exception occurs in the try block. CORRECT C. The else block executes regardless of whether an exception occurs. D. The else block is skipped if a finally block is present. RATIONALE: The else clause of a try statement runs only when the try block completes without raising an exception. It is not executed if an exception is caught, and it executes before finally if no exception occurs. Options A, C, and D misstate control flow.

Q23 WGU E010 FOUNDATIONS OF PROGRAMMING

Given the list nums = [10, 20, 30, 40, 50], what is the result of nums[1:4:2]?

A. [20, 40] CORRECT

B. [20, 30, 40]

C. [10, 30, 50]

D. [20, 40, 50]

RATIONALE: Slicing with start=1, stop=4, step=2 selects indices 1 and 3, giving [20, 40]. Option B ignores the step. Option C uses a different start/step. Option D incorrectly includes index 4 (50) despite stop=4 being exclusive.

Q24 WGU E010 FOUNDATIONS OF PROGRAMMING

A developer writes:

def add_item(item, basket=set()):

basket.add(item)

return basket

print(add_item('apple'))

print(add_item('banana'))

What is the output?

A. {'apple'} {'banana'} B. {'apple'} {'apple', 'banana'} CORRECT C. {'apple', 'banana'} {'apple', 'banana'} D. TypeError: unhashable type: 'set' RATIONALE: The default set is created once and shared across calls, so the second call mutates the same set, yielding {'apple', 'banana'}. Option A assumes new sets each call. Option C assumes both prints show the final state. Option D is incorrect because sets are mutable but hashable elements are added.

Q27 PYTHON

Which statement about Python's 'is' operator versus '==' is most accurate?

A. 'is' compares values; '==' compares identities. B. 'is' compares identities; '==' compares values. CORRECT C. Both compare values, but 'is' is faster. D. 'is' can only be used with numbers; '==' works with all types. RATIONALE: 'is' checks object identity (same memory address), while '==' checks value equality. Option A reverses them. Option C incorrectly claims both compare values. Option D falsely restricts 'is' to numbers.

Q28 PYTHON

Consider the code:

def outer():

x = 10

def inner():

nonlocal x

x += 5

inner()

return x

print(outer())

A. 10

B. 15 CORRECT

C. UnboundLocalError D. None RATIONALE: The nonlocal declaration allows inner to modify x in the enclosing scope, so x becomes 15. Option A ignores the increment. Option C would occur without nonlocal. Option D is incorrect because outer returns x.

Q29 PYTHON

Which of the following correctly reads a file and ensures it is closed even if an

exception occurs?

A. f = open('data.txt') data = f.read() f.close() B. with open('data.txt') as f: data = f.read() CORRECT C. try: f = open('data.txt') data = f.read() except: pass D. f = open('data.txt') data = f.read()

file closes automatically

RATIONALE: The with statement guarantees the file is closed via context manager even if an exception occurs. Option A lacks exception safety. Option C may leave the file open if an exception is caught. Option D falsely assumes automatic closing.

Q30 PYTHON

What is the primary purpose of a Python decorator?

A. To modify or enhance a function's behavior without changing its source code. CORRECT B. To convert a function into a class method. C. To enforce type checking at runtime. D. To automatically generate documentation for a function. RATIONALE: Decorators wrap a function to extend or alter its behavior transparently, preserving the original code. Option B describes @classmethod, a specific decorator, not the general purpose. Option C is not inherent to decorators. Option D describes docstring tools, not decorators.

Q33 2026 UPDATED OA

What is the output of: print([x*y for x in range(3) for y in range(3) if x != y])?

A. [0, 0, 2, 2, 4] CORRECT

B. [0, 0, 0, 2, 2, 4]

C. [0, 2, 4]

D. [0, 1, 2, 0, 2, 4]

RATIONALE: The nested comprehension iterates x=0,1,2 and y=0,1,2, skipping pairs where x==y, producing products 01,02,10,12,20,21 = 0,0,2,2,4. Options B and D include excluded diagonal products, and C omits valid pairs.

Q34 2026 UPDATED OA

A recursive function is defined as def g(n): return 0 if n == 0 else n + g(n-1). What

is the maximum recursion depth reached when calling g(1000) under CPython's

default settings?

A. 1000, and it completes successfully. B. Approximately 1000, but it raises RecursionError before returning. CORRECT C. 1, because Python optimizes tail recursion. D. 500, due to halving of the call stack. RATIONALE: CPython's default recursion limit is 1000, and g(1000) requires about 1001 nested frames, so a RecursionError is raised before completion. Python does not perform tail-call optimization, and the stack is not halved.

Q35 FOUNDATIONS OF COMPUTER SCIENCE - PYTHON PROGRAMMING FOUNDATIONS

Which statement about Python's exception handling is most accurate when an

exception is raised inside a finally block while another exception is propagating?

A. The original exception is preserved and the finally exception is logged. B. The finally block's exception replaces the original, and the original is chained as context. CORRECT C. Both exceptions are raised simultaneously as an ExceptionGroup. D. The finally block is skipped to preserve the original exception. RATIONALE: If a finally block raises, that new exception propagates and the original is attached as context (implicit chaining). It is not automatically grouped, and the finally block is not skipped.

Q36 FOUNDATIONS OF COMPUTER SCIENCE - PYTHON PROGRAMMING FOUNDATIONS

Given a file object f opened with open('data.txt', 'r'), which construct guarantees

the file is closed even if an exception occurs during processing?

A. try: process(f) finally: f.close() B. with open('data.txt', 'r') as f: process(f) CORRECT C. f = open('data.txt', 'r'); process(f); f.close() D. for line in f: process(line) RATIONALE: The with statement uses the context manager protocol to close the file on both normal and exceptional exit. Option A also closes but requires an explicit finally and is less idiomatic; C leaks on exception; D does not close.