Return to home page

Python Quiz: Module 2

Formatting and Files


  1. Which of the following code snippets will raise an error message assuming no variable assignments?
  2. The variables product and cost have been assigned values using this code: product:str = 'apple'
    cost:float = 1.39
    Which of the following code snippets will raise and error message?
  3. What type of error generated in question 2 above?
  4. Based on the variables created in question 2, which of the four ways to format output is used to generate This apple costs $1.39.:
    print(f'This {product} costs ${cost:.2f}.')?
  5. Based on the variables created in question 2, which of the four ways to format output is used to generate This apple costs $1.39.:
    print("This {} costs ${:.2f}.".format(product,cost))?
  6. What is the purpose of the backslash in this code: print('I\'m enjoying Python.')?
  7. Which characters can be used to render single quotes, double quotes, newline and tab characters without using a backslash?
  8. Using three string variables, name, address, phone, have been defined and there is a variable file_ref that points to an open file, which code snippets below will NOT be helpful in creating a CSV file?
  9. Given code that sets the value of val, val: float = 1000/3*2, what is the number returned by this code: print(1000/3*2:.2f)?
  10. What output is expected and why, when running the following code:
    msg: str
    msg += "hello"
    print(msg)