Download D278 SCRIPTING AND PROGRAMMING FOUNDATIONS LESSONS 2 2026/2027 CURRENT Q&A and more Exams Programming Languages in PDF only on Docsity!
D278 SCRIPTING AND PROGRAMMING FOUNDATIONS
LESSONS 2 2026/2027 CURRENT TESTING (Q&A)S
HAVING WELL EXPLAINED RATIONALES.
D
WHAT THIS COVERS MULTIPLE CHOICE. 01 Scripting and Programming Foundations 02 Programming concepts aligned with WGU D 03 Covering variables 04 Data types 05 Expressions 06 Control structures EXAMINATION QUESTIONS SECTION 1 - Scripting and Programming Foundations (Questions 1 to 97)
- In Python, what is the result of the expression 3 * 'ab'? A. 'ababab' B. 'ab3' C. Error D. 'abab' Answer: A [Concept: Scripting and Programming Foundations] Rationale: In Python, multiplying a string by an integer repeats the string that many times, so 3 * 'ab' yields 'ababab'. Option B would be concatenation with a string representation of 3, which is not automatic. Option C is incorrect because the operation is valid. Option
D would result from multiplying by 2.
- What is the output of print(3 * 'ab') in Python? A. ababab B. ab C. 3ab D. Error Answer: A [Concept: Scripting and Programming Foundations] Rationale: In Python, the * operator repeats a string: 'ab' * 3 yields 'ababab'. Options B and C incorrectly treat the integer as a string or misinterpret the operation; D is false because string repetition is valid.
- Which expression evaluates to True when x = 5 and y = 10? A. x > 3 and y < 8 B. x < 3 or y > 8 C. not (x == 5) D. x != 5 and y == 10 Answer: B [Concept: Scripting and Programming Foundations] Rationale: x<3 is False, but y>8 is True; False or True is True. The other options contain a false component that makes the whole expression False.
- Which expression correctly tests whether integer n is both even and positive using short-circuit logic? A. n % 2 == 0 and n > 0 B. n % 2 == 0 or n > 0 C. n % 2 != 0 and n > 0 D. n // 2 == 0 and n > 0
error. Global variables are declared outside functions and accessible everywhere, static variables retain value between calls but remain function-scoped, and instance variables belong to objects.
- In object-oriented programming, what does encapsulation primarily achieve? A. It allows a class to inherit from multiple parent classes. B. It hides internal state and exposes controlled access via methods. C. It enables polymorphism through method overloading. D. It ensures all methods are static and cannot be overridden. Answer: B [Concept: Scripting and Programming Foundations] Rationale: Encapsulation bundles data and methods, restricting direct access to internal state (e.g., via private fields and public getters/setters). Inheritance (A), polymorphism (C), and static methods (D) are separate OOP concepts.
- Which statement about variable scope in Python is correct? A. Variables assigned inside a function are global by default. B. Variables declared with global are local to the function. C. Variables assigned inside a function are local unless declared global. D. Global variables cannot be accessed inside functions. Answer: C [Concept: Scripting and Programming Foundations] Rationale: In Python, assignments inside a function create local variables unless the global keyword is used. A is false; B reverses the meaning; D is false because globals can be read inside functions.
- In Python, what is the result of evaluating 3 * 'ab' + 'c'? A. 'abababc' B. 'abababc'
C. 'abababc' D. 'abababc' Answer: A [Concept: Scripting and Programming Foundations] Rationale: String repetition binds tighter than concatenation, so 3 * 'ab' yields 'ababab', then + 'c' appends 'c' to give 'abababc'. The other options misapply operator precedence or assume numeric addition.
- What is the primary purpose of a debugger's breakpoint when tracing a faulty loop? A. To terminate the program immediately B. To pause execution at a specified line for inspection C. To automatically fix syntax errors D. To compile the code with optimizations Answer: B [Concept: Scripting and Programming Foundations] Rationale: A breakpoint halts execution at a chosen line so the programmer can inspect variable state and step through logic. It does not terminate the program, fix errors automatically, or control compilation.
- A loop is intended to sum integers from 1 to 10 but returns 0. Which debugging step is most appropriate first? A. Rewrite the loop using a different construct. B. Check the loop condition and initialization of the accumulator. C. Add more print statements inside the loop. D. Change the loop variable type to floating-point. Answer: B [Concept: Scripting and Programming Foundations] Rationale: A sum of 0 suggests the loop never executed or the accumulator was not updated. Verifying the condition and initialization
B. do-while loop C. for loop with a range starting at zero D. for-each loop over an empty collection Answer: B [Concept: Scripting and Programming Foundations] Rationale: A do-while loop evaluates its condition after executing the body, so the body runs at least once. Pre-test while and for loops may skip entirely if the condition is false initially, and for-each over an empty collection executes zero times.
- Which expression evaluates to True given x = 5 and y = 2 in Python? A. x // y == 2 and x % y == 1 B. x / y == 2.5 and x % y == 0 C. x ** y == 10 or x // y == 3 D. not (x > y) and x != 5 Answer: A [Concept: Scripting and Programming Foundations] Rationale: x // y is 2 and x % y is 1, so both operands are True, making the conjunction True. B fails because x % y is 1, not 0; C fails because 5**2 is 25 and 5//2 is 2; D fails because x > y is True so not (x > y) is False.
- What does the break statement do inside a loop? A. Skips the rest of the current iteration and continues with the next. B. Exits the loop immediately. C. Pauses execution for a specified time. D. Restarts the loop from the first iteration. Answer: B [Concept: Scripting and Programming Foundations] Rationale: break terminates the innermost enclosing loop immediately, transferring control to the statement after the loop. Option A
describes continue, C describes a sleep function, and D is not a standard loop control behavior.
- A loop's condition is evaluated before the body executes. Which loop type guarantees zero iterations when initially false? A. do-while loop B. while loop C. for loop with a post-test condition D. repeat-until loop Answer: B [Concept: Scripting and Programming Foundations] Rationale: A while loop tests its condition before each iteration, so if the condition is false initially, the body never executes. do-while and repeat-until are post-test loops that always execute at least once. A for loop with a post-test condition is not standard; typical for loops are pre-test but the option is a nonstandard construct. Page 4 | D278 Scripting and Programming Foundations Lessons 2- Questions (2026/2027) WGU Exam Prep & Practice Review 97 Questions
- An array named scores contains [85, 92, 78, 90]. What is the value of scores[2]? A. 85 B. 92 C. 78 D. 90 Answer: C [Concept: Scripting and Programming Foundations] Rationale: Arrays are zero-indexed, so scores[2] refers to the third element, which is 78. Options A, B, and D correspond to indices 0,
B. 10.
C. 11.
D. 7.
Answer: A [Concept: Scripting and Programming Foundations] Rationale: Operator precedence evaluates 3*2=6 and 4/2=2.0 before assignment operators, so x = 5 + 6 = 11, then 11 - 2.0 = 9.0. Python's / yields float; thus 9.0 is exact. Options B, C, D reflect precedence or integer-division errors.
- In a language using static scoping, where is a variable's value resolved when referenced inside a nested function? A. In the caller's local scope at runtime B. In the function's lexical (defining) scope C. In the global scope only D. In the most recently created scope Answer: B [Concept: Scripting and Programming Foundations] Rationale: Static (lexical) scoping resolves names based on where the function is defined, not where it is called. Dynamic scoping would use the caller's scope; global-only and most-recent-scope are incorrect generalizations. Page 5 | D278 Scripting and Programming Foundations Lessons 2- Questions (2026/2027) WGU Exam Prep & Practice Review 97 Questions
- Given def modify(lst): lst.append(4); lst = [5]; a=[1,2,3]; modify(a); what is a? A. [1, 2, 3, 4] B. [5] C. [1, 2, 3]
D. [1, 2, 3, 5]
Answer: A [Concept: Scripting and Programming Foundations] Rationale: The list is modified in place via append, so a becomes [1,2,3,4]; rebinding lst to [5] does not affect a. B reflects the local rebinding, C ignores the mutation, and D incorrectly combines append and rebinding.
- What is the output of print(2 ** 3 ** 2) in Python? A. 64 B. 512 C. 12 D. 36 Answer: B [Concept: Scripting and Programming Foundations] Rationale: Exponentiation is right-associative: 2 ** (3 ** 2) = 2 ** 9 = 512. Option A assumes left-associativity; C and D are arithmetic errors.
- Which of the following best describes the difference between a compiled and an interpreted language? A. Compiled languages are always faster at runtime B. Interpreted languages translate code line-by-line during execution C. Compiled languages cannot have syntax errors D. Interpreted languages require a separate compilation step Answer: B [Concept: Scripting and Programming Foundations] Rationale: Interpreted languages execute code by translating it line-by-line at runtime, whereas compiled languages are translated entirely before execution. Compiled languages can still have syntax errors, and interpreted languages do not require a separate
Answer: C [Concept: Scripting and Programming Foundations] Rationale: An index out of bounds error happens when code attempts to access an element at an index less than 0 or greater than or equal to the array length. Uninitialized arrays, negative indices (in some languages), or mixed types do not inherently cause this error. Page 6 | D278 Scripting and Programming Foundations Lessons 2- Questions (2026/2027) WGU Exam Prep & Practice Review 97 Questions
- Given x = [1,2,3], what does x.append(4) return and what is x after? A. Returns [1,2,3,4]; x becomes [1,2,3,4] B. Returns None; x becomes [1,2,3,4] C. Returns 4; x becomes [1,2,3,4] D. Returns [1,2,3,4]; x unchanged Answer: B [Concept: Scripting and Programming Foundations] Rationale: In Python, list.append() modifies the list in place and returns None. Thus x becomes [1,2,3,4], but the method call evaluates to None. Options A and C incorrectly state a return value; D incorrectly says x is unchanged.
- Which code snippet correctly swaps the values of variables a and b in Python? A. a = b; b = a B. temp = a; a = b; b = temp C. a, b = b, a D. Both B and C Answer: D [Concept: Scripting and Programming Foundations] Rationale: Both the temporary variable method (B) and tuple assignment (C) correctly swap values. A fails because the original value
of a is lost. Since both B and C are correct, D is the best answer.
- Which statement about variable scope in block-structured languages is correct? A. A variable declared inside a block is visible throughout the entire function. B. A variable declared inside a block is visible only within that block and nested blocks. C. A variable declared inside a block is visible only after its declaration point until the end of the function. D. A variable declared inside a block is visible only within that block and its sibling blocks. Answer: B [Concept: Scripting and Programming Foundations] Rationale: Block scope limits visibility to the block where declared and any nested blocks. Option A describes function scope. Option C describes a hoisting-like behavior not typical of block scope. Option D incorrectly includes sibling blocks.
- What is the value of x after executing x = 5; x += 3 * 2? A. 16 B. 11 C. 13 D. 10 Answer: B [Concept: Scripting and Programming Foundations] Rationale: Multiplication has higher precedence than addition, so 3 * 2 evaluates to 6, and x += 6 adds 6 to 5, yielding 11. Option A incorrectly adds first, C and D misapply operator precedence or the augmented assignment.
- In a dynamically typed language, what is the result of 5 + "5"? A. 10 (integer)
D. ababcd Answer: A [Concept: Scripting and Programming Foundations] Rationale: String repetition 'ab'*3 yields 'ababab', then concatenation with 'cd' gives 'abababcd'. B omits the concatenation, C repeats the second string, and D uses only two repetitions.
- What is the result of len(set([1, 2, 2, 3, 3, 3])) in Python? A. 3 B. 6 C. 2 D. 1 Answer: A [Concept: Scripting and Programming Foundations] Rationale: Converting the list to a set removes duplicates, leaving {1,2,3}, so len is 3. B counts all elements, C counts only duplicates, and D counts unique duplicates.
- What is the value of x after: x = 10; while x > 0: x - = 3? A. - 2 B. 1 C. 0 D. - 5 Answer: A [Concept: Scripting and Programming Foundations] Rationale: The loop subtracts 3 repeatedly: 10->7->4->1->-2, then stops because - 2 is not > 0. B stops one iteration early; C and D reflect incorrect subtraction counts.
- Which statement about exception handling in Python is correct? A. The else block executes if no exception occurs in try.
B. The finally block executes only if an exception occurs. C. Multiple except blocks are not allowed. D. The try block must be followed by except, not finally. Answer: A [Concept: Scripting and Programming Foundations] Rationale: The else clause runs only when try completes without exception. finally always runs; multiple except blocks are allowed; try can be followed by finally alone.
- Which of the following best describes the difference between a syntax error and a runtime error? A. Syntax errors occur during execution; runtime errors occur during compilation. B. Syntax errors are detected by the compiler/interpreter before execution; runtime errors occur during execution. C. Syntax errors are always fatal; runtime errors can be ignored. D. Syntax errors only occur in interpreted languages; runtime errors only in compiled languages. Answer: B [Concept: Scripting and Programming Foundations] Rationale: Syntax errors violate language grammar and are caught during parsing/compilation, preventing execution. Runtime errors occur while the program runs (e.g., division by zero). Option A reverses the definitions; C and D are false generalizations.
- In Python, what is the output of print(type(1/2))? A. B. C. D.
- Which data structure stores elements in contiguous memory and allows O(1) access by index? A. Linked list B. Stack C. Array D. Queue Answer: C [Concept: Scripting and Programming Foundations] Rationale: Arrays store elements in contiguous memory, enabling constant- time O(1) access via index. Linked lists use non-contiguous nodes with O(n) access, while stacks and queues restrict access to ends.
- How many times does the inner statement execute? for i in range(3): for j in range(4): print(i, j) A. 12 B. 7 C. 3 D. 4 Answer: A [Concept: Scripting and Programming Foundations] Rationale: The outer loop runs 3 times and the inner loop runs 4 times for each outer iteration, so the print executes 3 Ć 4 = 12 times. The other options reflect additive or single-loop counts.
- What is the final value of x after: x = 10; x += 5 * 2 - 3; x /= 2; in Python 3? A. 8. B. 8. C. 11. D. 10.
Answer: A [Concept: Scripting and Programming Foundations] Rationale: Operator precedence evaluates 5*2=10, then 10-3=7; x becomes 10+7=17; then 17/2=8.5 using true division. Option B incorrectly assumes integer division, C misapplies precedence, and D miscalculates the addition.
- Given arr = [3, 1, 4, 1, 5], what does arr[1:4] return in Python? A. [3, 1, 4] B. [1, 4, 1] C. [1, 4, 1, 5] D. [3, 1, 4, 1] Answer: B [Concept: Scripting and Programming Foundations] Rationale: Python slicing uses start-inclusive, end-exclusive indices; indices 1,2,3 yield elements 1,4,1. Option A starts at 0, C extends to index 4, and D ends at index 3 inclusive incorrectly. Page 9 | D278 Scripting and Programming Foundations Lessons 2- Questions (2026/2027) WGU Exam Prep & Practice Review 97 Questions
- A function computes factorial recursively without a base case. What type of error occurs at runtime? A. RecursionError (stack overflow) B. SyntaxError C. TypeError D. ValueError Answer: A [Concept: Scripting and Programming Foundations] Rationale: Missing base case causes infinite recursion, raising RecursionError due to stack limit. SyntaxError is compile-time, TypeError/ValueError are unrelated to recursion depth.