Python Strings, Input, and Decisions
- 67Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What does an object's type tell Python?
What does an object's type tell Python?
67 questions
- 1
What does an object's type tell Python?
- A
The number of variables that can refer to it
- B
The kinds of operations that are allowed on it
- C
Its value and the variable name that refers to it
- D
The location where its source code was written
Correct answer
BThe kinds of operations that are allowed on it
Explanation
Every Python object has a type, and its type determines which operations are allowed for it.
- A
- 2
What does assigning an object to a variable do?
- A
It copies the object's value into the variable permanently
- B
It changes the object's type to match the variable
- C
It binds a name to the object in memory
- D
It makes the variable an operation on the object
Correct answer
CIt binds a name to the object in memory
Explanation
Assigning an object to a variable binds a name to that object in memory; the variable is a name referring to the object.
- A
- 3
Given pi and radius as variables, what happens when Python evaluates
area = pi * radius**2?- A
It binds area to the expression without evaluating it
- B
It changes the values of pi and radius to their product
- C
It evaluates the right-hand expression and binds its result to area
- D
It treats the equals sign as a mathematical equality test
Correct answer
CIt evaluates the right-hand expression and binds its result to area
Explanation
Python evaluates the right-hand expression first, using the values bound to pi and radius, and assigns the result to area.
- A
- 4
If
radiusis initially bound to 2.2, what happens afterradius = radius + 1?- A
Python creates the result 3.2 and binds radius to it
- B
Python reports an error because a variable cannot be reassigned
- C
Python changes every object in memory that has value 2.2
- D
The object 2.2 is modified so its value becomes 3.2
Correct answer
APython creates the result 3.2 and binds radius to it
Explanation
In an assignment, Python evaluates the right-hand side and then binds the left-hand name to that result. The name radius is rebound to a new value, rather than the original object being changed.
- A
- 5
What value is bound to
varaftervar = type(5 * 4)?- A
var is bound to the string
"int" - B
var is bound to the integer 20
- C
var is bound to the expression
5 * 4 - D
var is bound to the type
int
Correct answer
Dvar is bound to the type
intExplanation
The expression
5 * 4evaluates to the integer 20, andtype(20)evaluates to the typeint; therefore var is bound to that type object. - A
- 6
How does a single equals sign function in a Python assignment?
- A
It assigns the evaluated right-hand value to the left-hand name
- B
It asks Python whether the variable equals the expression
- C
It creates an equation that Python solves
- D
It asserts that the expression must already have the variable's value
Correct answer
AIt assigns the evaluated right-hand value to the left-hand name
Explanation
In an assignment statement, the equals sign means assignment, not mathematical equality. Python evaluates the right-hand side and binds its result to the name on the left.
- A
- 7
Which description of Python expressions is accurate?
- A
Expressions can use operators or operations and evaluate to values
- B
Expressions always change the objects they use
- C
Expressions must contain only numbers and arithmetic operators
- D
Expressions cannot be used on the right side of assignments
Correct answer
AExpressions can use operators or operations and evaluate to values
Explanation
Expressions can combine objects using operators, and they can also use operations such as
type(...)that evaluate to an object. - A
- 8
Which kinds of objects can Python store in memory?
- A
Only objects assigned to names can exist in memory
- B
Every object in memory must be an integer or float
- C
Objects of many different types can be stored in memory
- D
Only numeric objects can be stored in memory
Correct answer
CObjects of many different types can be stored in memory
Explanation
Python can store objects of many types in memory, not only numbers; strings and type objects are examples discussed here.
- A
- 9
What is a Python string, as described in the lecture?
- A
A sequence of lowercase letters only
- B
A single character that must be a letter
- C
A sequence of numbers that ignores letter case
- D
A sequence of case-sensitive characters
Correct answer
DA sequence of case-sensitive characters
Explanation
A string is a sequence of characters, and it is case sensitive, so uppercase and lowercase letters are distinct characters.
- A
- 10
How do you indicate that a sequence of characters should be a string literal?
- A
Prefix the characters with the keyword
str - B
Enclose the characters in quotation marks
- C
Separate each character with a comma
- D
Place the characters inside square brackets
Correct answer
BEnclose the characters in quotation marks
Explanation
Quotation marks tell Python that the enclosed characters form a string object.
- A
- 11
Which characters can be included in a Python string?
- A
Letters and digits, but no newline or tab characters
- B
Any characters, including special characters such as newline and tab
- C
Only printable punctuation and digits
- D
Only letters and spaces
Correct answer
BAny characters, including special characters such as newline and tab
Explanation
Strings can contain letters, digits, special keyboard characters, and characters representing things such as tabs and newlines.
- A
- 12
What does the
+operator do between two strings?- A
It repeats the first string once for each character in the second
- B
It joins the strings without adding an unrequested space
- C
It joins the strings and always inserts a space
- D
It compares the strings and returns a Boolean
Correct answer
BIt joins the strings without adding an unrequested space
Explanation
For strings,
+concatenates the characters into a new string. It does not insert a space unless a space is included explicitly. - A
- 13
How can a program concatenate
"me"and"myself"with a space between them?- A
Convert both strings to numbers before concatenating them
- B
Rely on Python to insert a space between every pair of strings
- C
Include a space character in one of the strings being concatenated
- D
Use
+twice, because the second plus adds a space
Correct answer
CInclude a space character in one of the strings being concatenated
Explanation
To include a space in the concatenated result, one of the strings must contain a space character, such as
" ". - A
- 14
What does
"me" * 3produce?- A
It repeats the string's characters the specified number of times
- B
It returns the string's length multiplied by the integer
- C
It multiplies each character by the integer numerically
- D
It inserts that many spaces between each character
Correct answer
AIt repeats the string's characters the specified number of times
Explanation
A string multiplied by an integer repeats its sequence of characters that many times, producing a string.
- A
- 15
What happens if a program tries to multiply a string by the string
"3"?- A
Python automatically treats any string containing digits as an integer
- B
Python raises a type error because the repetition count is not an integer
- C
The multiplication returns an empty string
- D
The multiplication works, but returns the string
"333"
Correct answer
BPython raises a type error because the repetition count is not an integer
Explanation
String repetition requires an integer count. A string such as
"3"is not an integer until it is converted withint(...). - A
- 16
What does
len("abc")evaluate to?- A
It returns the number of characters in the string
- B
It returns the type of the string
- C
It returns the string's first character
- D
It returns the last index of the string
Correct answer
AIt returns the number of characters in the string
Explanation
The
lenoperation returns the number of characters in a string. Since"abc"has three characters, its length is 3. - A
- 17
What does
int("3")produce?- A
It becomes the Boolean value True
- B
It becomes the float 3.0
- C
It remains the string
"3" - D
It becomes the integer 3
Correct answer
DIt becomes the integer 3
Explanation
The input is a string containing one character. Converting it with
intproduces the integer 3, which can then be used in arithmetic. - A
- 18
If a program wants to change the first character of the string
"car"to make"bar", what must it do?- A
Use
lento convert the string into a mutable object - B
Create a new string and optionally rebind the variable
- C
Assign a new character directly to the string's index
- D
Change the character in memory while keeping the same string object
Correct answer
BCreate a new string and optionally rebind the variable
Explanation
A string is immutable: its characters cannot be changed in place. To obtain a different string, create a new string and, if desired, rebind a variable to it.
- A
- 19
What is the index of the first character in a Python string?
- A
The first character is at index equal to the string's length
- B
The first character is at index -1
- C
The first character is at index 0
- D
The first character is at index 1
Correct answer
CThe first character is at index 0
Explanation
The index of the first character is 0, the second is 1, and the third is 2.
- A
- 20
If
s = "abc", what doess[1]evaluate to?- A
a - B
b - C
c - D
An empty string
Correct answer
BbExplanation
With
s = "abc", index 1 refers to the second character,b. - A
- 21
What happens when a program evaluates
"abc"[3]?- A
It raises an IndexError
- B
It returns the empty string
- C
It returns the string's length, 3
- D
It returns the last character,
c
Correct answer
AIt raises an IndexError
Explanation
The valid nonnegative indices for a three-character string are 0, 1, and 2. Index 3 is beyond the last character and raises an IndexError.
- A
- 22
What does index
-1identify in a nonempty string?- A
It returns the last character
- B
It returns the first character
- C
It raises an IndexError for every string
- D
It returns the character before the last one
Correct answer
AIt returns the last character
Explanation
A negative index counts from the right; index -1 refers to the last character of the string.
- A
- 23
If
s = "abc", what doess[len(s) - 1]evaluate to?- A
It returns
abecause indexing starts from zero - B
It raises an IndexError because length is not an index
- C
It returns the empty string because the stop is omitted
- D
It returns
c, the last character
Correct answer
DIt returns
c, the last characterExplanation
For
"abc",len(s) - 1is 2, which is the index of the last character,c. - A
- 24
What does
"abcdefgh"[3:6]return?- A
def - B
defg - C
cdef - D
ef
Correct answer
AdefExplanation
In a slice, the start index is included and the stop index is excluded. Indices 3, 4, and 5 in
"abcdefgh"containd,e, andf. - A
- 25
What does
"abcdefgh"[3:6:2]return?- A
de - B
ef - C
dfg - D
df
Correct answer
DdfExplanation
A positive step of 2 takes every other character, starting at index 3 (
d), then index 5 (f), and stops before index 6. - A
- 26
What is the default step for a string slice such as
s[2:5]?- A
It takes every second character by default
- B
It takes each character from the start up to, but not including, the stop
- C
It includes the stop character but skips the start character
- D
It returns a single character at the start index
Correct answer
BIt takes each character from the start up to, but not including, the stop
Explanation
When a slice's step is omitted, it defaults to 1, so the slice takes each character from the start up to, but excluding, the stop index.
- A
- 27
What does
s[:]do for a strings?- A
It returns an empty string
- B
It returns the string in reverse order
- C
It returns only the first character
- D
It returns the full string
Correct answer
DIt returns the full string
Explanation
The slice
[:]omits the start, stop, and step, so it selects the full string in its original order. - A
- 28
What does
s[::-1]do?- A
It returns every other character from left to right
- B
It raises an IndexError because the step is negative
- C
It returns the string in reverse order
- D
It returns only the last character
Correct answer
CIt returns the string in reverse order
Explanation
A step of -1 moves through the string from right to left, producing the reversed character sequence.
- A
- 29
What does
"abcdefgh"[4:1:-2]return?- A
edc - B
ec - C
ce - D
ecb
Correct answer
BecExplanation
A negative step traverses from right to left. Starting at index 4 selects
e, then stepping by -2 selects index 2,c; the stop at index 1 is excluded. - A
- 30
What does
"abcdef"[5:2]return?- A
An empty string
- B
An IndexError
- C
The character at index 6 only
- D
The characters from index 3 through index 6
Correct answer
AAn empty string
Explanation
The slice starts at index 6 and uses the default positive step, but its stop is 3, which lies behind the start in that direction. No characters are selected, so the result is an empty string.
- A
- 31
In a Python slice
s[start:stop], how is the stop index treated?- A
It starts at index 4 because counting starts at 1
- B
It stops before the character at index 10
- C
It includes the character at index 10
- D
It raises an error if the stop equals the last index
Correct answer
BIt stops before the character at index 10
Explanation
Slicing includes the start index but excludes the stop index. Thus the slice from index 3 up to index 10 includes characters at indices 3 through 9.
- A
- 32
How does evaluating an expression in the interactive shell differ from placing it alone in a program file?
- A
The shell displays expression values automatically; a file requires explicit output
- B
Expressions print their values in files but not in the shell
- C
A file prints expressions only when they contain strings
- D
The shell and files both automatically print every expression
Correct answer
AThe shell displays expression values automatically; a file requires explicit output
Explanation
Typing an expression in the interactive shell displays its value, but an expression by itself in a program file does not automatically display that value to the user.
- A
- 33
How can a Python program explicitly display an expression's result to the user?
- A
Enclose the expression in quotation marks
- B
Place the expression on the last line of the file
- C
Wrap the expression in
print(...) - D
Assign the expression to a variable
Correct answer
CWrap the expression in
print(...)Explanation
A program must explicitly call
printto display a result to the user when it is run from a file. - A
- 34
What does Python normally do after a
printstatement displays its result?- A
Each call displays only the type of its result
- B
Each call waits for the user to press Enter before printing
- C
Each call appends its result to the previous line without a separator
- D
Each call prints its result and then starts a new line
Correct answer
DEach call prints its result and then starts a new line
Explanation
Each
printcall displays its result and then ends the line, so two separate calls normally produce output on separate lines. - A
- 35
What happens when multiple objects are passed to
printseparated by commas?- A
It prints them on one line with spaces between the objects
- B
It concatenates the objects with no separator and converts all to strings first
- C
It raises a type error if the objects have different types
- D
It prints each object on a separate line
Correct answer
AIt prints them on one line with spaces between the objects
Explanation
When multiple objects are passed to one
printcall separated by commas, Python displays them on the same line with spaces between them by default. - A
- 36
How can a program concatenate an integer with strings using
+?- A
Use
lento make the integer compatible with strings - B
Python automatically converts the integer to a string during concatenation
- C
Convert the other strings to integers
- D
Convert the integer to a string before concatenating
Correct answer
DConvert the integer to a string before concatenating
Explanation
Concatenation requires strings. Converting a non-string object such as the integer 3 with
str(...)allows it to be concatenated with other strings. - A
- 37
What is the role of the string passed to
input(...)?- A
It converts the prompt into a variable name
- B
It prints the prompt after the user has entered a response
- C
It displays the prompt and waits for user input
- D
It prints the prompt and immediately returns an empty string
Correct answer
CIt displays the prompt and waits for user input
Explanation
The string inside
input(...)is displayed as a prompt, and the program then waits for the user to type a response and press Enter. - A
- 38
What type of value does
inputreturn?- A
The user's entry is returned as a float if it contains a decimal point
- B
The user's entry is returned as a string
- C
The user's entry is automatically converted to an integer if it contains digits
- D
The user's entry is returned as a Boolean
Correct answer
BThe user's entry is returned as a string
Explanation
inputreturns the user's entry as a string, even when the entry consists of digits. - A
- 39
If a user enters
3, what doesprint(5 * input(...))display?- A
The string
"33333" - B
15
- C
A type error
- D
The integer 33333
Correct answer
AThe string
"33333"Explanation
If the user types
3,inputreturns the string"3". Multiplying a string by 5 repeats its characters, producing"33333". - A
- 40
If the user enters
3, what doesprint(5 * int(input(...)))display?- A
The string
"33333" - B
A type error because input is always a string
- C
The string
"15" - D
The integer 15
Correct answer
DThe integer 15
Explanation
Casting the input result with
intconverts the digit string"3"to the integer 3, so multiplying by 5 performs arithmetic and gives 15. - A
- 41
How can a program print a sentence that includes a verb entered by the user?
- A
Multiply the input value by the number of words in the sentence
- B
Put the variable name inside quotation marks so Python substitutes its value
- C
Print the literal text and the input value as separate objects
- D
Use
inputinside the sentence's quotation marks
Correct answer
CPrint the literal text and the input value as separate objects
Explanation
To build a sentence using a user's verb, pass the literal text and the input variable as separate objects to
print, or otherwise convert and combine them appropriately. - A
- 42
Which relationship describes the Newton-method next-guess calculation presented in the lecture?
- A
Divide the current guess by the sum of the function and derivative values
- B
Subtract the derivative value at the current guess from the function value
- C
Add the derivative at the current guess divided by the function value to the current guess
- D
Subtract the function value at the current guess divided by the derivative at that guess from the current guess
Correct answer
DSubtract the function value at the current guess divided by the derivative at that guess from the current guess
Explanation
A Newton-method update uses the current guess and the function and derivative evaluated at that guess: next guess equals current guess minus the function value divided by the derivative value.
- A
- 43
For the cube-root example, how are the function and its derivative supplied to the program?
- A
The function is 3g2 and the derivative is g3−x
- B
The derivative is always set equal to the current guess
- C
The program automatically infers the derivative from the function
- D
The program explicitly uses g3−x and 3g2 for the function and derivative
Correct answer
DThe program explicitly uses g3−x and 3g2 for the function and derivative
Explanation
For finding the cube root of x, the function used is g3−x and its derivative is 3g2. In this introductory program, both formulas are written explicitly.
- A
- 44
What does the Newton-method program in the example do after calculating the next guess?
- A
It keeps calculating guesses until the exact root is found
- B
It calculates one next guess and stops
- C
It prints only the initial guess and never calculates another
- D
It calculates the derivative automatically and repeats twice
Correct answer
BIt calculates one next guess and stops
Explanation
The demonstrated program calculates one next guess from the initial guess. It does not repeat the calculation because looping has not yet been introduced.
- A
- 45
How does an f-string combine literal text with calculated values?
- A
It converts every character inside the string to a variable
- B
It evaluates only the text outside curly braces
- C
It evaluates expressions inside curly braces and prints other text literally
- D
It uses commas to combine objects and inserts spaces automatically
Correct answer
CIt evaluates expressions inside curly braces and prints other text literally
Explanation
An f-string begins with
fbefore the quoted string. Expressions in curly braces are evaluated, while text outside the braces is printed literally. - A
- 46
What is a practical advantage of using an f-string to print a number beside a percent sign?
- A
They can insert evaluated expressions without manual casting and concatenation
- B
They can contain only variable names, not other expressions
- C
They automatically add a space before every punctuation mark
- D
They prevent expressions from being evaluated until after printing
Correct answer
AThey can insert evaluated expressions without manual casting and concatenation
Explanation
F-strings insert the evaluated result of an expression directly, avoiding the need to cast values to strings and concatenate them manually.
- A
- 47
Which statement summarizes where Python expressions can be used?
- A
Expressions can be placed in many contexts and are evaluated as needed
- B
Expressions are evaluated only when they are printed
- C
Expressions can appear only on the right side of assignments
- D
Expressions can appear only inside arithmetic statements
Correct answer
AExpressions can be placed in many contexts and are evaluated as needed
Explanation
Python can evaluate expressions in various contexts, including indexes, print calls, and f-string braces, before continuing with the program.
- A
- 48
What does
==mean in Python?- A
It assigns the right-hand value to the left-hand variable
- B
It tests equivalence and produces True or False
- C
It converts both values to the same type
- D
It checks whether the left value is greater than the right
Correct answer
BIt tests equivalence and produces True or False
Explanation
A double equals sign tests whether two values are equivalent and produces a Boolean result. A single equals sign performs assignment.
- A
- 49
What does the expression
2 < 3evaluate to?- A
The string
"True" - B
The integer 1
- C
The Boolean value
False - D
The Boolean value
True
Correct answer
DThe Boolean value
TrueExplanation
The expression
2 < 3is true, so it evaluates to the Boolean valueTrue. - A
- 50
What does the Python operator
!=test?- A
It tests whether two values have different types only
- B
It tests whether the left value is smaller
- C
It tests whether two values are different
- D
It assigns one value to another
Correct answer
CIt tests whether two values are different
Explanation
The
!=operator tests whether two values are not equivalent; it returns True when they differ and False when they match. - A
- 51
What is the result of comparing the string
"a"with the string"A"using==?- A
It is True only when both strings have the same length
- B
It is True because both are the letter a
- C
It raises an error because case cannot be compared
- D
It is False because lowercase and uppercase letters differ
Correct answer
DIt is False because lowercase and uppercase letters differ
Explanation
Because Python strings are case sensitive, lowercase
aand uppercaseAare distinct characters and are not equal. - A
- 52
What does
not (2 < 3)evaluate to?- A
An error, because
notapplies only to variables - B
True, because
notrepeats the comparison - C
The integer 0, rather than a Boolean
- D
False, because
notreverses True
Correct answer
DFalse, because
notreverses TrueExplanation
The
notoperator reverses a Boolean value. Since2 < 3is True,not (2 < 3)is False. - A
- 53
When does a Boolean expression joined by
andevaluate to True?- A
It is True only when both expressions are True
- B
It is True whenever at least one expression is True
- C
It returns the value of the first expression
- D
It is False only when both expressions are False
Correct answer
AIt is True only when both expressions are True
Explanation
An
andexpression is True only when both component Boolean expressions are True. If either one is False, the result is False. - A
- 54
When does a Boolean expression joined by
orevaluate to False?- A
It is False whenever either expression is False
- B
It is True when at least one expression is True
- C
It returns the value of the second expression
- D
It is True only when both expressions are True
Correct answer
BIt is True when at least one expression is True
Explanation
An
orexpression is False only when both component expressions are False; if either is True, the result is True. - A
- 55
Why are Boolean expressions useful in programs?
- A
Boolean expressions can be used only as print arguments
- B
Boolean expressions can be combined and used to guide program behavior
- C
Comparisons always produce numeric values
- D
Boolean expressions cannot be stored in variables
Correct answer
BBoolean expressions can be combined and used to guide program behavior
Explanation
A Boolean expression can combine comparisons, and the result can be used in a condition or stored in a variable.
- A
- 56
How does an
if/elsestructure choose which block to execute?- A
Neither block runs unless both conditions are True
- B
The
ifblock runs if the condition is True; otherwise, theelseblock runs - C
The
elseblock runs only when theifcondition is True - D
Both blocks run, in order, regardless of the condition
Correct answer
BThe
ifblock runs if the condition is True; otherwise, theelseblock runsExplanation
With
ifandelse, Python runs the indentedifblock when the condition is True and theelseblock otherwise; the two alternatives are not both executed. - A
- 57
How does Python determine which statements belong to an
ifblock?- A
By indentation beneath the
ifstatement - B
By quotation marks around the commands
- C
By placing the commands inside parentheses
- D
By the number of statements before the next variable assignment
Correct answer
ABy indentation beneath the
ifstatementExplanation
An
ifblock's commands are identified by indentation. Commands indented under theifbelong to that block. - A
- 58
What happens when an
ifcondition is False and there is noelseblock?- A
It executes the block anyway, then repeats the condition
- B
It stops the entire program
- C
It skips the indented block and continues with the rest of the program
- D
It treats the condition as an assignment
Correct answer
CIt skips the indented block and continues with the rest of the program
Explanation
If an
ifcondition is False and there is noelse, Python skips that block and continues with the following statements outside it. - A
- 59
What is the purpose of
elifin a conditional chain?- A
It runs only if the first
ifcondition is True - B
It always runs after the
ifblock - C
It checks every branch and executes all true ones
- D
It checks another condition if earlier conditions were False
Correct answer
DIt checks another condition if earlier conditions were False
Explanation
An
elifchecks another condition when the preceding condition was False. In an if/elif chain, the first true branch is executed and later branches are skipped. - A
- 60
When does the final
elseblock in an if/elif/else chain run?- A
It runs whenever any preceding condition is True
- B
It repeats the last
elifcondition - C
It runs if none of the preceding conditions is True
- D
It makes every branch in the chain execute
Correct answer
CIt runs if none of the preceding conditions is True
Explanation
An
elseat the end of a conditional chain acts as a catch-all: it runs only when none of the precedingiforelifconditions are True. - A
- 61
If multiple conditions in an if/elif chain are True, which branch executes?
- A
Only the first true branch runs
- B
Python chooses a true branch at random
- C
Every true branch runs
- D
The last true branch runs
Correct answer
AOnly the first true branch runs
Explanation
An if/elif chain selects the first condition that is True, so even if several conditions are True, only the earliest matching branch runs.
- A
- 62
What can happen when a program uses two separate
ifstatements?- A
Only the first block can run because all
ifstatements form one chain - B
Python reports an error when two
ifconditions are True - C
The second block runs only if the first condition is False
- D
Both blocks can run if both conditions are True
Correct answer
DBoth blocks can run if both conditions are True
Explanation
Separate
ifstatements are independent. If each condition is True, each corresponding block can execute; unlikeelif, they are not alternatives in one chain. - A
- 63
Why can incorrect indentation cause a program to print a statement even when an
ifcondition is False?- A
Statements at the outer level run only if the condition is True
- B
Every statement after an
ifbelongs to its block automatically - C
Indentation determines whether variables are converted to Booleans
- D
Indentation determines whether a statement belongs to a conditional block
Correct answer
DIndentation determines whether a statement belongs to a conditional block
Explanation
If a statement is indented inside an
ifblock, it runs only when that condition is True. A statement at the outer indentation level runs after the conditional regardless of that condition. - A
- 64
What is a nested conditional?
- A
They prevent code from rejoining the rest of the program
- B
They make the inner condition run before the outer condition
- C
They allow a conditional block to contain another conditional block
- D
They force all conditions to be true at once
Correct answer
CThey allow a conditional block to contain another conditional block
Explanation
Nested conditionals place one decision block inside another. The inner condition is considered only when execution reaches its enclosing block.
- A
- 65
Which conditional structure correctly classifies a guess as too high, equal, or too low?
- A
Use
>for too high, an equalityelif, andelsefor too low - B
Use three separate print statements without conditions
- C
Use
==for too high,<for equal, andelsefor too low - D
Use
>=for too high, then an equalityelif, then a lowelse
Correct answer
AUse
>for too high, an equalityelif, andelsefor too lowExplanation
For a guess that must be classified as too high, equal, or too low,
if/elif/elseselects one appropriate outcome. Using>rather than>=allows an equal guess to reach the equality case. - A
- 66
Why might a program use
elserather than anothereliffor the final guess outcome?- A
An
elseis required to contain a new Boolean condition - B
An
elifcannot be used for the final possible case - C
An
elseexecutes before the precedingif - D
An
elsecan handle the remaining case when earlier branches cover the other possibilities
Correct answer
DAn
elsecan handle the remaining case when earlier branches cover the other possibilitiesExplanation
A catch-all
elseis convenient when the remaining possibilities are already covered by earlier branches; an explicitelifcan also test the remaining case. - A
- 67
Why is it useful to test code early with different inputs?
- A
It converts conditional branches into separate functions
- B
It is useful only after the complete program has been written
- C
It guarantees the program has no errors without inspecting its output
- D
It helps reveal bugs early by checking behavior in different cases
Correct answer
DIt helps reveal bugs early by checking behavior in different cases
Explanation
Testing a program with different inputs can expose mistakes in its conditions, such as accidentally classifying an equal guess as too high.
- A

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