Spread the love

1. This code is supposed to display “2 + 2 = 4” on the screen, but there is an error. Find the error in the code and fix it, so that the output is correct.

print(“2 + 2 = ” + (2 + 2))

  • print(“2 + 2 = ” + str(2 + 2))

2. In this scenario, two friends are eating dinner at a restaurant. The bill comes in the amount of 47.28 dollars. The friends decide to split the bill evenly between them, after adding 15% tip for the service. Calculate the tip, the total amount to pay, and each friend’s share, then output a message saying “Each person needs to pay: ” followed by the resulting number.

bill = ___ # Assign “bill” variable with bill amount

tip = bill * ___ # Multiply by stated tip rate

total = bill + ___ # Sum the “total” of the “bill” and “tip”

share = ___ # Divide “total” by number of friends dining

print(“___” + ___) # Enter the required string and “share”

# Hint: Remember to convert incompatible data types

  • bill = 47.28 
    tip = bill * 0.15
    total = bill + tip num_friends = 2
    share = total / num_friends
    print(“Each person needs to pay: $” + str(share))

3. This code is supposed to take two numbers, divide one by another so that the result is equal to 1, and display the result on the screen. Unfortunately, there is an error in the code. Find the error and fix it, so that the output is correct.

numerator = 10

denominator = 0

result = numerator / denominator

print(result)

  • numerator = 10
    denominator = numerator
    result = numerator / denominator
    print(result)

4. Combine the variables to display the sentence “How do you like Python so far?”

word1 = “How”

word2 = “do”

word3 = “you”

word4 = “like”

word5 = “Python”

word6 = “so”

word7 = “far?”

print(___)

  • print(word1 + ” ” + word2 + ” ” + word3 + ” ” + word4 + ” ” + word5 + ” ” + word6 + ” ” + word7)

5. What do you call a combination of numbers, symbols, or other values that produce a result when evaluated?

  • An explicit conversion
  • An expression
  • A variable
  • An implicit conversion

6. This function converts miles to kilometers (km).

Complete the code to return the result of the conversion.

NOTE: The following items occur outside of the function. Do not try to change the indentations on the associated code or you will receive an error.

Call the function to convert the trip distance from miles to kilometers.

Fill in the blank to print the result of the conversion.

Calculate the round-trip in kilometers by doubling the result, and fill in the blank to print the result.

 

# 1) Complete the function to return the result of the conversion

def convert_distance(miles):

km = miles * 1.6 # approximately 1.6 km in 1 mile

return ___

# Do not indent any of the following lines of code as they are

# meant to be located outside of the function above

my_trip_miles = 55

# 2) Convert my_trip_miles to kilometers by calling the function above

my_trip_km = ___(my_trip_miles)

# 3) Fill in the blank to print the result of the my_trip_km conversion

print(“The distance in kilometers is ” + str(___))

# 4) Calculate the round-trip in kilometers by doubling the result of

# my_trip_km. Fill in the blank to print the result.

print(“The round-trip in kilometers is ” + str(___*___))

  • # 1) Complete the function to return the result of the conversion

def convert_distance(miles):

km = miles * 1.6 # approximately 1.6 km in 1 mile

return km

 

# Do not indent any of the following lines of code as they are

# meant to be located outside of the function above

 

my_trip_miles = 55



# 2) Convert my_trip_miles to kilometers by calling the function above

my_trip_km = convert_distance(my_trip_miles)



# 3) Fill in the blank to print the result of the my_trip_km conversion

print(“The distance in kilometers is ” + str(my_trip_km))



# 4) Calculate the round-trip in kilometers by doubling the result of

# my_trip_km. Fill in the blank to print the result.

print(“The round-trip in kilometers is ” + str(my_trip_km*2))

7. This function compares two numbers and returns them in increasing order.

Fill in the blanks, so the print statement displays the result of the function call in order.

Hint: if a function returns multiple values, don’t forget to store these values in multiple variables

# This function compares two numbers and returns them

# in increasing order.

def order_numbers(number1, number2):

if number2 > number1:

return number1, number2

else:

return number2, number1

# 1) Fill in the blanks so the print statement displays the result

# of the function call

___, ___ = order_numbers(100, 99)

print(smaller, bigger)

  • smaller, bigger

8. What are the values passed into functions as input called?

  • Variables
  • Return values
  • Parameters
  • Data types

9. Complete the first line of the “print_seconds” function so that it accepts three parameters: hours, minutes, and seconds. Remember to use the “def” keyword to tell the Python interpreter the block of code is intended to define a function.

___ ___(___, ___, ___):

print(hours*3600+minutes*60+seconds)

