1. Home
  2. Explore
  3. Computer Science
  4. Modularity in Game Development

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Modularity in Game Development

  • 81Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Modular Design

Why is modularity useful when a program becomes very large?

81 questions

  1. 1

    Why is modularity useful when a program becomes very large?

    1. A

      It divides a complex program into smaller parts that are easier to navigate and debug

    2. B

      It guarantees that every part of the program will fit in one file

    3. C

      It eliminates the need to test the program after it is written

    4. D

      It makes the computer execute every line of the program at the same time

    Correct answer
    A

    It divides a complex program into smaller parts that are easier to navigate and debug

    Explanation

    Modularity breaks a large problem into manageable components, avoiding the difficulty of navigating and debugging a giant block of code.

  2. 2

    In the role-playing game example, which sequence best illustrates progressively breaking down a problem?

    1. A

      Identify activities such as farming, then actions such as planting and harvesting

    2. B

      Write all farming code first, then decide which activities belong in the game

    3. C

      Create a function for every line before deciding what the game should do

    4. D

      Build the entire game, then divide it into files, then identify its features

    Correct answer
    A

    Identify activities such as farming, then actions such as planting and harvesting

    Explanation

    The game is first divided into activity categories, such as farming, and then farming is divided into actions such as planting, watering, and harvesting.

  3. 3

    Why is writing the harvest behavior separately easier than building all farming mechanics at once?

    1. A

      A smaller task lets the programmer focus on one subproblem rather than juggling the entire system

    2. B

      A smaller task automatically guarantees that the game will not contain bugs

    3. C

      A smaller task ensures that no other component will need to interact with it

    4. D

      A smaller task means the programmer can avoid deciding what the function should do

    Correct answer
    A

    A smaller task lets the programmer focus on one subproblem rather than juggling the entire system

    Explanation

    Breaking the larger problem into smaller subproblems makes each part easier to reason about and solve in isolation.

  4. 4

    How can modularity support collaboration between programmers?

    1. A

      Programmers can avoid integrating their components because modules work without connections

    2. B

      Each programmer must edit the same file so that all components remain identical

    3. C

      One programmer must complete the entire program before the others can begin

    4. D

      Each programmer can own a component or subtask and focus on it before the components are integrated

    Correct answer
    D

    Each programmer can own a component or subtask and focus on it before the components are integrated

    Explanation

    Different programmers can work on separate components or tasks, such as farming and fishing or harvesting and planting, and integrate them later.

  5. 5

    Why can testing small components increase confidence in the final program?

    1. A

      A component test proves that every possible combination of components will work

    2. B

      Testing components makes it unnecessary to check whether they work together

    3. C

      Each component can be checked directly, and tested parts are less likely to conceal defects in the full program

    4. D

      A component test automatically repairs any defects it finds

    Correct answer
    C

    Each component can be checked directly, and tested parts are less likely to conceal defects in the full program

    Explanation

    Small pieces of code are easier to verify than an entire program. Testing components as they are built provides more confidence in the final product, although integration still matters.

  6. 6

    What is the main organizational benefit of grouping code by task?

    1. A

      It removes the need for names that describe what the code does

    2. B

      It prevents a program from needing multiple functions

    3. C

      It ensures that all instructions are executed in alphabetical order

    4. D

      It makes related instructions easier to locate than they would be in one giant block

    Correct answer
    D

    It makes related instructions easier to locate than they would be in one giant block

    Explanation

    Organizing code by task makes it easier to find the functionality needed and makes the code more readable and workable.

  7. 7

    Which description best captures the relationship between modules and functions in the described organization?

    1. A

      Modules group related functionality, and functions within them perform specific tasks

    2. B

      Modules contain only variables, while functions contain only comments

    3. C

      Functions group related modules, and modules perform individual instructions

    4. D

      Modules and functions are interchangeable names for a single code statement

    Correct answer
    A

    Modules group related functionality, and functions within them perform specific tasks

    Explanation

    A module or file typically contains related functionality; functions within it contain instructions for particular tasks.

  8. 8

    Which set of benefits is associated with organizing code into modules and functions?

    1. A

      Greater reusability, testability, and maintainability

    2. B

      Removal of all repeated logic, all errors, and all dependencies

    3. C

      Automatic collaboration, fewer requirements, and no integration work

    4. D

      Guaranteed faster execution, fewer variables, and no need for debugging

    Correct answer
    A

    Greater reusability, testability, and maintainability

    Explanation

    The content identifies reusable, testable, maintainable, readable, and easier-to-navigate code as benefits of organizing programs into functions and modules.

  9. 9

    What is a function in the content's description?

    1. A

      A condition that determines whether a program should stop

    2. B

      A named block of code that performs a specific task

    3. C

      A file that automatically executes every program in a project

    4. D

      A value that can store any number of instructions without a name

    Correct answer
    B

    A named block of code that performs a specific task

    Explanation

    A function packages lines of code that perform a task and attaches a name to that set of instructions.

  10. 10

    How does naming a set of instructions with a function help reduce repeated code?

    1. A

      The function can be defined once and called repeatedly wherever its task is needed

    2. B

      The function makes repeated tasks execute without being called

    3. C

      The function copies its instructions into every possible location in the file

    4. D

      The function converts all repeated instructions into a single variable value

    Correct answer
    A

    The function can be defined once and called repeatedly wherever its task is needed

    Explanation

    After defining a function once, the programmer can call its name to perform the task again without rewriting its instructions.

  11. 11

    What role do a function's input values play?

    1. A

      They store the function's output in global scope automatically

    2. B

      They replace the need for a function body

    3. C

      They permanently determine the one value that the function can ever process

    4. D

      They let the function perform a general task using different values on different calls

    Correct answer
    D

    They let the function perform a general task using different values on different calls

    Explanation

    Inputs allow a function to generalize its task, such as rounding different numbers or handling potions of different colors.

  12. 12

    In a Python function definition, what is the function header?

    1. A

      The indented block that contains the function's task instructions

    2. B

      The first function call that supplies the input values

    3. C

      The return statement that produces the output value

    4. D

      The line beginning with def that gives the function name and parameters and ends with a colon

    Correct answer
    D

    The line beginning with def that gives the function name and parameters and ends with a colon

    Explanation

    The function header consists of the definition line: the def keyword, function name, parentheses containing parameters, and a colon.

  13. 13

    What are parameters in a function definition?

    1. A

      Names for modules that contain the function

    2. B

      Values supplied only after the function has finished running

    3. C

      Names for input variables that are accessible within the function body

    4. D

      Statements that cause the function to return to its caller

    Correct answer
    C

    Names for input variables that are accessible within the function body

    Explanation

    Parameters are variable names specified in the function definition; they are used within the function body to refer to inputs.

  14. 14

    How does Python identify the function body?

    1. A

      The body consists of every line in the file after the function definition

    2. B

      The body consists only of the function's parameters and return value

    3. C

      The body must be placed in a separate module from the function header

    4. D

      The body consists of instructions indented beneath the function header

    Correct answer
    D

    The body consists of instructions indented beneath the function header

    Explanation

    The function body is the indented set of instructions that performs the function's task.

  15. 15

    What does a return statement do in a function?

    1. A

      It stores every local variable in global memory

    2. B

      It supplies an output value and ends execution of that function call

    3. C

      It repeats the function body until a condition becomes true

    4. D

      It defines the function name and lists its input parameters

    Correct answer
    B

    It supplies an output value and ends execution of that function call

    Explanation

    The value after return is the function's output, and reaching return exits the function and sends execution back to the call site.

  16. 16

    What are arguments in a function call?

    1. A

      The output values stored in a module

    2. B

      The indented instructions that make up the function body

    3. C

      The variable names listed in the function definition

    4. D

      The input values passed to the function when it is called

    Correct answer
    D

    The input values passed to the function when it is called

    Explanation

    Arguments are the actual input values placed in a function call; they are bound to the corresponding parameters.

  17. 17

    Why must a function definition appear before its function call in the described program?

    1. A

      The function body is created only after the first argument is evaluated

    2. B

      Python requires every function call to be at the end of a file

    3. C

      The function call determines the function's name after it is executed

    4. D

      The computer needs to learn the function's task before it can execute that task

    Correct answer
    D

    The computer needs to learn the function's task before it can execute that task

    Explanation

    The definition teaches the computer the function's instructions, so it must be encountered before a call that tries to use them.

  18. 18

    When a function call supplies multiple arguments, how are they matched to parameters in the described model?

    1. A

      In reverse order, with the last argument assigned to the first parameter

    2. B

      By selecting whichever parameter has the broadest scope

    3. C

      In order, with the first argument assigned to the first parameter

    4. D

      By matching each argument's value to a parameter with the same name

    Correct answer
    C

    In order, with the first argument assigned to the first parameter

    Explanation

    The argument-to-parameter binding is order-dependent: the first argument corresponds to the first parameter, and so on.

  19. 19

    What happens to a function call expression that returns a value?

    1. A

      The expression always evaluates to the value zero

    2. B

      The expression evaluates to the function's parameter names

    3. C

      The expression creates a new module containing the function

    4. D

      The expression evaluates to the value returned by the function

    Correct answer
    D

    The expression evaluates to the value returned by the function

    Explanation

    After the function returns, its call expression stands for the returned value and can be used in a larger expression or assignment.

  20. 20

    What happens if a function returns a value but the caller does not store, print, or otherwise use it?

    1. A

      The function call is skipped because its result is unused

    2. B

      The value is automatically saved as a global variable

    3. C

      The function still computes the value, but the unused result is discarded

    4. D

      The return value is reused as the next call's argument

    Correct answer
    C

    The function still computes the value, but the unused result is discarded

    Explanation

    A function can run and return a value even when the calling code does nothing with it; in that case, the result is wasted.

  21. 21

    When Python encounters a function definition during program execution, what does it do before a call occurs?

    1. A

      It immediately prints the function's return value

    2. B

      It creates every local variable that the function might later use

    3. C

      It runs the function body once using default argument values

    4. D

      It records the function's instructions and identifying information without executing the body

    Correct answer
    D

    It records the function's instructions and identifying information without executing the body

    Explanation

    Encountering def teaches the computer the task by storing the function instructions and information such as its name and parameter count; it does not execute the body at that point.

  22. 22

    What is a stack frame in the function-execution model described?

    1. A

      A shared area where all local and global variables are combined

    2. B

      An isolated region of short-term memory for a particular function call

    3. C

      A permanent file where all function definitions are stored

    4. D

      A list of every module imported by a program

    Correct answer
    B

    An isolated region of short-term memory for a particular function call

    Explanation

    A stack frame is an isolated chunk of short-term memory used while a function call is executing.

  23. 23

    What happens to a function's stack frame when the function returns?

    1. A

      It is copied into the module that originally defined the function

    2. B

      It is kept permanently so that the next call can reuse all its local variables

    3. C

      It is popped off, and its local memory is cleared while the return value goes back to the caller

    4. D

      It is merged into the global scope, including every local variable

    Correct answer
    C

    It is popped off, and its local memory is cleared while the return value goes back to the caller

    Explanation

    On return, execution goes back to the call site and the function's stack frame is removed; its return value is carried back, but its local variables do not persist.

  24. 24

    If a function changes a local variable that has the same name as a global variable, what happens to the global variable?

    1. A

      The global variable remains unchanged because the function's local variable is separate

    2. B

      Both variables are merged into one value in the module scope

    3. C

      The local variable is ignored because global variables take priority

    4. D

      The global variable is always overwritten when the function returns

    Correct answer
    A

    The global variable remains unchanged because the function's local variable is separate

    Explanation

    A variable assigned within a function is stored in that function's local stack frame, so it does not overwrite a same-named global variable.

  25. 25

    What does variable scope describe?

    1. A

      The order in which modules are imported

    2. B

      The region of a program where a variable can be accessed

    3. C

      The number of times a variable can be assigned

    4. D

      The data type that a function must return

    Correct answer
    B

    The region of a program where a variable can be accessed

    Explanation

    Scope refers to the region of a program in which a name can be accessed.

  26. 26

    Where are names defined at the top level of a program stored in the described model?

    1. A

      In global scope, where they can be accessed throughout the program

    2. B

      Only in the module that imports the program

    3. C

      In the local scope of the next function that is called

    4. D

      In a separate stack frame that disappears after each statement

    Correct answer
    A

    In global scope, where they can be accessed throughout the program

    Explanation

    Top-level names are global: they are stored in the base level of memory and can be accessed throughout the program.

  27. 27

    Where are variables assigned as part of a function call stored by default?

    1. A

      In global scope alongside all top-level names

    2. B

      In the local scope of that function call

    3. C

      In the scope of every module in the program

    4. D

      In permanent memory that remains after the program ends

    Correct answer
    B

    In the local scope of that function call

    Explanation

    Function parameters and variables assigned in a function are stored in that call's local stack frame.

  28. 28

    How does Python search for a name used inside a function, according to the described scope model?

    1. A

      It searches only the function's parameters and never checks enclosing scopes

    2. B

      It checks the local scope first, then searches enclosing scopes such as global scope

    3. C

      It checks global scope first, then searches local scope only if necessary

    4. D

      It searches every module simultaneously and combines matching values

    Correct answer
    B

    It checks the local scope first, then searches enclosing scopes such as global scope

    Explanation

    Name lookup begins in the closest scope, such as the function's local scope, and moves outward to enclosing scopes if needed.

  29. 29

    What happens if a name cannot be found in the local or any enclosing scope?

    1. A

      Python silently substitutes the function's return value

    2. B

      Python creates a global variable with a default value

    3. C

      The name is automatically imported from the nearest module

    4. D

      The program raises a name error when it tries to use the name

    Correct answer
    D

    The program raises a name error when it tries to use the name

    Explanation

    If the search runs out of scopes without finding the name, Python raises a name error.

  30. 30

    Why is using the global keyword inside functions generally discouraged in the content?

    1. A

      It encourages functions to modify shared state and can create naming conflicts in large programs

    2. B

      It makes all parameters inaccessible inside the function body

    3. C

      It prevents functions from returning values to their callers

    4. D

      It causes every function call to create a separate module

    Correct answer
    A

    It encourages functions to modify shared state and can create naming conflicts in large programs

    Explanation

    Relying on global variables can create hidden dependencies and collisions; local scope allows functions to reuse names without conflicting with other code.

  31. 31

    What is the preferred way for a function to communicate a calculated result back to its caller?

    1. A

      Create a new local variable in the caller's stack frame directly

    2. B

      Change a global variable with the same name as the result

    3. C

      Print the result and assume the caller can use the printed text

    4. D

      Return the result and let the caller store or use it

    Correct answer
    D

    Return the result and let the caller store or use it

    Explanation

    The return value is the reliable way for a function to communicate information outward; the caller can then store that value in its own scope.

  32. 32

    Why is passing a needed value as a function parameter safer than relying on a global variable?

    1. A

      It makes the caller provide the value explicitly instead of depending on hidden program state

    2. B

      It guarantees that the function will never receive an incorrect value

    3. C

      It makes the parameter available to every function in the program

    4. D

      It lets the function modify the caller's local variables directly

    Correct answer
    A

    It makes the caller provide the value explicitly instead of depending on hidden program state

    Explanation

    Parameters make a function's requirements explicit. Relying on a global name assumes that some external variable exists and has been set correctly.

  33. 33

    What happens when one function calls another function before the first has returned?

    1. A

      The outer function's stack frame is deleted before the inner function begins

    2. B

      Both functions share one stack frame and overwrite each other's local variables

    3. C

      The inner function is postponed until the entire program has terminated

    4. D

      The inner call gets a new stack frame while the outer call's frame remains available beneath it

    Correct answer
    D

    The inner call gets a new stack frame while the outer call's frame remains available beneath it

    Explanation

    The outer call is paused but remains on the stack; the inner call creates a new frame above it, then returns to the paused outer function.

  34. 34

    How does the computer manage several levels of nested function calls?

    1. A

      It executes the deepest function first without recording where it was called

    2. B

      It stores every call's variables in global scope until the program ends

    3. C

      It replaces all earlier frames with the newest function's frame

    4. D

      It adds a frame for each call and removes the top frame whenever that call returns

    Correct answer
    D

    It adds a frame for each call and removes the top frame whenever that call returns

    Explanation

    Each call adds a stack frame on top, and each return pops off the current top frame, preserving the state of calls that have not finished.

  35. 35

    What is a key problem with putting movement, environment adjustment, and detection checking into one very large function?

    1. A

      The function automatically executes its code in global scope

    2. B

      The function becomes harder to understand, test, and reuse because it performs several different tasks

    3. C

      The function is unable to return a Boolean value

    4. D

      The function cannot accept more than one parameter

    Correct answer
    B

    The function becomes harder to understand, test, and reuse because it performs several different tasks

    Explanation

    A function with many responsibilities is harder to read, its final result is harder to diagnose, and its functionality is less convenient to reuse separately.

  36. 36

    In the stealth-game example, what should a movement-volume function do?

    1. A

      Take a detection result and return the number of guards

    2. B

      Take an environment and return whether a guard detects the player

    3. C

      Take a random threshold and return the player's movement type

    4. D

      Take a base volume and movement type, then return the adjusted volume

    Correct answer
    D

    Take a base volume and movement type, then return the adjusted volume

    Explanation

    The movement function isolates one task: adjusting a base volume according to the player's movement type.

  37. 37

    What task does the environment-adjustment function perform in the example?

    1. A

      It selects a random environment and changes the player's movement

    2. B

      It takes a volume and environment type, then returns the adjusted volume

    3. C

      It takes a movement type and returns the number of game levels

    4. D

      It compares the player's volume to a random threshold and returns a string

    Correct answer
    B

    It takes a volume and environment type, then returns the adjusted volume

    Explanation

    The environment function separately adjusts a volume based on the environment, such as grass or metal.

  38. 38

    Why separate the footstep-volume calculation from the detection-threshold check?

    1. A

      Separating them ensures the player can never be detected

    2. B

      The two tasks are independent, so each can be tested or reused without being tied to the other

    3. C

      The volume calculation cannot accept an environment as an input

    4. D

      The threshold check must be performed before any volume can be calculated

    Correct answer
    B

    The two tasks are independent, so each can be tested or reused without being tied to the other

    Explanation

    Calculating volume and checking that volume against a threshold are distinct tasks. Separating them creates reusable functions, such as an independent detection check.

  39. 39

    How can a wrapper function combine smaller functions to determine whether a player is detected?

    1. A

      It can obtain the adjusted footstep volume from smaller functions and pass that volume to a detection function

    2. B

      It can use only the movement type and ignore the environment and threshold

    3. C

      It can return the names of the smaller functions without executing them

    4. D

      It can copy all smaller functions into a global variable and skip their calls

    Correct answer
    A

    It can obtain the adjusted footstep volume from smaller functions and pass that volume to a detection function

    Explanation

    A wrapper can call the movement and environment functions to calculate volume, then supply that volume to an independent detection function.

  40. 40

    Why are function names such as movement-volume and environment-adjustment functions helpful?

    1. A

      Descriptive names cause the function to execute without a call

    2. B

      Descriptive names guarantee that the implementation is correct

    3. C

      Descriptive names document the task and can reduce the need for explanatory comments

    4. D

      Descriptive names make parameters unnecessary

    Correct answer
    C

    Descriptive names document the task and can reduce the need for explanatory comments

    Explanation

    Names that clearly describe a function's task make the code more self-documenting and easier to understand.

  41. 41

    How does decomposing a large function make its behavior easier to test?

    1. A

      The functions no longer need input values, so tests are unnecessary

    2. B

      Each smaller function has a focused responsibility with outputs that are easier to check

    3. C

      Each smaller function automatically tests every possible input

    4. D

      The combined program can be tested only after every feature is complete

    Correct answer
    B

    Each smaller function has a focused responsibility with outputs that are easier to check

    Explanation

    A function that does one specific task is easier to test in isolation; for example, crouching should reduce volume and running should increase it.

  42. 42

    How does decomposing functions help when adding voice volume to the stealth game?

    1. A

      The program can omit environment effects because they apply only to footsteps

    2. B

      The movement function can be renamed to make it handle voice automatically

    3. C

      The entire detection system must be rewritten to avoid using footstep volume

    4. D

      The environment-adjustment and detection functions can be reused for voice without rebuilding those tasks

    Correct answer
    D

    The environment-adjustment and detection functions can be reused for voice without rebuilding those tasks

    Explanation

    Voice volume can use the already-built environment adjustment and independent detection check, demonstrating how decoupled functions support reuse.

  43. 43

    What is a Python module in the described file-based organization?

    1. A

      A stack frame containing the variables from one function call

    2. B

      A section of a file that is identified only by its indentation

    3. C

      A special kind of function that must return a Boolean

    4. D

      A separate file with a .py extension that groups related code

    Correct answer
    D

    A separate file with a .py extension that groups related code

    Explanation

    A module is created as a separate Python file, conventionally with a .py extension, to hold related functionality.

  44. 44

    Why must a program import a module before using functions defined in that module?

    1. A

      The import makes the module's names available to the program

    2. B

      The import permanently saves the functions across program executions

    3. C

      The import converts the module's functions into global variables

    4. D

      The import runs the main program in every module automatically

    Correct answer
    A

    The import makes the module's names available to the program

    Explanation

    Without an import, the program does not know where to find the functions moved into another file.

  45. 45

    How is a function from an imported module typically called in the described approach?

    1. A

      By copying the function definition into every call site

    2. B

      By placing the function name inside a return statement

    3. C

      By using the module name as the function's argument

    4. D

      By prefixing the function name with the module name

    Correct answer
    D

    By prefixing the function name with the module name

    Explanation

    A module-qualified call, such as module_name.function_name, tells the computer to look for the function within that module's namespace.

  46. 46

    What is an entry point in a multi-file Python program?

    1. A

      The single file chosen to begin execution and trigger the program's logic

    2. B

      The file that contains every function used by the program

    3. C

      The first function alphabetically in any imported module

    4. D

      The last module to be imported before the program ends

    Correct answer
    A

    The single file chosen to begin execution and trigger the program's logic

    Explanation

    Although program logic can span many files, execution starts from one specified file, called the entry point or main logic.

  47. 47

    What does importing a module do from the computer's execution perspective, according to the content?

    1. A

      It automatically makes every name in the imported file global

    2. B

      It permanently switches execution to the imported file and never returns

    3. C

      It executes only the imported file's comments and function calls

    4. D

      It executes the module's contents and then returns to the importing file

    Correct answer
    D

    It executes the module's contents and then returns to the importing file

    Explanation

    Execution jumps to the imported module and, when it is done, returns to the importing module to continue where it left off.

  48. 48

    What is the effect of module scope on function names?

    1. A

      Names are accessible only inside the function that defines them

    2. B

      Names are automatically shared globally among every imported module

    3. C

      Names are associated with the module where they are defined, so separate modules can use the same function name

    4. D

      Names must be unique across all files in the entire program

    Correct answer
    C

    Names are associated with the module where they are defined, so separate modules can use the same function name

    Explanation

    Module scope adds a namespace layer. For example, two modules can each define get_score without their names colliding.

  49. 49

    Why can top-level print statements in an imported module cause confusion?

    1. A

      They are converted into comments when the module is loaded

    2. B

      They prevent the module from being imported more than once

    3. C

      They run when the module is imported, potentially producing output from many modules

    4. D

      They run only when a function in the module returns a value

    Correct answer
    C

    They run when the module is imported, potentially producing output from many modules

    Explanation

    Top-level statements execute on import. Print statements across chains of imported modules can make it hard to determine where output came from.

  50. 50

    Which is a practical benefit of putting related functions into a well-organized module?

    1. A

      The module eliminates the need for a program entry point

    2. B

      The module prevents any other file from importing its functions

    3. C

      Programmers can find, reuse, and focus on a related set of functionality

    4. D

      The module automatically verifies that every function is correct

    Correct answer
    C

    Programmers can find, reuse, and focus on a related set of functionality

    Explanation

    Modules help programmers navigate to related functionality, reuse it in multiple places, and focus on one area of a program at a time.

  51. 51

    In the card-game example, what does the draw-hand function need as an input to support different games?

    1. A

      The number of suits that exist in a playing card

    2. B

      The number of cards to draw for the hand

    3. C

      The name of the module that contains the deck

    4. D

      The final string representation of every possible card

    Correct answer
    B

    The number of cards to draw for the hand

    Explanation

    Different games use different hand sizes, so draw_hand takes the desired number of cards as a parameter.

  52. 52

    How does the draw-hand function build a hand in the described design?

    1. A

      It selects one complete hand from a list of prewritten hands

    2. B

      It draws all possible cards and removes the ones not requested

    3. C

      It calls itself once for every suit and then ignores its parameter

    4. D

      It loops the requested number of times, draws a card each iteration, and adds it to the hand

    Correct answer
    D

    It loops the requested number of times, draws a card each iteration, and adds it to the hand

    Explanation

    A loop draws one card per iteration and accumulates each card into the hand until the requested number has been drawn.

  53. 53

    How can a newline character help display a hand of cards?

    1. A

      It causes the card-drawing loop to begin a new iteration

    2. B

      It converts every card rank into an integer

    3. C

      It prevents a card from appearing more than once

    4. D

      It makes Python display each card on a separate line

    Correct answer
    D

    It makes Python display each card on a separate line

    Explanation

    The newline character, written as a backslash followed by n, is interpreted like pressing Enter, so cards can appear on separate lines.

  54. 54

    How is a playing card represented in the example, and how is a random card selected?

    1. A

      As a string combining a randomly selected suit and a randomly selected rank

    2. B

      As a Boolean that indicates whether the card is a face card

    3. C

      As a function that selects a suit but never selects a rank

    4. D

      As an integer selected from a range that represents the entire deck

    Correct answer
    A

    As a string combining a randomly selected suit and a randomly selected rank

    Explanation

    The design represents a card with a string such as queen of hearts and selects its suit and rank as separate pieces.

  55. 55

    How does the example select a suit with equal likelihood?

    1. A

      It returns a fixed suit and randomizes only the rank

    2. B

      It generates a number from one through thirteen and maps each to a suit

    3. C

      It selects the first suit in a list and changes only when the caller asks

    4. D

      It generates a random number from one through four and maps each outcome to a suit

    Correct answer
    D

    It generates a random number from one through four and maps each outcome to a suit

    Explanation

    There are four suits, so the example randomly selects a number from one through four and maps the outcomes to hearts, diamonds, spades, or clubs.

  56. 56

    Why should the rank function return a consistent data type?

    1. A

      A consistent type guarantees that the random rank is unique

    2. B

      Callers should be able to combine its results without encountering type mismatches

    3. C

      The function cannot use conditionals if it returns different types

    4. D

      Python functions can return only integers unless they are in a module

    Correct answer
    B

    Callers should be able to combine its results without encountering type mismatches

    Explanation

    The example encountered a type error when an integer rank was combined with a string. Casting numeric ranks to strings makes the function's return type consistent.

  57. 57

    How does a Boolean parameter support games that exclude face cards?

    1. A

      The function always removes every card except aces when the parameter is true

    2. B

      The parameter prevents the caller from choosing the hand size

    3. C

      The parameter determines whether the suit is a string or an integer

    4. D

      The caller chooses whether face cards are enabled, and disabled mode selects only ranks from two through ten

    Correct answer
    D

    The caller chooses whether face cards are enabled, and disabled mode selects only ranks from two through ten

    Explanation

    A Boolean option lets the caller decide whether face cards are allowed; when they are disabled, the rank selection uses the range from two through ten.

  58. 58

    What limitation results from modeling card draws without maintaining deck state?

    1. A

      The program cannot represent suits as strings

    2. B

      The same card can be drawn more than once, even if a particular game requires one of each card

    3. C

      The draw-hand function cannot support different hand sizes

    4. D

      The caller cannot choose whether face cards are enabled

    Correct answer
    B

    The same card can be drawn more than once, even if a particular game requires one of each card

    Explanation

    Because the model does not track which cards have already been drawn, duplicates can occur. This may be acceptable for some games but incorrect for others.

  59. 59

    What is the central rule of Nim in the described version?

    1. A

      Players remove any number of stones, and whoever removes none loses

    2. B

      Players take turns removing one or two stones, and whoever removes the last stone loses

    3. C

      Players take turns adding one or two stones, and whoever adds the last stone wins

    4. D

      Players remove exactly one stone each turn, and the first player to stop wins

    Correct answer
    B

    Players take turns removing one or two stones, and whoever removes the last stone loses

    Explanation

    The described Nim game allows each player to remove one or two stones per turn; taking the final stone causes that player to lose.

  60. 60

    Why should the computer not take a turn after the user removes the last stone?

    1. A

      The pile is automatically refilled before the computer's turn

    2. B

      The game has already ended, so the computer should not receive another turn

    3. C

      The user and computer must always take the same number of turns

    4. D

      The computer is allowed to move only before the user takes a turn

    Correct answer
    B

    The game has already ended, so the computer should not receive another turn

    Explanation

    The program must check whether the pile has run out after the user's move; if so, the game is over and the computer should not move.

  61. 61

    How does a Boolean turn variable help alternate the players?

    1. A

      It determines whether the input prompt should accept numbers or strings

    2. B

      One value indicates the user's turn and the other indicates the computer's turn, and the value is switched each iteration

    3. C

      It records the number of stones remaining and decreases after each move

    4. D

      It stores the winner's name until the next game begins

    Correct answer
    B

    One value indicates the user's turn and the other indicates the computer's turn, and the value is switched each iteration

    Explanation

    A Boolean can represent whose turn it is: one state selects the user, the other selects the computer, and the state is toggled after each turn.

  62. 62

    Why can the program determine the winner after the game loop by examining the turn variable?

    1. A

      The variable is set by the input function when the pile reaches zero

    2. B

      The variable is switched at the bottom of the loop, so it identifies the player who would move next rather than the player who just moved

    3. C

      The variable is never changed, so it always identifies the winner

    4. D

      The variable stores the number of turns, which directly gives the winner's identity

    Correct answer
    B

    The variable is switched at the bottom of the loop, so it identifies the player who would move next rather than the player who just moved

    Explanation

    After each move, the turn state is switched. If the loop ends because the pile is empty, the state indicates the next player, meaning the other player took the last stone.

  63. 63

    Why should the user-pick function validate the user's input?

    1. A

      To ensure that the pile begins with a random number of stones

    2. B

      To ensure that the user always removes the last stone

    3. C

      To ensure that the computer receives the same input value

    4. D

      To ensure that it returns only an allowed choice of one or two stones

    Correct answer
    D

    To ensure that it returns only an allowed choice of one or two stones

    Explanation

    The function repeats its prompt until the user enters one or two, so it returns only a move permitted by the game.

  64. 64

    What is the purpose of a separate function that displays the stone pile?

    1. A

      It isolates how the pile is drawn so its appearance can change without altering the main game logic

    2. B

      It replaces the need to update the pile after each turn

    3. C

      It validates the user's move before the game loop begins

    4. D

      It decides which player wins without checking the pile

    Correct answer
    A

    It isolates how the pile is drawn so its appearance can change without altering the main game logic

    Explanation

    A display function can take the number of stones and return a picture, allowing the presentation to change independently of the main game logic.

  65. 65

    In the strategy described for the computer player, why is leaving four stones to the opponent desirable?

    1. A

      Four stones allow the opponent to win immediately without making a move

    2. B

      The computer can remove all four stones in one turn

    3. C

      The opponent is forced into a position where either permitted move can leave the computer to take the last stone

    4. D

      The game ends automatically when four stones remain

    Correct answer
    C

    The opponent is forced into a position where either permitted move can leave the computer to take the last stone

    Explanation

    If four stones are left, taking one leaves three and taking two leaves two; the computer can then respond so the opponent is eventually forced to take the last stone.

  66. 66

    According to the partial algorithm described, which moves should the computer make for piles of five and six stones?

    1. A

      Take two from both piles to leave three stones in each case

    2. B

      Take one from five and two from six to leave four stones

    3. C

      Take two from five and one from six to leave three stones

    4. D

      Take one from both piles to leave four stones in each case

    Correct answer
    B

    Take one from five and two from six to leave four stones

    Explanation

    The described strategy aims to leave four stones: take one from five or two from six. The content also identifies taking one for piles of one, two, or five, and two for three or six.

  67. 67

    What is a unit test designed to test?

    1. A

      Whether a program's user interface is visually appealing

    2. B

      One unit of functionality at a time, commonly a single function

    3. C

      Only the speed of the program under heavy load

    4. D

      Every possible interaction across the entire finished program

    Correct answer
    B

    One unit of functionality at a time, commonly a single function

    Explanation

    Unit tests focus on a single unit of functionality, which in many cases is one function.

  68. 68

    What does a unit test assertion check?

    1. A

      Whether the function has the same name as its module

    2. B

      Whether the function's actual result matches the expected result for given inputs

    3. C

      Whether every line of the program is printed to the console

    4. D

      Whether the program uses a particular number of files

    Correct answer
    B

    Whether the function's actual result matches the expected result for given inputs

    Explanation

    An assertion compares the function's result with the result expected for specified arguments; a match passes the test, and a mismatch fails it.

  69. 69

    How can automated unit tests help a programming team review changes?

    1. A

      They allow a team to avoid specifying expected behavior

    2. B

      They guarantee that no future test case will ever fail

    3. C

      They automatically rewrite any function that fails a test

    4. D

      They can run after changes and prevent submission until related tests pass

    Correct answer
    D

    They can run after changes and prevent submission until related tests pass

    Explanation

    Teams can automate tests to run when code changes and block a change from being submitted until the related tests pass.

  70. 70

    Why can a unit test be described as a living contract for a function?

    1. A

      It replaces the need to communicate requirements to callers

    2. B

      It prevents the function from ever changing its behavior

    3. C

      It records expected behavior and can be updated when the requirements intentionally change

    4. D

      It records every variable name used in the entire program

    Correct answer
    C

    It records expected behavior and can be updated when the requirements intentionally change

    Explanation

    Tests enforce the function's requirements. If the requirements intentionally change, the test can be updated alongside the code change.

  71. 71

    Why is one test case usually insufficient to establish that a function works?

    1. A

      A function that always returns one fixed value could still pass that single test

    2. B

      A test case cannot include an expected result

    3. C

      A single test case always executes every conditional branch

    4. D

      A single test case causes the function to run only once per program

    Correct answer
    A

    A function that always returns one fixed value could still pass that single test

    Explanation

    One input-output example does not establish that the function behaves correctly for other inputs; a function that always returns the tested answer might pass.

  72. 72

    What does code coverage aim to achieve when selecting unit tests?

    1. A

      Tests should use every variable name in the program

    2. B

      Tests should execute every possible path through the function

    3. C

      Tests should avoid conditional branches so results are predictable

    4. D

      Tests should call every function exactly once with the same argument

    Correct answer
    B

    Tests should execute every possible path through the function

    Explanation

    Code coverage aims for tests that exercise every possible execution path, often by testing representative classes of inputs for each branch.

  73. 73

    How should programmers select representative test inputs for a function with conditional branches?

    1. A

      Identify distinct input classes that lead to different branches and test each class

    2. B

      Test only the smallest and largest values and ignore intermediate branches

    3. C

      Choose many inputs from only the most common branch

    4. D

      Choose inputs randomly without considering the function's conditions

    Correct answer
    A

    Identify distinct input classes that lead to different branches and test each class

    Explanation

    Programmers can identify classes of inputs that exercise different branches and write a representative test case for each class.

  74. 74

    What should a team do when it discovers a bug that was not caught by existing tests?

    1. A

      Add a test for the failure case so the same bug is less likely to recur

    2. B

      Change the function name so the old tests no longer apply

    3. C

      Delete the existing tests because they did not catch the bug

    4. D

      Avoid testing that input again to keep the test suite small

    Correct answer
    A

    Add a test for the failure case so the same bug is less likely to recur

    Explanation

    Teams commonly add a test for the exact failure case so that the bug is detected if it returns in the future.

  75. 75

    Which statement correctly describes Python unit-testing tools mentioned in the content?

    1. A

      The tools replace the need to define expected results

    2. B

      Python requires every program to use both tools together

    3. C

      Programmers commonly use the unittest module or the pytest module

    4. D

      The tools test only performance and cannot test function outputs

    Correct answer
    C

    Programmers commonly use the unittest module or the pytest module

    Explanation

    The content names unittest and pytest as commonly used Python unit-testing frameworks.

  76. 76

    For a beginner learning independently, what testing practice does the content still recommend even if automated unit tests are not worthwhile yet?

    1. A

      Avoid testing functions until the entire program has been completed

    2. B

      Change the function after every call, regardless of the result

    3. C

      Write a separate test for every possible input value

    4. D

      Call a function with several different arguments and check that it behaves as expected

    Correct answer
    D

    Call a function with several different arguments and check that it behaves as expected

    Explanation

    Even when writing automated tests may take too much time, manually trying several arguments is a useful habit for checking functions.

  77. 77

    When debugging a stack trace involving nested function calls, where does the content recommend focusing first?

    1. A

      Near the bottom, where the actual error and its line are reported

    2. B

      On the module name, because it contains the error message

    3. C

      At the top, because it always identifies the underlying error directly

    4. D

      Only on the function that was called first

    Correct answer
    A

    Near the bottom, where the actual error and its line are reported

    Explanation

    The bottom of the stack trace reports the actual error and the line where it occurred; the rest describes the execution path that led there.

  78. 78

    In the card-rank example, why did combining a numeric rank with a string cause a TypeError?

    1. A

      The random module cannot return numbers from a range

    2. B

      The function call used a parameter instead of an argument

    3. C

      A string cannot contain a suit and a rank together

    4. D

      Some rank cases returned integers while other cases returned strings

    Correct answer
    D

    Some rank cases returned integers while other cases returned strings

    Explanation

    The function returned strings for face cards but returned an integer for ordinary numeric ranks. Converting the numeric rank to a string made the return type consistent.

  79. 79

    How did isolated test cases help diagnose the racing game's disappearing cars?

    1. A

      They revealed that the display function failed when a car passed the track end or when both cars shared a position

    2. B

      They showed that car positions should be stored as strings instead of numbers

    3. C

      They established that the winner was calculated before the cars moved

    4. D

      They proved that the game loop was correct and the random numbers were invalid

    Correct answer
    A

    They revealed that the display function failed when a car passed the track end or when both cars shared a position

    Explanation

    Testing the display function independently exposed two problematic cases: a car beyond the track's end and both cars occupying the same position.

  80. 80

    How should the display loop's endpoint be chosen if a car can move beyond the nominal track length?

    1. A

      Use the shortest of the two car positions to avoid drawing empty spaces

    2. B

      Always stop at the track length, regardless of car positions

    3. C

      Use the first car's position and ignore the second car's position

    4. D

      Use the furthest of the track end and both car positions so every car can be drawn

    Correct answer
    D

    Use the furthest of the track end and both car positions so every car can be drawn

    Explanation

    To draw a car beyond the nominal track end, the loop must continue to the maximum of the track length and both car positions.

  81. 81

    What additional condition is needed before the display function draws an X for two cars on the same space?

    1. A

      The current space must be the end of the track

    2. B

      The current space, car one's position, and car two's position must all match

    3. C

      The two car positions must match, regardless of the current space

    4. D

      The current space must match car one's position, regardless of car two

    Correct answer
    B

    The current space, car one's position, and car two's position must all match

    Explanation

    Checking only whether the cars share a position would draw an X at every space. The current position must also equal that shared position.

Question 1 of 81
Question 1 of 81

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent