Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 3: Objects

31 problems · 0 figures

Concepts in Practice 3.1.1 §3.1 source ↗

What is the index of the second character in a string?
  1. a.
    1
  2. b.
    2
  3. c.
    -2

Answer

A. 1
Since index values start at 0, the index 1 refers to the second character of the string.

Concepts in Practice 3.1.2 §3.1 source ↗

If s = "Python!", what is the value of s[1] + s[-1]?
  1. a.
    "P!"
  2. b.
    "y!"
  3. c.
    "yn"

Answer

B. "y!"
The character at index 1 is y, and the character at index -1 is !.

Concepts in Practice 3.1.3 §3.1 source ↗

If s = "Python!", what type of object is s[0]?
  1. a.
    character
  2. b.
    integer
  3. c.
    string

Answer

C. string
The result of s[0] is "P" , which is a string of length 1.

Concepts in Practice 3.1.4 §3.1 source ↗

What is the code point for the letter A?
  1. a.
    1
  2. b.
    65
  3. c.
    97

Answer

B. 65
Code points 65 to 90 are used for uppercase letters.

Concepts in Practice 3.1.5 §3.1 source ↗

What value does ord("0") return?
  1. a.
    0
  2. b.
    48
  3. c.
    Error

Answer

B. 48
The code point for the character "0" is 48.

Concepts in Practice 3.1.6 §3.1 source ↗

What does chr(126) return?
  1. a.
    ~
  2. b.
    "~"
  3. c.
    Error

Answer

B. "~"
Tilde (~) is the character for code point 126. The chr() function returns a string of length one.

Concepts in Practice 3.1.7 §3.1 source ↗

Which of the following is an escape sequence?
  1. a.
    t
  2. b.
    /t
  3. c.
    \t

Answer

C. \t
An escape sequence is two characters long and begins with a backslash (\).

Concepts in Practice 3.1.8 §3.1 source ↗

Which statement prints a backslash (\) to the screen?
  1. a.
    print(\\)
  2. b.
    print(\"\")
  3. c.
    print("\\")

Answer

C. print("\\")
The first backslash indicates an escape sequence, and the second backslash indicates the character.

Concepts in Practice 3.1.9 §3.1 source ↗

Which statement prints Enter and here on separate lines?
  1. a.
    print("Enter here")
  2. b.
    print("Enter" + \n + "here")
  3. c.
    print("Enter" + "\n" + "here")

Answer

C. print("Enter" + "\n" + "here")
The escape sequence \n produces a new line. print("Enter\nhere") produces the same result.

Concepts in Practice 3.2.1 §3.2 source ↗

What is the output of the following code?
animal = "dog"
says = "bark"
print(f"My {animal} says {says} {says} {says}")
  1. a.
    My dog bark bark bark bark
  2. b.
    My dog says bark bark bark
  3. c.
    Error

Answer

B. My dog says bark bark bark
The same variable, says, is used in three replacement fields.

Concepts in Practice 3.2.2 §3.2 source ↗

What is the output of the following code?
temp = "hot"
food = "potato"
print("{temp} {food}")
  1. a.
    {temp} {food}
  2. b.
    hot potato
  3. c.
    Error

Answer

A. {temp} {food}
The string is not an f-string, so the brace characters (around temp and food) are printed literally.

Concepts in Practice 3.2.3 §3.2 source ↗

How can the following code be rewritten using an f-string?
print(x, "+", y, "=", x + y)
  1. a.
    print(f"x + y = {x+y}")
  2. b.
    print(f"{x + y} = {x + y}")
  3. c.
    print(f"{x} + {y} = {x+y}")

Answer

C. print(f"{x} + {y} = {x+y}")
Using an f-string allows the entire output to be specified without commas and extra quote marks.

Concepts in Practice 3.2.4 §3.2 source ↗

What f-string formats a date as MM/DD/YYYY?
  1. a.
    f"{month:02d}/{day:02d}/{year}"
  2. b.
    f"{month:2d}/{day:2d}/{year}"
  3. c.
    f"{month}/{day}/{year}"

Answer

A. f"{month:02d}/{day:02d}/{year}"
The format specifier 02d appends a 0 to the front of the integer when the integer is only one digit.

Concepts in Practice 3.2.5 §3.2 source ↗

What statement displays the variable money rounded to two decimal places?
  1. a.
    print(f"{money:2d}")
  2. b.
    print(f"{money:2f}")
  3. c.
    print(f"{money:.2f}")

Answer

C. print(f"{money:.2f}")
The format specifier ".2f" (with a period) means fixed-point, rounded to two decimal places.

Concepts in Practice 3.2.6 §3.2 source ↗

What format specifier displays a floating-point number with comma separators and rounded to two decimal places?
  1. a.
    .2,f
  2. b.
    ,.2f
  3. c.
    ,d.2f

Answer

B. ,.2f
This format specifier is useful for displaying large amounts of money.

Concepts in Practice 3.3.1 §3.3 source ↗

How many assignment statements are in the above animation?
  1. a.
    2
  2. b.
    3
  3. c.
    4
Video · Example memory diagram in the book ↗

Answer

B. 3
Each line of the program in the animation is an assignment statement.

Concepts in Practice 3.3.2 §3.3 source ↗

Which of the following best describes the objects assigned?
  1. a.
    two float objects
  2. b.
    two int objects, one float object
  3. c.
    one int object, one float object

Answer

C. one int object, one float object
The program assigns the int object 10 and the float object 9.7 , as shown in the memory diagram.

Concepts in Practice 3.3.3 §3.3 source ↗

What symbol is used to show a variable's current value?
  1. a.
    an arrow
  2. b.
    a small black box
  3. c.
    a rounded box

Answer

A. an arrow
An arrow shows which object a variable currently refers to.

Concepts in Practice 3.3.4 §3.3 source ↗

Which value might be returned by id(rating)?
  1. a.
    9793344
  2. b.
    <class 'float'>
  3. c.
    10.0

Answer

A. 9793344
An object's identity is an integer representing the memory location.

Concepts in Practice 3.3.5 §3.3 source ↗

Which value might be returned by type(rating)?
  1. a.
    10.0
  2. b.
    "float"
  3. c.
    <class 'float'>

Answer

C. <class 'float'>
The type function returns an object's class name like int, float, or str.

Concepts in Practice 3.3.6 §3.3 source ↗

What expression returns the value of an object?
  1. a.
    value(rating)
  2. b.
    rating
  3. c.
    "rating"

Answer

B. rating
A variable refers to an object, so the variable's value is the object's value.

Concepts in Practice 3.4.1 §3.4 source ↗

Which is the correct way to make a list of the numbers 3, 4, and 5?
  1. a.
    new_list == [3, 4, 5]
  2. b.
    new_list = [3, 4, 5]
  3. c.
    new_list = (3, 4, 5)

Answer

B. new_list = [3, 4, 5]
A list of numbers can be set up using an assignment operator.

Concepts in Practice 3.4.2 §3.4 source ↗

Which of the following is not a valid way to specify a list in Python?
  1. a.
    my_list = [2, 2 3, 23]
  2. b.
    my_list = ["Jimmy", 2, "times"]
  3. c.
    my_list = ["C", "C++", "Python", "Java", "Rust", "Scala"]

Answer

A. my_list = [2, 2 3, 23]
Individual elements in a list must be separated by a comma. Python does not understand what is meant by 2 3.

Concepts in Practice 3.4.3 §3.4 source ↗

Which is the correct way to access the 17th element in a list called cardList?
  1. a.
    card_list[17]
  2. b.
    16)card_list(16)
  3. c.
    card_list[16]

