Functions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which correctly passes two integer arguments for the function call? calc_val(...)

(99, 44 + 5)

Given a function definition: def calc_val(a, b, c): and given variables i, j, and k, which are valid arguments in the call? calc_val(...)

(k, i + j, 99)

Which correctly defines two parameters x and y for a function definition: def calc_val(...):

(x, y)

Assume the function below is defined: def split_check(amount, num_people, tax_percentage, tip_percentage): # ... What value is passed as the *tax_percentage* argument in the function call? Answer ERROR if an error occurs. *split_check(60.52, 5, .07, tip_percentage = 0.18)*

0.07

Assume the function below is defined: def split_check(amount = 10, num_people = 2, tax_percentage = 0.095, tip_percentage = 0.18): # ... What will the parameter *tax_percentage* be assigned for the following call? Type ERROR if the call is invalid. *split_check(amount = 49.50, num_people = 3)*

0.095

The following function is defined: def split_check(amount, num_people, tax_percentage = 0.095, tip_percentage = 0.15) # ... What will the parameter *tax_percentage* be assigned for the following call? Type ERROR if the call is invalid. *split_check(65.50, 3)*

0.095

The following function is defined: def split_check(amount, num_people, tax_percentage = 0.095, tip_percentage = 0.15) # ... What will the parameter *tax_percentage* be assigned for the following call? Type ERROR if the call is invalid. *split_check(65.50, 3, 0.125)*

0.125

