Skip to main content

Section 2.18 Chapter 2 Assessment

Check your understanding

Checkpoint 2.18.1.

There is a function we are providing in for you in this problem called square. It takes one integer and returns the square of that integer value. Write code to assign a variable called xyz the value 5*5 (five squared). Use the square function, rather than just multiplying with *.

Checkpoint 2.18.2.

Write code to assign the number of characters in the string rv to a variable num_chars.

Checkpoint 2.18.3.

    The code below initializes two variables, z and y. We want to assign the total number of characters in z and in y to the variable a. Which of the following solutions, if any, would be considered hard coding?
    z = "hello world"
    y = "welcome!"
    
  • a = len("hello worldwelcome!")
  • Though we are using the len function here, we are hardcoding what len should return the length of. We are not referencing z or y.
  • a = 11 + 8
  • This is hardcoding, we are writing in the value without referencing z or y.
  • a = len(z) + len(y)
  • This is not considered hard coding. We are using the function len to determine the length of what is stored in z and y, which is a correct way to approach this problem.
  • a = len("hello world") + len("welcome!")
  • Though we are using the len function here, we are hardcoding what len should return the length of each time we call len. We are not referencing z or y.
  • none of the above are hardcoding.
  • At least one of these solutions is considered hardcoding. Take another look.

Checkpoint 2.18.4.

    :
    What does the code shown in the codelens image print when it finishes executing?
  • an error
  • This is a correct. When using + to stick strings together, all variables need to be a string. This throws a type error.
  • sara 11
  • Hmm, this would be true if 11 was a string but it isn’t
  • sara11
  • Hmm, not quite, we stick a space on our string in line 3
  • "sara11"
  • Hmm, not quite, we stick a space on our string in line 3 and we don’t have quotes
  • "sara 11"
  • Hmm, we don’t have quotes in our string

Checkpoint 2.18.5.

    :
    name = "sara"
    age = 11
    info = name + " " + str(age)
    print(info)
    
    Here is that same code again, but the error has been fixed. Which line number shows a line of code we would define as an expression?
  • 1
  • This is a variable being asssigned a string value
  • 2
  • This is a variable being assigned an integer value
  • 3
  • Correct, "sara" + " " + age is an expression that we want to evaluate to "sara 11" and store the string in the info variable
  • 4
  • This prints the string value stored the info variable
You have attempted of activities on this page.