COSC 1306 - 12. Set 3: Functions

Ace your homework & exams now with Quizwiz!

False

A benefit of functions is to increase redundant code.

True

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

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

False

A function may return multiple objects.

False

A global statement must be used whenever a global variable is to be read or written in a function.

False

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

True

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

False Not from the colon but rather from the variable's assignment.

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

True

A name space is how the Python interpreter restricts variable to a specific scope.

False

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

False

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

return num*num*num

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

num1 + num2

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

True

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

4321

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

False

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

print_age(21)

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

def f( arg1, arg2, *args):

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

status( 'John', age = 10, gender 'm')

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, valie in info.items(): print('{}: {}'.format(key, value)) status(_________)

user_age

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

True

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

True

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) print_val(9.0)

False

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) square_root(49.0) = z

True

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) square_root(9.0)

False

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) y = 1 + print_val(9.0)

True

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) y = 1.0 + square_root(144.0)

True

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) y = print_val(9.0)

True

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) y = square_root(49.0)

True

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) y = square_root(square_root(16.0))

True

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

1 + 2 is 3

Given def print_sum(num1, num2): print(num1, '+', num2, 'is', (num1 + num2))

(k, i + j, 99)

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(. . .)?

55

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

False

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

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.

False Function objects don't support most operators.

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

True

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

No (False)

Is the following a valid function definition beginning? def my_fct(userNum + 5):

No

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

No

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

True

Polymorphism refers to how an operation depends on the involved object types

True

Returning the incorrect variable from a function is a common error.

True

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

False

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

False The statement cat = pig reassigns the variable cat to the pig function object. The call cat() thus calls the referenced object, which is equivalent to pig().

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

False

The same name cannot be in multiple namespaces.

False

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

True

Whenever a function is called, a local namespcae is created for that function.

(x, y) The comma separates the two parameters

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

(99, 44 + 5) Each argument evaluates to an integer

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

print_sum(x, 400)

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

my_fee = ebay_fee(15.23)

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.

pizza_calories(pizza_diameter)

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 = _____

total_calories / pizza_slices(pizza_diameter)

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 = ____

0.095

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? splich_check(amount = 49.50, num_people = 3)

split_check(25, 3, tip_percentage = 0.25) or split_check(amount = 25, num_people = 3, tip_percentage = 0.25)

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

split_check(50, 4)

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 and tip amount.

Error split_check() requires at least the amount and num_people arguments

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? split_check(12.50, tip_percentage = 0.18)

Error Keyword arguments must come after positional arguments

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? split_check(tip_percentage = 0.18, 12.50, 4)

0.095

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? split_check(65.50, 3)

0.125 (fraction)

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? split_check(65.50, 3, 0.125)

Error All keyword arguments must come after every positional argument

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

0.07

def split_check(amount, num_people, tax_percentage, tip_percentage): What value is passed as the tax_percentage argument in split_check(60.52, 5, .07, tip_percentage = 0.18)

True

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

False

Assignment to a parameter name inside a function affect the code outside the function.

False

Determine which statement is valid, given def saquare_root(x): return math.sqrt(x) def print_val(x): print(x) y = square_root()

True

Forgetting to return a value from a function is a common error.

False

The expression my_func1(my_func2()) passes the my_func2 function object as an argument to my_func1


Related study sets

Chapter 17 - Neurologic Emergencies

View Set

Social Studies test Study Guide 3

View Set

NCLEX Neurologic and Sensory Systems

View Set

POLS 2301 FINAL LAMAR UNIVERSITY DR. SOWERS

View Set

Section 12.1 - DNA, Genes, & Chromosomes

View Set