Computer science functions.

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following options is NOT a valid Python function definition?

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

Q. ___________ are the arguments passed to a function in correct positional order.

Required arguments

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

def myFunction(x,y) return x + y myFunction(3,2) Q. What will print?

nothing

Question: 14 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())

Q. Python supports variable number of parameters.

True

def say(messafe, times = 1) print(message * times) say("hello") say ("World", 5) Q. times is ________________ type of argument.

default arguments

data = 10 def my_function(): print(data) data = 20 print(data) print(data) Q. What is the output of the given code?

10

data = 10 def my_function(): data = 20 print(data) print(data) my_function() print(data) Q. What is the output of the given code?

10 20 10

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

<><><>

def study(x, y) print(x+y*2) study(1,2)

5

Q. __________ is the built-in function to know the type of a variable or function.

type()

def myFunction(x,y) return x + y z = myFunction(3, myFunction(4,5)) print(z)

12

def suprise(): print("Boo") for x in range(1,5): suprise()

4

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

def myFunction(x,y) return x + y myFunction(3,2) print(2)

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

Q. How do you rate the CRT sessions? a.Excellent b.Good c.Average d.Poor

A) Excellent

Q. Function header in Python contains _________________. A)function name B)list of parameters C)return type D)all the above E)Only A & B

E)Only A & B

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

Q. The default return value of any function in Python is _______________.

None

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

Q. What term is used to describe data passed into a function?

Parameter

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

Q. Select the advantages of functions a. Modular programming b. Code re-usability c.Improving clarity of the code d.All the above

d.All the above

Q. __________ keyword is used for creating a function in Python.

def

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")

Q. Leo wants to create a function that will roll a dice. Which is the correct function definition header?

def diceroll ():

Which of the following programs will NOT cause an error?

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_a(i)

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


Ensembles d'études connexes

Ch. 21 Disorders of Oxygen Transport

View Set

Limited Partnerships and Limited Liability Partnerships

View Set

Mastering Biology: The Immune System

View Set

Nama surat Al-Qur'an dan arti [51-60]

View Set

Locke- Essay Concerning Human Understanding

View Set

Psychology 111 - Exam 3 - Chapter 5 and 6 - Full Review

View Set

Hard to Remember Questions for INTRO

View Set

Disaster FEMA: Introduction to ICS Lessons 5 & 6

View Set

vSim Fundamentals | Edith Jacobson (Patient Safety, Fall Risks, & the Older Adult)

View Set

Econ 201 Homework: Ch. 3: Demand and Supply

View Set