4.61 Functions and Exceptions Quiz

Ace your homework & exams now with Quizwiz!

What would this program print? x_pos = 16 y_pos = 20 # Computes the midpoint between (0,0) # and the point def compute_distance(x_pos, y_pos): print("Original Point: " + str(x_pos) + ", " + str(y_pos)) print("Midpoint from Origin: " + str(x_pos/2) + ", " + str(y_pos/2)) compute_distance(12, 14) print("Point: " + str(x_pos) + ", " + str(y_pos))

Original Point: 12, 14 Midpoint from Origin: 6.0, 7.0 Point: 16, 20

What will be the output of the following program? name = "Tracy" def print_hello(name): name = "Turtle" print("hello " + name) print_hello(name)

hello Turtle

What will be printed to the screen if the inputs given to the program below are 1.2, -4, hi, 5 ? def retrieve_positive_number(): while True: try: number = int(input("Enter a positive number: ")) if number > 0: return number print("The number must be positive!") except ValueError: print("That wasn't a number!") print(retrieve_positive_number())

Enter a positive number: 1.2 That wasn't a number! Enter a positive number: -4 The number must be positive! Enter a positive number: hi That wasn't a number! Enter a positive number: 5 5

Why do programmers use try and except statements when their code might throw an exception?

To handle exceptions gracefully and either handle the error or print out a useful error message.

What is the purpose of exceptions?

To indicate that something has gone wrong, and the program does not know how to continue

What is the output of this program? Assume the user enters "kipp52", then "baseball45". # Checks if the user entered the correct username def validate_user(username): if username != "coder43" and username != "goodHacker90" and username != "baseball45": return False else: return True # Gets the user's username def get_username(): while True: username = input("Enter your username: ") if validate_user(username): print("Welcome!") return username else: print("That's not a valid username. Try again") print("Welcome to CodeHS!") get_username()

Welcome to CodeHS! Enter your username: kipp52 That's not a valid username. Try again Enter your username: baseball45 Welcome!

What will be the output of the following program? a = 4 def print_something(): a = "code" print(a * 3) print_something()

codecodecode

Which of the programs below will print the following output? I would like a green balloon. def balloon_choice(color): print("I would like a " + color + " balloon.") balloon_choice("green") def balloon_choice(color): print("I would like a " + color + " balloon.") def balloon_choice(): print("I would like a " + color + " balloon.") balloon_choice("green") def balloon_choice(): print("I would like a green balloon.") balloon_choice("green")

def balloon_choice(color): print("I would like a " + color + " balloon.") balloon_choice("green")

Which of the following programs will NOT cause an error? say_hi("Margaret") def say_hi(name): print("hi " + name) function say_hi(name): print("hi " + name) def say_hi(first_name="John", last_name="Doe"): print("hi " + first_name + " " + last_name) say_hi("Polly Pocket") def say_hi(first_name="John", last_name): print("hi " + first_name + " " + last_name) say_hi('Polly', 'Pocket')

def say_hi(first_name="John", last_name="Doe"): print("hi " + first_name + " " + last_name) say_hi("Polly Pocket")

Consider the following functions. def fn_a(num): if num > 3: print("Fizz") def fn_b(num): if num > 2 and num < 4: print("Fizz") Which for loop would print Fizz Fizz for i in range(6): fn_b(i+1) for i in range(6): fn_a(i) fn_b(i) for i in range(6): fn_a(i) for i in range(6): fn_b(i)

for i in range(6): fn_a(i)

What line of code should be written after the function definition to add two single-digit values and print the sum? def get_single_digit(): while True: try: num = int(input("Enter a single-digit positive number: ")) if num > 9: print("Number must be less than 10") elif num < 0: print("Number must be positive.") else: return num except ValueError: print("That isn't a number!")

print(get_single_digit() + get_single_digit())

Which of the following options is NOT a valid Python function definition? # computes the surface area of a cube of side length 10 # def surface_area(): return 2*(100 + 100 + 100) # computes the surface area of a rectangular prism def surface_area(length, width=10, height): return 2*(length * width + width * height + height * length) # computes the surface area of a rectangular prism def surface_area(length, width, height): return 2*(length * width + width * height + height * length) # computes the surface area of a rectangular prism def surface_area(length, width, height=10): return 2*(length * width + width * height + height * length)

# computes the surface area of a rectangular prism def surface_area(length, width=10, height): return 2*(length * width + width * height + height * length) SyntaxError: non-default argument follows default argument

Which of the following inputs to the program below would print a statement containing Your number: try: my_number = int(input("Enter an integer: ")) print("Your number: " + str(my_number)) except ValueError: print("That wasn't an integer!")

5

What will be printed when the program below is run? def add_two(x): return x + 2 def multiply_by_three(x): return x * 3 def my_function(x): return add_two(x) + multiply_by_three(x) print(my_function(12))

50

What would this program print? def mystery(num, pattern): result = "" for i in range(num): result = result + pattern return result print(mystery(3, "<>"))

<><><>


Related study sets

Permit Test Questions أسئلة اختبار التصريح

View Set

Chapter 46: Assessment and Management of Patients with Diabetes

View Set

MTA 98-364 Practice Exam Questions (1/2/3)

View Set

Ch 18: Caring for Pts with Cancer

View Set

Topic 9 Quick Check - Forecasting & Integrated Business Planning

View Set

Chapter 44: Nursing Care of the Child with an Alteration in Mobility/Neuromuscular or Musculoskeletal Disorder

View Set