Docsity

WGU D278 — Scripting and Programming Foundations 2026–2027 — Comprehensive Practice Ques, Exams of C programming

Toggle sidebar
bg1
WGU D278 — Scripting and Programming
Foundations 2026–2027 —
Comprehensive Practice Questions &
Study Guide
1. A program contains the following pseudocode:
x = 8
y = 3
x = x + y
y = x - y
x = x - y
What are the final values of x and y?
A. x = 8, y = 3
B. x = 3, y = 8
C. x = 11, y = 3
D. x = 11, y = 8
Answer: B. x = 3, y = 8
Rationale: The first statement changes x to 11. The second
changes y to 8 because 11 − 3 = 8. The final statement
changes x to 3 because 11 − 8 = 3. This effectively swaps the
two original values without using a temporary variable.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Partial preview of the text

Download WGU D278 — Scripting and Programming Foundations 2026–2027 — Comprehensive Practice Ques and more Exams C programming in PDF only on Docsity!

WGU D278 — Scripting and Programming

Foundations 2026 – 2027 —

Comprehensive Practice Questions &

Study Guide

  1. A program contains the following pseudocode: x = 8 y = 3 x = x + y y = x - y x = x - y What are the final values of x and y? A. x = 8, y = 3 B. x = 3, y = 8 C. x = 11, y = 3 D. x = 11, y = 8 Answer: B. x = 3, y = 8 Rationale: The first statement changes x to 11. The second changes y to 8 because 11 − 3 = 8. The final statement changes x to 3 because 11 − 8 = 3. This effectively swaps the two original values without using a temporary variable.
  1. Which situation most clearly requires the use of a loop rather than a simple conditional statement? A. Determining whether a student's score is at least 60 B. Selecting one of two messages based on whether a value is positive or negative C. Processing every element in a list until all elements have been examined D. Determining whether a user's password matches a stored password Answer: C. Processing every element in a list until all elements have been examined Rationale: A loop is designed to repeatedly execute a block of instructions while processing multiple values or until a condition changes. The other scenarios require a decision between alternatives and can normally be handled with conditional logic.
  2. Consider the pseudocode: total = 0 FOR i = 1 TO 5 total = total + i END FOR What is the final value of total? A. 10 B. 12

A. x > 10 AND x < 10 B. x != 10 C. x >= 10 AND x <= 10 D. x < 10 OR x > 10 Answer: C. x >= 10 AND x <= 10 Rationale: The expression requires x to be both at least 10 and at most 10. Therefore, the only possible value satisfying both conditions is exactly 10. The other expressions either exclude 10 or cannot be true when x equals 10.

  1. A program must repeatedly request a number from the user until the user enters a value between 1 and 100, inclusive. Which control structure is most appropriate? A. A single IF statement B. A WHILE or DO-WHILE loop C. A CASE statement without repetition D. A function with no condition Answer: B. A WHILE or DO-WHILE loop Rationale: The requirement involves repeated input until a validation condition is satisfied. A loop allows the program to continue requesting input while the value remains outside the permitted range. A do-while structure is especially useful when the input must occur at least once.
  2. What is the primary purpose of an algorithm?

A. To specify the exact syntax of a programming language B. To provide a finite sequence of steps for solving a problem C. To determine the hardware architecture of a computer D. To replace the need for testing Answer: B. To provide a finite sequence of steps for solving a problem Rationale: An algorithm describes a logical procedure for accomplishing a task or solving a problem. It is independent of the syntax of a particular programming language and can later be implemented using code.

  1. Consider: x = 5 IF x > 3 IF x < 10 result = "A" ELSE result = "B" END IF ELSE result = "C" END IF What is stored in result?

A. 7

B. 8

C. 9

D. 6

Answer: A. 7 Rationale: Zero-based indexing begins with index 0. Therefore, an array containing eight elements has indexes 0 through 7. The final element is consequently located at index 7.

  1. What is the output of the following pseudocode? x = 17 y = 5 result = x MOD y A. 2 B. 3 C. 3. D. 4 Answer: A. 2 Rationale: The MOD operator returns the remainder after integer division. Dividing 17 by 5 produces a quotient of 3 with a remainder of 2, so 17 MOD 5 equals 2.
  2. Which situation is most likely to produce an off-by- one error?

A. Using a Boolean variable to represent true or false B. Starting an array traversal at index 0 but iterating one position beyond its final index C. Assigning a string to a string variable D. Calling a function with two correctly typed arguments Answer: B. Starting an array traversal at index 0 but iterating one position beyond its final index Rationale: Off-by-one errors occur when a loop or calculation includes one position too many or too few. Array boundaries are a common source because programmers must correctly account for whether indexing begins at zero and where the final valid index occurs.

  1. A developer writes: IF age >= 18 status = "Adult" ELSE IF age >= 13 status = "Teen" ELSE status = "Child" What is the result when age = 18? A. Child B. Teen C. Adult D. The program produces an error