What does ebay_fee() return if its argument is 0.0 (show your answer in the form #.#)?

0.5

Given the following program and the print_pizza_area() function defined above: print_pizza_area() print_pizza_area() How many function definitions of print_pizza_area() exist?

1

Given the following program and the print_pizza_area() function defined above: print_pizza_area() print_pizza_area() How many print statements exist in the program code?

1

Given: def print_sum(num1, num2): print(num1, '+', num2, 'is', (num1 + num2)) What will be printed for the following function call? print_sum(1, 2)

1 + 2 is 3

Given the following program and the print_pizza_area() function defined above: print_pizza_area() print_pizza_area() How many output statements would execute in total?

2

Assume a function def print_num(user_num): simply prints the value of user_num without any space or newline. What will the following code output? print_num(43) print_num(21)

4321

For any call to ebay_fee(), how many assignment statements will execute?

5

Given a function definition: def calc_val(a, b, c):, b is assigned with what value during this function call? calc_val(42, 55, 77)

55

What does ebay_fee() return if its argument is 100.0 (show your answer in the form #.#)?

9.5

A local variable's scope extends from a function definition's ending colon ":" to the end of the function. True or False

False

A pass statement should be used in a function stub when the programmer wants the stub to stop program execution when called. True or False

False

Assignments to a parameter name inside a function affect the code outside the function. True or False

False

Avoiding redundancy means to avoid calling a function from multiple places in a program. True or False

False

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *square_root(49.0) = z*

False

If a programmer defines a function called list(), the program will crash because there is already a built-in function with the same name. True or False

False

If my_func1() and my_func2() are defined functions, then the expression my_func1 + my_func2 returns a valid value. True or False

False

The expression my_func1(my_func2()) passes the my_func2 function object as an argument to my_func1. True or False

False

A function definition must be evaluated by the interpreter before the function can be called. True or False

True

A global statement must be used to write to a global variable inside a function. True or False

True

A local variable is defined inside a function, while a global variable is defined outside any function. True or False

True

A namespace is how the Python interpreter restricts variables to a specific scope. True or False

True

A programmer can protect mutable arguments from unwanted changes by passing a copy of the object to a function. True or False

True

Adding an element to a dictionary argument in a function might affect variables outside the function that reference the same dictionary object. True or False

True

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *square_root(9.0)*

True

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *y = 1.0 + square_root(144.0)*

True

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *y = print_val(9.0)*

True

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *y = square_root(49.0)*

True

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *y = square_root(square_root(16.0))*

True

trajectory() cannot return two values (for x and y), so instead returns a single tuple containing both x and y. True or False

True

Given the following program, select the namespace that each name would belong to. import random player_name = 'Gandalf' player_type = 'Wizard' def roll(): """Returns a roll of a 20-sided die""" number = random.randint(1, 20) return number print('A troll attacks!') troll_roll = roll() player_roll = roll() print('Player: {} Troll: {}'.format(str(player_roll), str(troll_roll))) *player_name*

global

import random player_name = 'Gandalf' player_type = 'Wizard' def roll(): """Returns a roll of a 20-sided die""" number = random.randint(1, 20) return number print('A troll attacks!') troll_roll = roll() player_roll = roll() print('Player: {} Troll: {}'.format(str(player_roll), str(troll_roll))) *roll*

global

Write a function call using print_sum() to print the sum of x and 400 (providing the arguments in that order).

print_sum(x, 400)

Add a return statement to the function that returns the result of adding num1 and num2. def sum(num1, num2):

return num1 + num2

Assume the function below is defined: def split_check(amount = 10, num_people = 2, tax_percentage = 0.095, tip_percentage = 0.18): # ... Write a statement that splits a $25 check among 3 people and leaves a 25% tip. Use the default tax rate.

split_check(amount = 25.0, num_people = 3, tip_percentage = 0.25)

Assume the function below is defined: def split_check(amount = 10, num_people = 2, tax_percentage = 0.095, tip_percentage = 0.18): # ... Write a statement that splits a $50 check among 4 people. Use the default tax percentage and tip amount.

split_check(amount = 50, num_people = 4)

def pizza_calories_per_slice(pizza_diameter): total_calories = <placeholder_A> calories_per_slice = <placeholder_B> return calories_per_slice Type the expression for placeholder_B to compute the calories per slice. calories_per_slice = ?

total_calories / pizza_slices(pizza_diameter)

Complete the function definition to have a parameter named user_age. def print_age():

user_age

Given the following program and the print_pizza_area() function defined above: print_pizza_area() print_pizza_area() How many function calls to print_pizza_area() exist?

2

For a single execution of the program, how many calls to user-defined functions are made?

4

Assume the function below is defined: What value is passed as the *num_people* argument in the function call? Answer ERROR if an error occurs. *split_check(tax_percentage = .07, 60.52, 2, tip_percentage = 0.18)*

ERROR

A key reason for creating functions is to help the program run faster. True or False

False

A programmer can pass only string arguments to a user-defined function. True or False

False

Function convert_to_pres() would likely return (temp * volume) / (mols * gas_constant). True or False

False

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *y = 1 + print_val(9.0)*

False

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *y = square_root()*

False

If a function's internal statements are revised, all function calls will have to be modified too. True or False

False

The main advantage of function stubs is that they ultimately lead to faster-running programs. True or False

False

The output of the following program is 'meow': def cat(): print('meow') def pig(): print('oink') cat = pig cat() True or False

False

The program could replace float() by int() without causing much change in computed values. True or False

False

The same name cannot be in multiple namespaces. True or False

False

Is this function correct for squaring an integer? def sqr (a): t = a * a return a Yes or No

No

Incremental development may involve more frequent testing, but ultimately leads to faster development of a program. True or False

True

Returning the incorrect variable from a function is a common error. True or False

True

Static-typed languages require that the type of every variable is defined in the source code. True or False

True

The statement *return a, b, [c, d]* is valid. True or False

True

Whenever a function is called, a local namespace is created for that function. True or False

True

Add a return statement to the function that returns the cube of the argument, i.e. (num num num). def cubed(num):

return num * num * num

Complete the function call so that the output of the program is John is: age: 10 gender: m *def* stats(name, **info): *print*(name, 'is:') *for* key, value *in* info.items(): *print*('{}: {}'.format(key, value)) stats()...?

'John', age = 10, gender = 'm'

The following function is defined: def split_check(amount, num_people, tax_percentage = 0.095, tip_percentage = 0.15) # ... What will the parameter *num_people* be assigned for the following call? Type ERROR if the call is invalid. *split_check(12.50, tip_percentage = 0.18)*

ERROR

The following function is defined: def split_check(amount, num_people, tax_percentage = 0.095, tip_percentage = 0.15) # ... What will the parameter *num_people* be assigned for the following call? Type ERROR if the call is invalid. *split_check(tip_percentage = 0.18, 12.50, 4)*

ERROR

A benefit of functions is to increase redundant code. True or False

False

A function may return multiple objects. True or False

False

Each iteration of the loop will see y_loc increase.

False

When a function is called, copies of all the argument objects are made. True or False

False

Is the following a valid function definition beginning? Type yes or no. def my_fct(userNum + 5): Yes or No

No

Is this function correct for squaring an integer? def sqr (a): t = a * a Yes or No

No

A dynamic-typed language like Python checks that an operation is valid when that operation is executed by the interpreter. If the operation is invalid, a run-time error occurs. True or False

True

Assuming the launch angle is less than 90 degrees, each iteration of the loop will see x_loc increase.

True

Copying-and-pasting code can lead to common errors if all necessary changes are not made to the pasted code. True or False

True

Forgetting to return a value from a function is a common error. True or False

True

Function convert_to_temp uses a global variable for the gas constant R. True or False

True

Function convert_to_temp uses a rewritten form of PV = nRT to solve for T, namely T = PV/nR. True or False

True

Functions are compiled into bytecode when the function definition is evaluated by the interpreter. True or False

True

Given the following functions, determine which statements are valid. def square_root(x): return math.sqrt(x) def print_val(x): print(x) *print_val(9.0)*

True

Polymorphism refers to how an operation depends on the involved object types. True or False

True

Complete the first line of the function definition for f() requiring two arguments arg1 and arg2, and an arbitrary argument list *args. def function_name():

arg1, arg2, *args

import random player_name = 'Gandalf' player_type = 'Wizard' def roll(): """Returns a roll of a 20-sided die""" number = random.randint(1, 20) return number print('A troll attacks!') troll_roll = roll() player_roll = roll() print('Player: {} Troll: {}'.format(str(player_roll), str(troll_roll))) *str*

built-in

import random player_name = 'Gandalf' player_type = 'Wizard' def roll(): """Returns a roll of a 20-sided die""" number = random.randint(1, 20) return number print('A troll attacks!') troll_roll = roll() player_roll = roll() print('Player: {} Troll: {}'.format(str(player_roll), str(troll_roll))) *number*

local

Write a function call using the ebay_fee() function to determine the fee for a selling price of 15.23, storing the result in a variable named my_fee.

my_fee = ebay_fee(15.23)

def pizza_calories_per_slice(pizza_diameter): total_calories = <placeholder_A> calories_per_slice = <placeholder_B> return calories_per_slice Type the expression for placeholder_A to compute the total calories for a pizza with diameter pizza_diameter. total_calories = ?

pizza_calories(pizza_diameter)

Call a function named print_age, passing the value 21 as an argument.

print_age(21)


Kaugnay na mga set ng pag-aaral

Chapter 52: Assessment and Management of Patients with Endocrine Disorders NCLEX

View Set

Florida Traffic School Online (4-Hour BDI) Exam Answers

View Set

Cancer Screening in Primary Care

View Set

TEO-141 Teradata 14 Basics - Personal

View Set

Physical Science Exam #1 Study Guide

View Set

Prep U Chapter 34: Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set