print_seconds(1,2,3)

  • def print_seconds(hours, minutes, seconds):
    print(hours*3600+minutes*60+seconds)
    print_seconds(1, 2, 3)

10. What is the purpose of the def keyword?

  • Used to define a new function
  • Used to define a return value
  • Used to define a new variable
  • Used to define a new parameter

11. What’s the value of this Python expression: (2**2) == 4?

  • 4
  • 2**2
  • True
  • False

12. Complete the script by filling in the missing parts. The function receives a name, then returns a greeting based on whether or not that name is “Taylor”.

def greeting(name):

if ___ == “Taylor”:

return “Welcome back Taylor!”

___:

return “Hello there, ” + name

print(greeting(“Taylor”))

print(greeting(“John”))

  • def greeting(name):
         if name == “Taylor”:
              return “Welcome back Taylor!”
         else: return “Hello there, “ + name

    print(greeting(“Taylor”))
    print(greeting(“John”))

13. What’s the output of this code if number equals 10?

if number > 11:

print(0)

elif number != 10:

print(1)

elif number >= 20 or number < 12:

print(2)

else:

print(3)

14. Is “A dog” smaller or larger than “A mouse”? Is 9999+8888 smaller or larger than 100*100? Replace the plus sign in the following code to let Python check it for you and then answer.

print(“A dog” + “A mouse”)

print(9999+8888 + 100*100)

  • “A dog” is larger than “A mouse” and 9999+8888 is larger than 100*100
  • “A dog” is smaller than “A mouse” and 9999+8888 is larger than 100*100
  • “A dog” is larger than “A mouse” and 9999+8888 is smaller than 100*100
  • “A dog” is smaller than “A mouse” and 9999+8888 is smaller than 100*100

15. If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you fill in the gaps in the calculate_storage function below, which calculates the total number of bytes needed to store a file of a given size?

def calculate_storage(filesize):

block_size = 4096

# Use floor division to calculate how many blocks are fully occupied

full_blocks = ___

# Use the modulo operator to check whether there’s any remainder

partial_block_remainder = ___

# Depending on whether there’s a remainder or not, return

# the total number of bytes required to allocate enough blocks

# to store your data.

if partial_block_remainder > 0:

return ___

return ___

 

print(calculate_storage(1)) # Should be 4096

print(calculate_storage(4096)) # Should be 4096

print(calculate_storage(4097)) # Should be 8192

print(calculate_storage(6000)) # Should be 8192

  • def calculate_storage(filesize):

block_size = 4096

# Use floor division to calculate how many blocks are fully occupied

full_blocks = filesize // block_size

# Use the modulo operator to check whether there’s any remainder

partial_block_remainder = filesize % block_size

# Depending on whether there’s a remainder or not, return

# the total number of bytes required to allocate enough blocks

# to store your data.

if partial_block_remainder > 0:

return (full_blocks + 1) * block_size

return full_blocks * block_size

print(calculate_storage(1)) # Should be 4096

print(calculate_storage(4096)) # Should be 4096

print(calculate_storage(4097)) # Should be 8192

print(calculate_storage(6000)) # Should be 8192

crash course on python week 2 quiz answers

16. Complete the code to output the statement, “Diego’s favorite food is lasagna”. Remember that precise syntax must be used to receive credit.

name = ___

fav_food = ___

print(name + “’s favorite food is ” + fav_food)

  • name = “Diego”

fav_food = “lasagna”

print(name + “‘s favorite food is ” + fav_food)

17. What’s the value of this Python expression: 7 < “number”?

  • True
  • False
  • TypeError
  • 0

18. What directly follows the elif keyword in an elif statement?

  • A logical operator
  • A colon
  • A function definition
  • A comparison

19. Consider the following scenario about using if-elif-else statements:

Police patrol a specific stretch of dangerous highway and are very particular about speed limits. The speed limit is 65 miles per hour. Cars going 80 miles per hour or more are given a “Reckless Driving” ticket. Cars going more than 65 miles per hour are given a “Speeding” ticket. Any cars going less than that are labeled “Safe” in the system.

Fill in the blanks in this function so it returns the proper ticket type or label.

20. In the following code, what would be the output?

test_num = 12

if test_num > 15:

print(test_num / 4)

else:

print(test_num + 3)

  • 15
  • 3
  • 12
  • 4

21. Fill in the blanks to complete the function. The character translator function receives a single lowercase letter, then prints the numeric location of the letter in the English alphabet. For example, “a” would return 1 and “b” would return 2. Currently, this function only supports the letters “a”, “b”, “c”, and “d” It returns “unknown” for all other letters or if the letter is uppercase.

22. Can you calculate the output of this code?