Answer: C. Floating-point Rationale: The value contains a fractional component and therefore cannot be represented as an ordinary integer without losing information. Floating-point data types are designed to represent numbers containing decimal portions.

  1. Consider the following pseudocode: count = 1 WHILE count <= 16 count = count * 2 END WHILE How many times does the loop body execute? A. 3 B. 4 C. 5 D. 16 Answer: B. 4 Rationale: The values of count at the beginning of each iteration are 1, 2, 4, and 8. After the fourth iteration, count becomes 16. The condition remains true for that next check, so the loop executes once more? Let's trace carefully: starting values are 1 → 2 → 4 → 8 → 16. At 16, the condition 16 <= 16 is true, producing 32. Therefore, the body executes 5 times. The correct answer is C. 5.
  1. Which operation should generally be performed first when designing a program to solve a complex problem? A. Write all source code immediately B. Identify and clearly define the problem requirements C. Optimize the program's runtime D. Choose variable names randomly Answer: B. Identify and clearly define the problem requirements Rationale: Effective programming begins with understanding what the program must accomplish, including inputs, outputs, constraints, and expected behavior. Coding before understanding the requirements increases the likelihood of implementing the wrong solution.
  2. What does decomposition accomplish when solving a programming problem? A. It converts all variables to integers B. It breaks a complex problem into smaller, manageable components C. It eliminates the need for algorithms D. It prevents functions from being used Answer: B. It breaks a complex problem into smaller, manageable components Rationale: Decomposition reduces complexity by dividing a large problem into smaller tasks. Those tasks can often be
  1. Which example represents a logical error rather than a syntax error? A. Missing a required closing quotation mark B. Misspelling a language keyword C. Using the wrong formula to calculate an average while the program still executes D. Omitting a required parenthesis Answer: C. Using the wrong formula to calculate an average while the program still executes Rationale: A logical error occurs when syntactically valid code produces an incorrect result because the implemented reasoning is wrong. Syntax errors generally prevent the program from being parsed or executed correctly.
  2. A programmer needs to store the names of 500 students and process each name sequentially. Which structure is most appropriate? A. A single Boolean variable B. An array or list C. A single character variable D. A constant containing one name Answer: B. An array or list Rationale: An array or list is designed to store multiple related values under one data structure. It also allows the program to iterate through the collection systematically.
  1. A loop must execute exactly 10 times regardless of user input. Which design is generally the most appropriate? A. A FOR loop with a controlled iteration count B. An IF statement C. A recursive function with no base case D. An unrestricted WHILE loop Answer: A. A FOR loop with a controlled iteration count Rationale: A for loop is particularly appropriate when the number of iterations is known in advance. A controlled counter can explicitly represent the ten required iterations, reducing the risk of an unintended infinite or prematurely terminated loop.
  2. Which principle most directly improves code maintainability? A. Repeating the same logic in every part of the program B. Using meaningful names and modular functions with clearly defined responsibilities C. Avoiding functions entirely D. Placing unrelated operations in one large procedure Answer: B. Using meaningful names and modular functions with clearly defined responsibilities Rationale: Meaningful naming makes code easier to understand, while modular functions reduce duplication and

z = 2 result = x + y * z What is the value of result? A. 22 B. 18 C. 30 D. 24 Answer: A. 22 Rationale: Multiplication has precedence over addition. Therefore, y * z is evaluated first: 7 × 2 = 14. Then 4 + 14 = 18. Wait—the correct calculation is 18 , so the correct answer is B. The key concept is operator precedence: multiplication occurs before addition.

  1. Which statement correctly describes a constant? A. A value that must change every time a loop executes B. A variable whose value is automatically recalculated C. A named value that should not change during program execution D. A temporary variable available only inside a loop Answer: C. A named value that should not change during program execution Rationale: Constants represent fixed values such as tax rates, mathematical constants, or configuration limits. Using

constants instead of repeatedly embedding literal values improves readability and reduces accidental modification.

  1. A program needs to determine whether a customer qualifies for a discount when the customer is at least 65 years old or has a membership. Which Boolean expression correctly represents the requirement? A. age >= 65 AND member == true B. age > 65 AND member == true C. age >= 65 OR member == true D. age > 65 OR member == false Answer: C. age >= 65 OR member == true Rationale: The requirement specifies two alternative conditions: being at least 65 or having membership. The OR operator is therefore required. The greater-than-or-equal comparison also ensures that a customer exactly 65 qualifies.
  2. What is the output? a = 10 b = 4 IF a > 5 AND b < 3 output = "X" ELSE
  1. A programmer writes the following loop: i = 1 WHILE i < 10 print(i) i = i + 2 END WHILE Which sequence is printed? A. 1, 2, 3, 4, 5, 6, 7, 8, 9 B. 1, 3, 5, 7, 9 C. 2, 4, 6, 8, 10 D. 1, 3, 5, 7, 9, 11 Answer: B. 1, 3, 5, 7, 9 Rationale: The initial value is 1, and two is added after every iteration. The values printed are therefore 1, 3, 5, 7, and 9. Once i becomes 11, the condition i < 10 is false.
  2. Which programming construct is specifically designed to allow a program to choose among multiple discrete alternatives based on a single expression? A. Loop B. Switch/case statement C. Variable declaration D. Function return statement Answer: B. Switch/case statement

Rationale: A switch or case construct evaluates an expression and selects an appropriate branch from multiple predefined cases. It is particularly useful when comparing one value against several discrete possibilities.

  1. A function contains a local variable named total. A different function also declares a local variable named total. What is generally true? A. Both variables must refer to the same memory location B. The second declaration automatically changes the first function's variable C. Each variable can have its own scope within its respective function D. Local variables cannot have the same name anywhere in a program Answer: C. Each variable can have its own scope within its respective function Rationale: Local variables are generally limited to the function or block where they are declared. Therefore, two different functions can independently use the same variable name without necessarily referring to the same variable.
  2. What is the primary advantage of passing information to a function through parameters? A. It makes every variable global B. It allows the function to operate on supplied values