Modularity in Game Development
- 81Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Why is modularity useful when a program becomes very large?
Why is modularity useful when a program becomes very large?
81 questions
- 1
Why is modularity useful when a program becomes very large?
- A
It divides a complex program into smaller parts that are easier to navigate and debug
- B
It guarantees that every part of the program will fit in one file
- C
It eliminates the need to test the program after it is written
- D
It makes the computer execute every line of the program at the same time
Correct answer
AIt 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.
- A
- 2
In the role-playing game example, which sequence best illustrates progressively breaking down a problem?
- A
Identify activities such as farming, then actions such as planting and harvesting
- B
Write all farming code first, then decide which activities belong in the game
- C
Create a function for every line before deciding what the game should do
- D
Build the entire game, then divide it into files, then identify its features
Correct answer
AIdentify 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.
- A
- 3
Why is writing the harvest behavior separately easier than building all farming mechanics at once?
- A
A smaller task lets the programmer focus on one subproblem rather than juggling the entire system
- B
A smaller task automatically guarantees that the game will not contain bugs
- C
A smaller task ensures that no other component will need to interact with it
- D
A smaller task means the programmer can avoid deciding what the function should do
Correct answer
AA 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.
- A
- 4
How can modularity support collaboration between programmers?
- A
Programmers can avoid integrating their components because modules work without connections
- B
Each programmer must edit the same file so that all components remain identical
- C
One programmer must complete the entire program before the others can begin
- D
Each programmer can own a component or subtask and focus on it before the components are integrated
Correct answer
DEach 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.
- A
- 5
Why can testing small components increase confidence in the final program?
- A
A component test proves that every possible combination of components will work
- B
Testing components makes it unnecessary to check whether they work together
- C
Each component can be checked directly, and tested parts are less likely to conceal defects in the full program
- D
A component test automatically repairs any defects it finds
Correct answer
CEach 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.
- A
- 6
What is the main organizational benefit of grouping code by task?
- A
It removes the need for names that describe what the code does
- B
It prevents a program from needing multiple functions
- C
It ensures that all instructions are executed in alphabetical order
- D
It makes related instructions easier to locate than they would be in one giant block
Correct answer
DIt 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.
- A
- 7
Which description best captures the relationship between modules and functions in the described organization?
- A
Modules group related functionality, and functions within them perform specific tasks
- B
Modules contain only variables, while functions contain only comments
- C
Functions group related modules, and modules perform individual instructions
- D
Modules and functions are interchangeable names for a single code statement
Correct answer
AModules 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.
- A
- 8
Which set of benefits is associated with organizing code into modules and functions?
- A
Greater reusability, testability, and maintainability
- B
Removal of all repeated logic, all errors, and all dependencies
- C
Automatic collaboration, fewer requirements, and no integration work
- D
Guaranteed faster execution, fewer variables, and no need for debugging
Correct answer
AGreater 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.
- A
- 9
What is a function in the content's description?
- A
A condition that determines whether a program should stop
- B
A named block of code that performs a specific task
- C
A file that automatically executes every program in a project
- D
A value that can store any number of instructions without a name
Correct answer
BA 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.
- A
- 10
How does naming a set of instructions with a function help reduce repeated code?
- A
The function can be defined once and called repeatedly wherever its task is needed
- B
The function makes repeated tasks execute without being called
- C
The function copies its instructions into every possible location in the file
- D
The function converts all repeated instructions into a single variable value
Correct answer
AThe 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.
- A
- 11
What role do a function's input values play?
- A
They store the function's output in global scope automatically
- B
They replace the need for a function body
- C
They permanently determine the one value that the function can ever process
- D
They let the function perform a general task using different values on different calls
Correct answer
DThey 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.
- A
- 12
In a Python function definition, what is the function header?
- A
The indented block that contains the function's task instructions
- B
The first function call that supplies the input values
- C
The return statement that produces the output value
- D
The line beginning with def that gives the function name and parameters and ends with a colon
Correct answer
DThe 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.
- A
- 13
What are parameters in a function definition?
- A
Names for modules that contain the function
- B
Values supplied only after the function has finished running
- C
Names for input variables that are accessible within the function body
- D
Statements that cause the function to return to its caller
Correct answer
CNames 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.
- A
- 14
How does Python identify the function body?
- A
The body consists of every line in the file after the function definition
- B
The body consists only of the function's parameters and return value
- C
The body must be placed in a separate module from the function header
- D
The body consists of instructions indented beneath the function header
Correct answer
DThe 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.
- A
- 15
What does a return statement do in a function?
- A
It stores every local variable in global memory
- B
It supplies an output value and ends execution of that function call
- C
It repeats the function body until a condition becomes true
- D
It defines the function name and lists its input parameters
Correct answer
BIt 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.
- A
- 16
What are arguments in a function call?
- A
The output values stored in a module
- B
The indented instructions that make up the function body
- C
The variable names listed in the function definition
- D
The input values passed to the function when it is called
Correct answer
DThe 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.
- A
- 17
Why must a function definition appear before its function call in the described program?
- A
The function body is created only after the first argument is evaluated
- B
Python requires every function call to be at the end of a file
- C
The function call determines the function's name after it is executed
- D
The computer needs to learn the function's task before it can execute that task
Correct answer
DThe 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.
- A
- 18
When a function call supplies multiple arguments, how are they matched to parameters in the described model?
- A
In reverse order, with the last argument assigned to the first parameter
- B
By selecting whichever parameter has the broadest scope
- C
In order, with the first argument assigned to the first parameter
- D
By matching each argument's value to a parameter with the same name
Correct answer
CIn 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.
- A
- 19
What happens to a function call expression that returns a value?
- A
The expression always evaluates to the value zero
- B
The expression evaluates to the function's parameter names
- C
The expression creates a new module containing the function
- D
The expression evaluates to the value returned by the function
Correct answer
DThe 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.
- A
- 20
What happens if a function returns a value but the caller does not store, print, or otherwise use it?
- A
The function call is skipped because its result is unused
- B
The value is automatically saved as a global variable
- C
The function still computes the value, but the unused result is discarded
- D
The return value is reused as the next call's argument
Correct answer
CThe 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.
- A
- 21
When Python encounters a function definition during program execution, what does it do before a call occurs?
- A
It immediately prints the function's return value
- B
It creates every local variable that the function might later use
- C
It runs the function body once using default argument values
- D
It records the function's instructions and identifying information without executing the body
Correct answer
DIt 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.
- A
- 22
What is a stack frame in the function-execution model described?
- A
A shared area where all local and global variables are combined
- B
An isolated region of short-term memory for a particular function call
- C
A permanent file where all function definitions are stored
- D
A list of every module imported by a program
Correct answer
BAn 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.
- A
- 23
What happens to a function's stack frame when the function returns?
- A
It is copied into the module that originally defined the function
- B
It is kept permanently so that the next call can reuse all its local variables
- C
It is popped off, and its local memory is cleared while the return value goes back to the caller
- D
It is merged into the global scope, including every local variable
Correct answer
CIt 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.
- A
- 24
If a function changes a local variable that has the same name as a global variable, what happens to the global variable?
- A
The global variable remains unchanged because the function's local variable is separate
- B
Both variables are merged into one value in the module scope
- C
The local variable is ignored because global variables take priority
- D
The global variable is always overwritten when the function returns
Correct answer
AThe 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.
- A
- 25
What does variable scope describe?
- A
The order in which modules are imported
- B
The region of a program where a variable can be accessed
- C
The number of times a variable can be assigned
- D
The data type that a function must return
Correct answer
BThe 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.
- A
- 26
Where are names defined at the top level of a program stored in the described model?
- A
In global scope, where they can be accessed throughout the program
- B
Only in the module that imports the program
- C
In the local scope of the next function that is called
- D
In a separate stack frame that disappears after each statement
Correct answer
AIn 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.
- A
- 27
Where are variables assigned as part of a function call stored by default?
- A
In global scope alongside all top-level names
- B
In the local scope of that function call
- C
In the scope of every module in the program
- D
In permanent memory that remains after the program ends
Correct answer
BIn 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.
- A
- 28
How does Python search for a name used inside a function, according to the described scope model?
- A
It searches only the function's parameters and never checks enclosing scopes
- B
It checks the local scope first, then searches enclosing scopes such as global scope
- C
It checks global scope first, then searches local scope only if necessary
- D
It searches every module simultaneously and combines matching values
Correct answer
BIt 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.
- A
- 29
What happens if a name cannot be found in the local or any enclosing scope?
- A
Python silently substitutes the function's return value
- B
Python creates a global variable with a default value
- C
The name is automatically imported from the nearest module
- D
The program raises a name error when it tries to use the name
Correct answer
DThe 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.
- A
- 30
Why is using the global keyword inside functions generally discouraged in the content?
- A
It encourages functions to modify shared state and can create naming conflicts in large programs
- B
It makes all parameters inaccessible inside the function body
- C
It prevents functions from returning values to their callers
- D
It causes every function call to create a separate module
Correct answer
AIt 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.
- A
- 31
What is the preferred way for a function to communicate a calculated result back to its caller?
- A
Create a new local variable in the caller's stack frame directly
- B
Change a global variable with the same name as the result
- C
Print the result and assume the caller can use the printed text
- D
Return the result and let the caller store or use it
Correct answer
DReturn 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.
- A
- 32
Why is passing a needed value as a function parameter safer than relying on a global variable?
- A
It makes the caller provide the value explicitly instead of depending on hidden program state
- B
It guarantees that the function will never receive an incorrect value
- C
It makes the parameter available to every function in the program
- D
It lets the function modify the caller's local variables directly
Correct answer
AIt 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.
- A
- 33
What happens when one function calls another function before the first has returned?
- A
The outer function's stack frame is deleted before the inner function begins
- B
Both functions share one stack frame and overwrite each other's local variables
- C
The inner function is postponed until the entire program has terminated
- D
The inner call gets a new stack frame while the outer call's frame remains available beneath it
Correct answer
DThe 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.
- A
- 34
How does the computer manage several levels of nested function calls?
- A
It executes the deepest function first without recording where it was called
- B
It stores every call's variables in global scope until the program ends
- C
It replaces all earlier frames with the newest function's frame
- D
It adds a frame for each call and removes the top frame whenever that call returns
Correct answer
DIt 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.
- A
- 35
What is a key problem with putting movement, environment adjustment, and detection checking into one very large function?
- A
The function automatically executes its code in global scope
- B
The function becomes harder to understand, test, and reuse because it performs several different tasks
- C
The function is unable to return a Boolean value
- D
The function cannot accept more than one parameter
Correct answer
BThe 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.
- A
- 36
In the stealth-game example, what should a movement-volume function do?
- A
Take a detection result and return the number of guards
- B
Take an environment and return whether a guard detects the player
- C
Take a random threshold and return the player's movement type
- D
Take a base volume and movement type, then return the adjusted volume
Correct answer
DTake 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.
- A
- 37
What task does the environment-adjustment function perform in the example?
- A
It selects a random environment and changes the player's movement
- B
It takes a volume and environment type, then returns the adjusted volume
- C
It takes a movement type and returns the number of game levels
- D
It compares the player's volume to a random threshold and returns a string
Correct answer
BIt 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.
- A
- 38
Why separate the footstep-volume calculation from the detection-threshold check?
- A
Separating them ensures the player can never be detected
- B
The two tasks are independent, so each can be tested or reused without being tied to the other
- C
The volume calculation cannot accept an environment as an input
- D
The threshold check must be performed before any volume can be calculated
Correct answer
BThe 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.
- A
- 39
How can a wrapper function combine smaller functions to determine whether a player is detected?
- A
It can obtain the adjusted footstep volume from smaller functions and pass that volume to a detection function
- B
It can use only the movement type and ignore the environment and threshold
- C
It can return the names of the smaller functions without executing them
- D
It can copy all smaller functions into a global variable and skip their calls
Correct answer
AIt 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.
- A
- 40
Why are function names such as movement-volume and environment-adjustment functions helpful?
- A
Descriptive names cause the function to execute without a call
- B
Descriptive names guarantee that the implementation is correct
- C
Descriptive names document the task and can reduce the need for explanatory comments
- D
Descriptive names make parameters unnecessary
Correct answer
CDescriptive 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.
- A
- 41
How does decomposing a large function make its behavior easier to test?
- A
The functions no longer need input values, so tests are unnecessary
- B
Each smaller function has a focused responsibility with outputs that are easier to check
- C
Each smaller function automatically tests every possible input
- D
The combined program can be tested only after every feature is complete
Correct answer
BEach 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.
- A
- 42
How does decomposing functions help when adding voice volume to the stealth game?
- A
The program can omit environment effects because they apply only to footsteps
- B
The movement function can be renamed to make it handle voice automatically
- C
The entire detection system must be rewritten to avoid using footstep volume
- D
The environment-adjustment and detection functions can be reused for voice without rebuilding those tasks
Correct answer
DThe 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.
- A
- 43
What is a Python module in the described file-based organization?
- A
A stack frame containing the variables from one function call
- B
A section of a file that is identified only by its indentation
- C
A special kind of function that must return a Boolean
- D
A separate file with a .py extension that groups related code
Correct answer
DA 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.
- A
- 44
Why must a program import a module before using functions defined in that module?
- A
The import makes the module's names available to the program
- B
The import permanently saves the functions across program executions
- C
The import converts the module's functions into global variables
- D
The import runs the main program in every module automatically
Correct answer
AThe 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.
- A
- 45
How is a function from an imported module typically called in the described approach?
- A
By copying the function definition into every call site
- B
By placing the function name inside a return statement
- C
By using the module name as the function's argument
- D
By prefixing the function name with the module name
Correct answer
DBy 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.
- A
- 46
What is an entry point in a multi-file Python program?
- A
The single file chosen to begin execution and trigger the program's logic
- B
The file that contains every function used by the program
- C
The first function alphabetically in any imported module
- D
The last module to be imported before the program ends
Correct answer
AThe 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.
- A
- 47
What does importing a module do from the computer's execution perspective, according to the content?
- A
It automatically makes every name in the imported file global
- B
It permanently switches execution to the imported file and never returns
- C
It executes only the imported file's comments and function calls
- D
It executes the module's contents and then returns to the importing file
Correct answer
DIt 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.
- A
- 48
What is the effect of module scope on function names?
- A
Names are accessible only inside the function that defines them
- B
Names are automatically shared globally among every imported module
- C
Names are associated with the module where they are defined, so separate modules can use the same function name
- D
Names must be unique across all files in the entire program
Correct answer
CNames 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.
- A
- 49
Why can top-level print statements in an imported module cause confusion?
- A
They are converted into comments when the module is loaded
- B
They prevent the module from being imported more than once
- C
They run when the module is imported, potentially producing output from many modules
- D
They run only when a function in the module returns a value
Correct answer
CThey 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.
- A
- 50
Which is a practical benefit of putting related functions into a well-organized module?
- A
The module eliminates the need for a program entry point
- B
The module prevents any other file from importing its functions
- C
Programmers can find, reuse, and focus on a related set of functionality
- D
The module automatically verifies that every function is correct
Correct answer
CProgrammers 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.
- A
- 51
In the card-game example, what does the draw-hand function need as an input to support different games?
- A
The number of suits that exist in a playing card
- B
The number of cards to draw for the hand
- C
The name of the module that contains the deck
- D
The final string representation of every possible card
Correct answer
BThe 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.
- A
- 52
How does the draw-hand function build a hand in the described design?
- A
It selects one complete hand from a list of prewritten hands
- B
It draws all possible cards and removes the ones not requested
- C
It calls itself once for every suit and then ignores its parameter
- D
It loops the requested number of times, draws a card each iteration, and adds it to the hand
Correct answer
DIt 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.
- A
- 53
How can a newline character help display a hand of cards?
- A
It causes the card-drawing loop to begin a new iteration
- B
It converts every card rank into an integer
- C
It prevents a card from appearing more than once
- D
It makes Python display each card on a separate line
Correct answer
DIt 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.
- A
- 54
How is a playing card represented in the example, and how is a random card selected?
- A
As a string combining a randomly selected suit and a randomly selected rank
- B
As a Boolean that indicates whether the card is a face card
- C
As a function that selects a suit but never selects a rank
- D
As an integer selected from a range that represents the entire deck
Correct answer
AAs 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.
- A
- 55
How does the example select a suit with equal likelihood?
- A
It returns a fixed suit and randomizes only the rank
- B
It generates a number from one through thirteen and maps each to a suit
- C
It selects the first suit in a list and changes only when the caller asks
- D
It generates a random number from one through four and maps each outcome to a suit
Correct answer
DIt 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.
- A
- 56
Why should the rank function return a consistent data type?
- A
A consistent type guarantees that the random rank is unique
- B
Callers should be able to combine its results without encountering type mismatches
- C
The function cannot use conditionals if it returns different types
- D
Python functions can return only integers unless they are in a module
Correct answer
BCallers 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.
- A
- 57
How does a Boolean parameter support games that exclude face cards?
- A
The function always removes every card except aces when the parameter is true
- B
The parameter prevents the caller from choosing the hand size
- C
The parameter determines whether the suit is a string or an integer
- D
The caller chooses whether face cards are enabled, and disabled mode selects only ranks from two through ten
Correct answer
DThe 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.
- A
- 58
What limitation results from modeling card draws without maintaining deck state?
- A
The program cannot represent suits as strings
- B
The same card can be drawn more than once, even if a particular game requires one of each card
- C
The draw-hand function cannot support different hand sizes
- D
The caller cannot choose whether face cards are enabled
Correct answer
BThe 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.
- A
- 59
What is the central rule of Nim in the described version?
- A
Players remove any number of stones, and whoever removes none loses
- B
Players take turns removing one or two stones, and whoever removes the last stone loses
- C
Players take turns adding one or two stones, and whoever adds the last stone wins
- D
Players remove exactly one stone each turn, and the first player to stop wins
Correct answer
BPlayers 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.
- A
- 60
Why should the computer not take a turn after the user removes the last stone?
- A
The pile is automatically refilled before the computer's turn
- B
The game has already ended, so the computer should not receive another turn
- C
The user and computer must always take the same number of turns
- D
The computer is allowed to move only before the user takes a turn
Correct answer
BThe 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.
- A
- 61
How does a Boolean turn variable help alternate the players?
- A
It determines whether the input prompt should accept numbers or strings
- B
One value indicates the user's turn and the other indicates the computer's turn, and the value is switched each iteration
- C
It records the number of stones remaining and decreases after each move
- D
It stores the winner's name until the next game begins
Correct answer
BOne 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.
- A
- 62
Why can the program determine the winner after the game loop by examining the turn variable?
- A
The variable is set by the input function when the pile reaches zero
- 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
- C
The variable is never changed, so it always identifies the winner
- D
The variable stores the number of turns, which directly gives the winner's identity
Correct answer
BThe 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.
- A
- 63
Why should the user-pick function validate the user's input?
- A
To ensure that the pile begins with a random number of stones
- B
To ensure that the user always removes the last stone
- C
To ensure that the computer receives the same input value
- D
To ensure that it returns only an allowed choice of one or two stones
Correct answer
DTo 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.
- A
- 64
What is the purpose of a separate function that displays the stone pile?
- A
It isolates how the pile is drawn so its appearance can change without altering the main game logic
- B
It replaces the need to update the pile after each turn
- C
It validates the user's move before the game loop begins
- D
It decides which player wins without checking the pile
Correct answer
AIt 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.
- A
- 65
In the strategy described for the computer player, why is leaving four stones to the opponent desirable?
- A
Four stones allow the opponent to win immediately without making a move
- B
The computer can remove all four stones in one turn
- C
The opponent is forced into a position where either permitted move can leave the computer to take the last stone
- D
The game ends automatically when four stones remain
Correct answer
CThe 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.
- A
- 66
According to the partial algorithm described, which moves should the computer make for piles of five and six stones?
- A
Take two from both piles to leave three stones in each case
- B
Take one from five and two from six to leave four stones
- C
Take two from five and one from six to leave three stones
- D
Take one from both piles to leave four stones in each case
Correct answer
BTake 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.
- A
- 67
What is a unit test designed to test?
- A
Whether a program's user interface is visually appealing
- B
One unit of functionality at a time, commonly a single function
- C
Only the speed of the program under heavy load
- D
Every possible interaction across the entire finished program
Correct answer
BOne 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.
- A
- 68
What does a unit test assertion check?
- A
Whether the function has the same name as its module
- B
Whether the function's actual result matches the expected result for given inputs
- C
Whether every line of the program is printed to the console
- D
Whether the program uses a particular number of files
Correct answer
BWhether 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.
- A
- 69
How can automated unit tests help a programming team review changes?
- A
They allow a team to avoid specifying expected behavior
- B
They guarantee that no future test case will ever fail
- C
They automatically rewrite any function that fails a test
- D
They can run after changes and prevent submission until related tests pass
Correct answer
DThey 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.
- A
- 70
Why can a unit test be described as a living contract for a function?
- A
It replaces the need to communicate requirements to callers
- B
It prevents the function from ever changing its behavior
- C
It records expected behavior and can be updated when the requirements intentionally change
- D
It records every variable name used in the entire program
Correct answer
CIt 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.
- A
- 71
Why is one test case usually insufficient to establish that a function works?
- A
A function that always returns one fixed value could still pass that single test
- B
A test case cannot include an expected result
- C
A single test case always executes every conditional branch
- D
A single test case causes the function to run only once per program
Correct answer
AA 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.
- A
- 72
What does code coverage aim to achieve when selecting unit tests?
- A
Tests should use every variable name in the program
- B
Tests should execute every possible path through the function
- C
Tests should avoid conditional branches so results are predictable
- D
Tests should call every function exactly once with the same argument
Correct answer
BTests 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.
- A
- 73
How should programmers select representative test inputs for a function with conditional branches?
- A
Identify distinct input classes that lead to different branches and test each class
- B
Test only the smallest and largest values and ignore intermediate branches
- C
Choose many inputs from only the most common branch
- D
Choose inputs randomly without considering the function's conditions
Correct answer
AIdentify 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.
- A
- 74
What should a team do when it discovers a bug that was not caught by existing tests?
- A
Add a test for the failure case so the same bug is less likely to recur
- B
Change the function name so the old tests no longer apply
- C
Delete the existing tests because they did not catch the bug
- D
Avoid testing that input again to keep the test suite small
Correct answer
AAdd 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.
- A
- 75
Which statement correctly describes Python unit-testing tools mentioned in the content?
- A
The tools replace the need to define expected results
- B
Python requires every program to use both tools together
- C
Programmers commonly use the unittest module or the pytest module
- D
The tools test only performance and cannot test function outputs
Correct answer
CProgrammers commonly use the unittest module or the pytest module
Explanation
The content names unittest and pytest as commonly used Python unit-testing frameworks.
- A
- 76
For a beginner learning independently, what testing practice does the content still recommend even if automated unit tests are not worthwhile yet?
- A
Avoid testing functions until the entire program has been completed
- B
Change the function after every call, regardless of the result
- C
Write a separate test for every possible input value
- D
Call a function with several different arguments and check that it behaves as expected
Correct answer
DCall 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.
- A
- 77
When debugging a stack trace involving nested function calls, where does the content recommend focusing first?
- A
Near the bottom, where the actual error and its line are reported
- B
On the module name, because it contains the error message
- C
At the top, because it always identifies the underlying error directly
- D
Only on the function that was called first
Correct answer
ANear 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.
- A
- 78
In the card-rank example, why did combining a numeric rank with a string cause a TypeError?
- A
The random module cannot return numbers from a range
- B
The function call used a parameter instead of an argument
- C
A string cannot contain a suit and a rank together
- D
Some rank cases returned integers while other cases returned strings
Correct answer
DSome 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.
- A
- 79
How did isolated test cases help diagnose the racing game's disappearing cars?
- A
They revealed that the display function failed when a car passed the track end or when both cars shared a position
- B
They showed that car positions should be stored as strings instead of numbers
- C
They established that the winner was calculated before the cars moved
- D
They proved that the game loop was correct and the random numbers were invalid
Correct answer
AThey 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.
- A
- 80
How should the display loop's endpoint be chosen if a car can move beyond the nominal track length?
- A
Use the shortest of the two car positions to avoid drawing empty spaces
- B
Always stop at the track length, regardless of car positions
- C
Use the first car's position and ignore the second car's position
- D
Use the furthest of the track end and both car positions so every car can be drawn
Correct answer
DUse 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.
- A
- 81
What additional condition is needed before the display function draws an X for two cars on the same space?
- A
The current space must be the end of the track
- B
The current space, car one's position, and car two's position must all match
- C
The two car positions must match, regardless of the current space
- D
The current space must match car one's position, regardless of car two
Correct answer
BThe 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.
- A

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