Answer

C. card_list[16]
Since indexes start at 0, the required number is one less than the position being accessed.

Concepts in Practice 3.4.4 §3.4 source ↗

What is the correct way to modify the element "Tom" to "Tim" in the following list?
name_list = ["Carla", "Monique", "Westin", "Tom"]
  1. a.
    name_list[-1] = "Tim"
  2. b.
    name_list[4] = "Tim"
  3. c.
    name_list[end] = "Tim"

Answer

A. name_list[-1] = "Tim"
The index -1 always refers to the last element of a list. The expression len(name_list)-1, which evaluates to 3 and refers to the last element in a list of 4 elements, can also be used in place of -1 here.

Concepts in Practice 3.5.1 §3.5 source ↗

Consider the example above. Which accesses tuple_1's first element?
  1. a.
    tuple_1[0]
  2. b.
    tuple_1[1]
  3. c.
    tuple_1[2]

Answer

A. tuple_1[0]
The first element, 2 , is at index 0.

Concepts in Practice 3.5.2 §3.5 source ↗

Which creates a valid tuple?
  1. a.
    tuple_2 = (42, 26, 13)
  2. b.
    tuple_3 = (42, 26, 13.5)
  3. c.
    both

Answer

C. both
Tuples can have elements of the same type as well as elements of different types.

Concepts in Practice 3.5.3 §3.5 source ↗

Which creates a tuple with three elements?
  1. a.
    my_tuple = 'a', 'b', 'c'
  2. b.
    my_tuple = ['a', 'b', 'c']
  3. c.
    my_tuple = ('a', 'b', 'c')

Answer

A. my_tuple = 'a', 'b', 'c'
A tuple must be created using commas. Parentheses are optional but recommended. The three elements are "a" , "b" , and "c" .

Concepts in Practice 3.5.4 §3.5 source ↗

Consider the final program in the example above. What is the value of my_tuple?
  1. a.
    (0.693, 0.414, 3.142)
  2. b.
    (0.693, 1.414, 3.142)
  3. c.
    Error
Video · Mutability of a tuple vs. a list in the book ↗

Answer

B. (0.693, 1.414, 3.142)
my_tuple is initially assigned with the tuple (0.693, 0.414, 3.142). Then my_tuple is assigned with the new tuple, (0.693, 1.414, 3.142), to correct a mistake with the second element.

Concepts in Practice 3.5.5 §3.5 source ↗

Why does the following code produce an error?
tuple_1 = ('alpha', 'bravo', 'charlie')
tuple_1.append('charlie')
  1. a.
    Append isn't allowed.
  2. b.
    Duplicates aren't allowed.
  3. c.
    Strings aren't allowed

Answer

A. Append isn't allowed.
Tuples are immutable. No changes, including appending, can be made after the tuple is created.

Concepts in Practice 3.5.6 §3.5 source ↗

A programmer wants to create a sequence of constants for easy reference that can't be changed anywhere in the program. Which sequence would be the most appropriate?
  1. a.
    list
  2. b.
    tuple

Answer

B. tuple
Tuples are immutable, so attempted changes produce errors. Using a tuple would protect the constants from accidental changes.

Loading chapter 4…

Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.