def difference(x, y):

z = x – y

return z

print(difference(5, 3))

23. What’s the value of this Python expression?

x = 5*2 ((10 != x) or (10 > x))

  • True
  • False
  • 15
  • 10

24. Fill in the blanks to complete the “safe_division” function. The function accepts two numeric variables through the function parameters and divides the “numerator” by the “denominator”. The function’s main purpose is to prevent a ZeroDivisionError by checking if the “denominator” is 0. If it is 0, the function should return 0 instead of attempting the division. Otherwise all other numbers will be part of the division equation. Complete the body of the function so that the function completes its purpose.

25. Code that is written so that it is readable and doesn’t conceal its intent is called what?

  • Maintainable code
  • Self-documenting code
  • Obvious code
  • Intentional code

26. Consider the following scenario about using if-elif-else statements:

The fall weather is unpredictable. If the temperature is below 32 degrees Fahrenheit, a heavy coat should be worn. If it is above 32 degrees but not above 50 degrees, then a jacket should be sufficient. If it is above 50 but not above 65 degrees, a sweatshirt is appropriate, and above 65 degrees a t-shirt can be worn.

Fill in the blanks in the function below so it returns the proper clothing type for the temperature.

27. What’s the value of the comparison in this if statement? Hint: The answer is not what the code will print.

n = 4
if n*6 > n**2 or n%2 == 0:
print(“Check”)

  • Check
  • False
  • 24 > 16 or 0
  • True

28. Fill in the blanks to complete the function. The “identify_IP” function receives an “IP_address” as a string through the function’s parameters, then it should print a description of the IP address. Currently, the function should only support three IP addresses and return “unknown” for all other IPs.

29. Can you calculate the output of this code?

def sum(x, y):
return(x+y)
print(sum(sum(1,2), sum(3,4)))

  • 10

30. What’s the value of this Python expression?

((24 == 5*2) and (24 > 3*5) and (2*6 == 12))

  • True
  • False
  • 10
  • 15

31. Which of the following are good coding-style habits? Select all that apply.

  • Writing code using the least amount of characters as possible
  • Cleaning up duplicate code by creating a function that can be reused
  • Adding comments
  • Refactoring the code

32. Complete the code to output the statement, “Marjery lives at her home address of 1234 Mockingbird Lane”. Remember that precise syntax must be used to receive credit.

33. Consider the following scenario about using if-elif-else statements:

Students in a class receive their grades as Pass/Fail. Scores of 60 or more (out of 100) mean that the grade is “Pass”. For lower scores, the grade is “Fail”. In addition, scores above 95 (not included) are graded as “Top Score”.

Fill in the blanks in this function so that it returns the appropriate “Pass”, “Fail”, or “Top Score” grade.

34. Fill in the blanks to complete the function. The “complementary_color” function receives a primary color name in all lower case, then prints its complementary color. Currently, the function only supports the primary colors of red, yellow, and blue. It returns “unknown” for all other colors or if the word has any uppercase characters.

35. Complete the code to output the statement, “192.168.1.10 is the IP address of Printer Server 1”. Remember that precise syntax must be used to receive credit.

36. What is the value of this Python expression: “blue” == “Blue”?

  • True
  • False
  • orange
  • blue

37. In the following code, what would be the output?

number = 4
if number * 4 < 15:
print(number / 4)
elif number < 5:
print(number + 3)
else:
print(number * 2 % 5)

  • 3
  • 4
  • 7
  • 1

38. Fill in the blanks to complete the function. The fractional_part function divides the numerator by the denominator, and returns just the fractional part (a number between 0 and 1). Complete the body of the function so that it returns the right number. Note: Since division by 0 produces an error, if the denominator is 0, the function should return 0 instead of attempting the division.

39. What are some of the benefits of good code style? Select all that apply.

  • Makes the intent of the code obvious
  • Makes sure the author will refactor it later
  • Allows it to never have to be touched again
  • Easier to maintain

40. What’s the value of this Python expression: “big” > “small”?

  • True
  • False
  • big
  • small

41. What is the elif keyword used for?

  • To mark the end of the if statement
  • To handle more than two comparison cases
  • To replace the “or” clause in the if statement
  • Nothing – it’s a misspelling of the else-if keyword

42. When using an if statement, the code inside the if block will only execute if the conditional statement returns what?

  • False
  • A string
  • 0
  • True

43. What’s the value of this Python expression?

((10 >= 5*2) and (10 <= 5*2))

  • True
  • False
  • 10
  • 5*2

44. Can you calculate the output of this code?

def greater_value(x, y):
if x > y:
return x
else:
return y

print(greater_value(10,3*5))

  • 15

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *