1. Home
  2. Explore
  3. Computer Science
  4. Python Strings, Input, and Decisions

Study methods

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

Python Strings, Input, and Decisions

  • 67Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Objects and Assignment

What does an object's type tell Python?

67 questions

  1. 1

    What does an object's type tell Python?

    1. A

      The number of variables that can refer to it

    2. B

      The kinds of operations that are allowed on it

    3. C

      Its value and the variable name that refers to it

    4. D

      The location where its source code was written

    Correct answer
    B

    The 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.

  2. 2

    What does assigning an object to a variable do?

    1. A

      It copies the object's value into the variable permanently

    2. B

      It changes the object's type to match the variable

    3. C

      It binds a name to the object in memory

    4. D

      It makes the variable an operation on the object

    Correct answer
    C

    It 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.

  3. 3

    Given pi and radius as variables, what happens when Python evaluates area = pi * radius**2?

    1. A

      It binds area to the expression without evaluating it

    2. B

      It changes the values of pi and radius to their product

    3. C

      It evaluates the right-hand expression and binds its result to area

    4. D

      It treats the equals sign as a mathematical equality test

    Correct answer
    C

    It 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.

  4. 4

    If radius is initially bound to 2.2, what happens after radius = radius + 1?

    1. A

      Python creates the result 3.2 and binds radius to it

    2. B

      Python reports an error because a variable cannot be reassigned

    3. C

      Python changes every object in memory that has value 2.2

    4. D

      The object 2.2 is modified so its value becomes 3.2

    Correct answer
    A

    Python 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.

  5. 5

    What value is bound to var after var = type(5 * 4)?

    1. A

      var is bound to the string "int"

    2. B

      var is bound to the integer 20

    3. C

      var is bound to the expression 5 * 4

    4. D

      var is bound to the type int

    Correct answer
    D

    var is bound to the type int

    Explanation

    The expression 5 * 4 evaluates to the integer 20, and type(20) evaluates to the type int; therefore var is bound to that type object.

  6. 6

    How does a single equals sign function in a Python assignment?

    1. A

      It assigns the evaluated right-hand value to the left-hand name

    2. B

      It asks Python whether the variable equals the expression

    3. C

      It creates an equation that Python solves

    4. D

      It asserts that the expression must already have the variable's value

    Correct answer
    A

    It 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.

  7. 7

    Which description of Python expressions is accurate?

    1. A

      Expressions can use operators or operations and evaluate to values

    2. B

      Expressions always change the objects they use

    3. C

      Expressions must contain only numbers and arithmetic operators

    4. D

      Expressions cannot be used on the right side of assignments

    Correct answer
    A

    Expressions 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.

  8. 8

    Which kinds of objects can Python store in memory?

    1. A

      Only objects assigned to names can exist in memory

    2. B

      Every object in memory must be an integer or float

    3. C

      Objects of many different types can be stored in memory

    4. D

      Only numeric objects can be stored in memory

    Correct answer
    C

    Objects 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.

  9. 9

    What is a Python string, as described in the lecture?

    1. A

      A sequence of lowercase letters only

    2. B

      A single character that must be a letter

    3. C

      A sequence of numbers that ignores letter case

    4. D

      A sequence of case-sensitive characters

    Correct answer
    D

    A 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.

  10. 10

    How do you indicate that a sequence of characters should be a string literal?

    1. A

      Prefix the characters with the keyword str

    2. B

      Enclose the characters in quotation marks

    3. C

      Separate each character with a comma

    4. D

      Place the characters inside square brackets

    Correct answer
    B

    Enclose the characters in quotation marks

    Explanation

    Quotation marks tell Python that the enclosed characters form a string object.

  11. 11

    Which characters can be included in a Python string?

    1. A

      Letters and digits, but no newline or tab characters

    2. B

      Any characters, including special characters such as newline and tab

    3. C

      Only printable punctuation and digits

    4. D

      Only letters and spaces

    Correct answer
    B

    Any 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.

  12. 12

    What does the + operator do between two strings?

    1. A

      It repeats the first string once for each character in the second

    2. B

      It joins the strings without adding an unrequested space

    3. C

      It joins the strings and always inserts a space

    4. D

      It compares the strings and returns a Boolean

    Correct answer
    B

    It 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.

  13. 13

    How can a program concatenate "me" and "myself" with a space between them?

    1. A

      Convert both strings to numbers before concatenating them

    2. B

      Rely on Python to insert a space between every pair of strings

    3. C

      Include a space character in one of the strings being concatenated

    4. D

      Use + twice, because the second plus adds a space

    Correct answer
    C

    Include 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 " ".

  14. 14

    What does "me" * 3 produce?

    1. A

      It repeats the string's characters the specified number of times

    2. B

      It returns the string's length multiplied by the integer

    3. C

      It multiplies each character by the integer numerically

    4. D

      It inserts that many spaces between each character

    Correct answer
    A

    It 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.

  15. 15

    What happens if a program tries to multiply a string by the string "3"?

    1. A

      Python automatically treats any string containing digits as an integer

    2. B

      Python raises a type error because the repetition count is not an integer

    3. C

      The multiplication returns an empty string

    4. D

      The multiplication works, but returns the string "333"

    Correct answer
    B

    Python 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 with int(...).

  16. 16

    What does len("abc") evaluate to?

    1. A

      It returns the number of characters in the string

    2. B

      It returns the type of the string

    3. C

      It returns the string's first character

    4. D

      It returns the last index of the string

    Correct answer
    A

    It returns the number of characters in the string

    Explanation

    The len operation returns the number of characters in a string. Since "abc" has three characters, its length is 3.

  17. 17

    What does int("3") produce?

    1. A

      It becomes the Boolean value True

    2. B

      It becomes the float 3.0

    3. C

      It remains the string "3"

    4. D

      It becomes the integer 3

    Correct answer
    D

    It becomes the integer 3

    Explanation

    The input is a string containing one character. Converting it with int produces the integer 3, which can then be used in arithmetic.

  18. 18

    If a program wants to change the first character of the string "car" to make "bar", what must it do?

    1. A

      Use len to convert the string into a mutable object

    2. B

      Create a new string and optionally rebind the variable

    3. C

      Assign a new character directly to the string's index

    4. D

      Change the character in memory while keeping the same string object

    Correct answer
    B

    Create 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.

  19. 19

    What is the index of the first character in a Python string?

    1. A

      The first character is at index equal to the string's length

    2. B

      The first character is at index -1

    3. C

      The first character is at index 0

    4. D

      The first character is at index 1

    Correct answer
    C

    The first character is at index 0

    Explanation

    The index of the first character is 0, the second is 1, and the third is 2.

  20. 20

    If s = "abc", what does s[1] evaluate to?

    1. A

      a

    2. B

      b

    3. C

      c

    4. D

      An empty string

    Correct answer
    B

    b

    Explanation

    With s = "abc", index 1 refers to the second character, b.

  21. 21

    What happens when a program evaluates "abc"[3]?

    1. A

      It raises an IndexError

    2. B

      It returns the empty string

    3. C

      It returns the string's length, 3

    4. D

      It returns the last character, c

    Correct answer
    A

    It 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.

  22. 22

    What does index -1 identify in a nonempty string?

    1. A

      It returns the last character

    2. B

      It returns the first character

    3. C

      It raises an IndexError for every string

    4. D

      It returns the character before the last one

    Correct answer
    A

    It returns the last character

    Explanation

    A negative index counts from the right; index -1 refers to the last character of the string.

  23. 23

    If s = "abc", what does s[len(s) - 1] evaluate to?

    1. A

      It returns a because indexing starts from zero

    2. B

      It raises an IndexError because length is not an index

    3. C

      It returns the empty string because the stop is omitted

    4. D

      It returns c, the last character

    Correct answer
    D

    It returns c, the last character

    Explanation

    For "abc", len(s) - 1 is 2, which is the index of the last character, c.

  24. 24

    What does "abcdefgh"[3:6] return?

    1. A

      def

    2. B

      defg

    3. C

      cdef

    4. D

      ef

    Correct answer
    A

    def

    Explanation

    In a slice, the start index is included and the stop index is excluded. Indices 3, 4, and 5 in "abcdefgh" contain d, e, and f.

  25. 25

    What does "abcdefgh"[3:6:2] return?

    1. A

      de

    2. B

      ef

    3. C

      dfg

    4. D

      df

    Correct answer
    D

    df

    Explanation

    A positive step of 2 takes every other character, starting at index 3 (d), then index 5 (f), and stops before index 6.

  26. 26

    What is the default step for a string slice such as s[2:5]?

    1. A

      It takes every second character by default

    2. B

      It takes each character from the start up to, but not including, the stop

    3. C

      It includes the stop character but skips the start character

    4. D

      It returns a single character at the start index

    Correct answer
    B

    It 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.

  27. 27

    What does s[:] do for a string s?

    1. A

      It returns an empty string

    2. B

      It returns the string in reverse order

    3. C

      It returns only the first character

    4. D

      It returns the full string

    Correct answer
    D

    It returns the full string

    Explanation

    The slice [:] omits the start, stop, and step, so it selects the full string in its original order.

  28. 28

    What does s[::-1] do?

    1. A

      It returns every other character from left to right

    2. B

      It raises an IndexError because the step is negative

    3. C

      It returns the string in reverse order

    4. D

      It returns only the last character

    Correct answer
    C

    It returns the string in reverse order

    Explanation

    A step of -1 moves through the string from right to left, producing the reversed character sequence.

  29. 29

    What does "abcdefgh"[4:1:-2] return?

    1. A

      edc

    2. B

      ec

    3. C

      ce

    4. D

      ecb

    Correct answer
    B

    ec

    Explanation

    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.

  30. 30

    What does "abcdef"[5:2] return?

    1. A

      An empty string

    2. B

      An IndexError

    3. C

      The character at index 6 only

    4. D

      The characters from index 3 through index 6

    Correct answer
    A

    An 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.

  31. 31

    In a Python slice s[start:stop], how is the stop index treated?

    1. A

      It starts at index 4 because counting starts at 1

    2. B

      It stops before the character at index 10

    3. C

      It includes the character at index 10

    4. D

      It raises an error if the stop equals the last index

    Correct answer
    B

    It 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.

  32. 32

    How does evaluating an expression in the interactive shell differ from placing it alone in a program file?

    1. A

      The shell displays expression values automatically; a file requires explicit output

    2. B

      Expressions print their values in files but not in the shell

    3. C

      A file prints expressions only when they contain strings

    4. D

      The shell and files both automatically print every expression

    Correct answer
    A

    The 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.

  33. 33

    How can a Python program explicitly display an expression's result to the user?

    1. A

      Enclose the expression in quotation marks

    2. B

      Place the expression on the last line of the file

    3. C

      Wrap the expression in print(...)

    4. D

      Assign the expression to a variable

    Correct answer
    C

    Wrap the expression in print(...)

    Explanation

    A program must explicitly call print to display a result to the user when it is run from a file.

  34. 34

    What does Python normally do after a print statement displays its result?

    1. A

      Each call displays only the type of its result

    2. B

      Each call waits for the user to press Enter before printing

    3. C

      Each call appends its result to the previous line without a separator

    4. D

      Each call prints its result and then starts a new line

    Correct answer
    D

    Each call prints its result and then starts a new line

    Explanation

    Each print call displays its result and then ends the line, so two separate calls normally produce output on separate lines.

  35. 35

    What happens when multiple objects are passed to print separated by commas?

    1. A

      It prints them on one line with spaces between the objects

    2. B

      It concatenates the objects with no separator and converts all to strings first

    3. C

      It raises a type error if the objects have different types

    4. D

      It prints each object on a separate line

    Correct answer
    A

    It prints them on one line with spaces between the objects

    Explanation

    When multiple objects are passed to one print call separated by commas, Python displays them on the same line with spaces between them by default.

  36. 36

    How can a program concatenate an integer with strings using +?

    1. A

      Use len to make the integer compatible with strings

    2. B

      Python automatically converts the integer to a string during concatenation

    3. C

      Convert the other strings to integers

    4. D

      Convert the integer to a string before concatenating

    Correct answer
    D

    Convert 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.

  37. 37

    What is the role of the string passed to input(...)?

    1. A

      It converts the prompt into a variable name

    2. B

      It prints the prompt after the user has entered a response

    3. C

      It displays the prompt and waits for user input

    4. D

      It prints the prompt and immediately returns an empty string

    Correct answer
    C

    It 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.

  38. 38

    What type of value does input return?

    1. A

      The user's entry is returned as a float if it contains a decimal point

    2. B

      The user's entry is returned as a string

    3. C

      The user's entry is automatically converted to an integer if it contains digits

    4. D

      The user's entry is returned as a Boolean

    Correct answer
    B

    The user's entry is returned as a string

    Explanation

    input returns the user's entry as a string, even when the entry consists of digits.

  39. 39

    If a user enters 3, what does print(5 * input(...)) display?

    1. A

      The string "33333"

    2. B

      15

    3. C

      A type error

    4. D

      The integer 33333

    Correct answer
    A

    The string "33333"

    Explanation

    If the user types 3, input returns the string "3". Multiplying a string by 5 repeats its characters, producing "33333".

  40. 40

    If the user enters 3, what does print(5 * int(input(...))) display?

    1. A

      The string "33333"

    2. B

      A type error because input is always a string

    3. C

      The string "15"

    4. D

      The integer 15

    Correct answer
    D

    The integer 15

    Explanation

    Casting the input result with int converts the digit string "3" to the integer 3, so multiplying by 5 performs arithmetic and gives 15.

  41. 41

    How can a program print a sentence that includes a verb entered by the user?

    1. A

      Multiply the input value by the number of words in the sentence

    2. B

      Put the variable name inside quotation marks so Python substitutes its value

    3. C

      Print the literal text and the input value as separate objects

    4. D

      Use input inside the sentence's quotation marks

    Correct answer
    C

    Print 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.

  42. 42

    Which relationship describes the Newton-method next-guess calculation presented in the lecture?

    1. A

      Divide the current guess by the sum of the function and derivative values

    2. B

      Subtract the derivative value at the current guess from the function value

    3. C

      Add the derivative at the current guess divided by the function value to the current guess

    4. D

      Subtract the function value at the current guess divided by the derivative at that guess from the current guess

    Correct answer
    D

    Subtract 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.

  43. 43

    For the cube-root example, how are the function and its derivative supplied to the program?

    1. A

      The function is 3g23g^23g2 and the derivative is g3−xg^3-xg3−x

    2. B

      The derivative is always set equal to the current guess

    3. C

      The program automatically infers the derivative from the function

    4. D

      The program explicitly uses g3−xg^3-xg3−x and 3g23g^23g2 for the function and derivative

    Correct answer
    D

    The program explicitly uses g3−xg^3-xg3−x and 3g23g^23g2 for the function and derivative

    Explanation

    For finding the cube root of x, the function used is g3−xg^3-xg3−x and its derivative is 3g23g^23g2. In this introductory program, both formulas are written explicitly.

  44. 44

    What does the Newton-method program in the example do after calculating the next guess?

    1. A

      It keeps calculating guesses until the exact root is found

    2. B

      It calculates one next guess and stops

    3. C

      It prints only the initial guess and never calculates another

    4. D

      It calculates the derivative automatically and repeats twice

    Correct answer
    B

    It 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.

  45. 45

    How does an f-string combine literal text with calculated values?

    1. A

      It converts every character inside the string to a variable

    2. B

      It evaluates only the text outside curly braces

    3. C

      It evaluates expressions inside curly braces and prints other text literally

    4. D

      It uses commas to combine objects and inserts spaces automatically

    Correct answer
    C

    It evaluates expressions inside curly braces and prints other text literally

    Explanation

    An f-string begins with f before the quoted string. Expressions in curly braces are evaluated, while text outside the braces is printed literally.

  46. 46

    What is a practical advantage of using an f-string to print a number beside a percent sign?

    1. A

      They can insert evaluated expressions without manual casting and concatenation

    2. B

      They can contain only variable names, not other expressions

    3. C

      They automatically add a space before every punctuation mark

    4. D

      They prevent expressions from being evaluated until after printing

    Correct answer
    A

    They 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.

  47. 47

    Which statement summarizes where Python expressions can be used?

    1. A

      Expressions can be placed in many contexts and are evaluated as needed

    2. B

      Expressions are evaluated only when they are printed

    3. C

      Expressions can appear only on the right side of assignments

    4. D

      Expressions can appear only inside arithmetic statements

    Correct answer
    A

    Expressions 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.

  48. 48

    What does == mean in Python?

    1. A

      It assigns the right-hand value to the left-hand variable

    2. B

      It tests equivalence and produces True or False

    3. C

      It converts both values to the same type

    4. D

      It checks whether the left value is greater than the right

    Correct answer
    B

    It 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.

  49. 49

    What does the expression 2 < 3 evaluate to?

    1. A

      The string "True"

    2. B

      The integer 1

    3. C

      The Boolean value False

    4. D

      The Boolean value True

    Correct answer
    D

    The Boolean value True

    Explanation

    The expression 2 < 3 is true, so it evaluates to the Boolean value True.

  50. 50

    What does the Python operator != test?

    1. A

      It tests whether two values have different types only

    2. B

      It tests whether the left value is smaller

    3. C

      It tests whether two values are different

    4. D

      It assigns one value to another

    Correct answer
    C

    It 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.

  51. 51

    What is the result of comparing the string "a" with the string "A" using ==?

    1. A

      It is True only when both strings have the same length

    2. B

      It is True because both are the letter a

    3. C

      It raises an error because case cannot be compared

    4. D

      It is False because lowercase and uppercase letters differ

    Correct answer
    D

    It is False because lowercase and uppercase letters differ

    Explanation

    Because Python strings are case sensitive, lowercase a and uppercase A are distinct characters and are not equal.

  52. 52

    What does not (2 < 3) evaluate to?

    1. A

      An error, because not applies only to variables

    2. B

      True, because not repeats the comparison

    3. C

      The integer 0, rather than a Boolean

    4. D

      False, because not reverses True

    Correct answer
    D

    False, because not reverses True

    Explanation

    The not operator reverses a Boolean value. Since 2 < 3 is True, not (2 < 3) is False.

  53. 53

    When does a Boolean expression joined by and evaluate to True?

    1. A

      It is True only when both expressions are True

    2. B

      It is True whenever at least one expression is True

    3. C

      It returns the value of the first expression

    4. D

      It is False only when both expressions are False

    Correct answer
    A

    It is True only when both expressions are True

    Explanation

    An and expression is True only when both component Boolean expressions are True. If either one is False, the result is False.

  54. 54

    When does a Boolean expression joined by or evaluate to False?

    1. A

      It is False whenever either expression is False

    2. B

      It is True when at least one expression is True

    3. C

      It returns the value of the second expression

    4. D

      It is True only when both expressions are True

    Correct answer
    B

    It is True when at least one expression is True

    Explanation

    An or expression is False only when both component expressions are False; if either is True, the result is True.

  55. 55

    Why are Boolean expressions useful in programs?

    1. A

      Boolean expressions can be used only as print arguments

    2. B

      Boolean expressions can be combined and used to guide program behavior

    3. C

      Comparisons always produce numeric values

    4. D

      Boolean expressions cannot be stored in variables

    Correct answer
    B

    Boolean 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.

  56. 56

    How does an if/else structure choose which block to execute?

    1. A

      Neither block runs unless both conditions are True

    2. B

      The if block runs if the condition is True; otherwise, the else block runs

    3. C

      The else block runs only when the if condition is True

    4. D

      Both blocks run, in order, regardless of the condition

    Correct answer
    B

    The if block runs if the condition is True; otherwise, the else block runs

    Explanation

    With if and else, Python runs the indented if block when the condition is True and the else block otherwise; the two alternatives are not both executed.

  57. 57

    How does Python determine which statements belong to an if block?

    1. A

      By indentation beneath the if statement

    2. B

      By quotation marks around the commands

    3. C

      By placing the commands inside parentheses

    4. D

      By the number of statements before the next variable assignment

    Correct answer
    A

    By indentation beneath the if statement

    Explanation

    An if block's commands are identified by indentation. Commands indented under the if belong to that block.

  58. 58

    What happens when an if condition is False and there is no else block?

    1. A

      It executes the block anyway, then repeats the condition

    2. B

      It stops the entire program

    3. C

      It skips the indented block and continues with the rest of the program

    4. D

      It treats the condition as an assignment

    Correct answer
    C

    It skips the indented block and continues with the rest of the program

    Explanation

    If an if condition is False and there is no else, Python skips that block and continues with the following statements outside it.

  59. 59

    What is the purpose of elif in a conditional chain?

    1. A

      It runs only if the first if condition is True

    2. B

      It always runs after the if block

    3. C

      It checks every branch and executes all true ones

    4. D

      It checks another condition if earlier conditions were False

    Correct answer
    D

    It checks another condition if earlier conditions were False

    Explanation

    An elif checks another condition when the preceding condition was False. In an if/elif chain, the first true branch is executed and later branches are skipped.

  60. 60

    When does the final else block in an if/elif/else chain run?

    1. A

      It runs whenever any preceding condition is True

    2. B

      It repeats the last elif condition

    3. C

      It runs if none of the preceding conditions is True

    4. D

      It makes every branch in the chain execute

    Correct answer
    C

    It runs if none of the preceding conditions is True

    Explanation

    An else at the end of a conditional chain acts as a catch-all: it runs only when none of the preceding if or elif conditions are True.

  61. 61

    If multiple conditions in an if/elif chain are True, which branch executes?

    1. A

      Only the first true branch runs

    2. B

      Python chooses a true branch at random

    3. C

      Every true branch runs

    4. D

      The last true branch runs

    Correct answer
    A

    Only 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.

  62. 62

    What can happen when a program uses two separate if statements?

    1. A

      Only the first block can run because all if statements form one chain

    2. B

      Python reports an error when two if conditions are True

    3. C

      The second block runs only if the first condition is False

    4. D

      Both blocks can run if both conditions are True

    Correct answer
    D

    Both blocks can run if both conditions are True

    Explanation

    Separate if statements are independent. If each condition is True, each corresponding block can execute; unlike elif, they are not alternatives in one chain.

  63. 63

    Why can incorrect indentation cause a program to print a statement even when an if condition is False?

    1. A

      Statements at the outer level run only if the condition is True

    2. B

      Every statement after an if belongs to its block automatically

    3. C

      Indentation determines whether variables are converted to Booleans

    4. D

      Indentation determines whether a statement belongs to a conditional block

    Correct answer
    D

    Indentation determines whether a statement belongs to a conditional block

    Explanation

    If a statement is indented inside an if block, it runs only when that condition is True. A statement at the outer indentation level runs after the conditional regardless of that condition.

  64. 64

    What is a nested conditional?

    1. A

      They prevent code from rejoining the rest of the program

    2. B

      They make the inner condition run before the outer condition

    3. C

      They allow a conditional block to contain another conditional block

    4. D

      They force all conditions to be true at once

    Correct answer
    C

    They 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.

  65. 65

    Which conditional structure correctly classifies a guess as too high, equal, or too low?

    1. A

      Use > for too high, an equality elif, and else for too low

    2. B

      Use three separate print statements without conditions

    3. C

      Use == for too high, < for equal, and else for too low

    4. D

      Use >= for too high, then an equality elif, then a low else

    Correct answer
    A

    Use > for too high, an equality elif, and else for too low

    Explanation

    For a guess that must be classified as too high, equal, or too low, if/elif/else selects one appropriate outcome. Using > rather than >= allows an equal guess to reach the equality case.

  66. 66

    Why might a program use else rather than another elif for the final guess outcome?

    1. A

      An else is required to contain a new Boolean condition

    2. B

      An elif cannot be used for the final possible case

    3. C

      An else executes before the preceding if

    4. D

      An else can handle the remaining case when earlier branches cover the other possibilities

    Correct answer
    D

    An else can handle the remaining case when earlier branches cover the other possibilities

    Explanation

    A catch-all else is convenient when the remaining possibilities are already covered by earlier branches; an explicit elif can also test the remaining case.

  67. 67

    Why is it useful to test code early with different inputs?

    1. A

      It converts conditional branches into separate functions

    2. B

      It is useful only after the complete program has been written

    3. C

      It guarantees the program has no errors without inspecting its output

    4. D

      It helps reveal bugs early by checking behavior in different cases

    Correct answer
    D

    It 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.

Question 1 of 67
Question 1 of 67

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent