Set 3 Zybooks COSC 1306

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Determine the output of the following code: my_str = 'The cat in the hat' print(my_str[3:7])

''cat '' means there is a space there

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('%s: %s' % (key, value)) stats(_____________________)

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

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)

What is the value of my_str after the following statements are evaluated: my_str = 'http://reddit.com/r/python' my_str = my_str[17:]

/r/python

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

0.07

Assume the function below is defined: def split_check(amount=10, num_people=2, tax_rate=0.095, tip_rate=0.18): # ... When entering answers, use the same number of significant digits as the default parameter values in the split_check() definition. What will the parameter tax_rate 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_rate=0.095, tip_rate=0.15) # ... What will the parameter tax_rate 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_rate=0.095, tip_rate=0.15) # ... What will the parameter tax_rate 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

What is the output of each print? print('%05d' % 75.55)

00075

What is the output of each print? print('%05d' % 150)

00150

What is the output of each print? print('%05.1f' % 75.55)

075.5

Given the following program, and the definition of print_face() above: print_face() print_face() How many function definitions of print_face are there?

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 definition of print_face() above: print_face() print_face() How many function calls to print_face are there?

2

Given the following program, and the definition of print_face() above: print_face() print_face() How many print statements exist in the program code?

3

For a single execution of the program, how many user-defined method calls execute?

4

Assume a function def print_num(user_num): simply prints the value of user_num without any space or newline. What will the following 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): what value is assigned to b during this function call: calc_val(42, 55, 77)

55

Given the following program, and the definition of print_face() above: print_face() print_face() How many print statements would execute in total?

6

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

9.5

Assume the variable my_str is 'Agt2t3afc2kjMhagrds!'. What is the result of the expression my_str[0:5:1]?

Agt2t

Assume the variable my_str is 'Agt2t3afc2kjMhagrds!'. What is the result of the expression my_str[::2]

AttackMars

Assume that the variable song has the value: song = "I scream; you scream; we all scream, for ice cream.\n" What is the result of song.split()? A. ['I scream; you scream; we all scream, for ice cream.\n'] B. ['I scream; you scream;', 'we all scream,', 'for ice cream.\n'] C. ['I', 'scream;', 'you', 'scream;', 'we', 'all', 'scream,' 'for', 'ice', 'cream.']

C. ['I', 'scream;', 'you', 'scream;', 'we', 'all', 'scream,' 'for', 'ice', 'cream.']

Assume the function below is defined: def split_check(amount, num_people, tax_rate, tip_rate): # ... What value is passed as the num_people argument in the statement split_check(60.52, tax_rate=.07, 2, tip_rate=0.18)? Answer ERROR if an error occurs.

ERROR

The following function is defined: def split_check(amount, num_people, tax_rate=0.095, tip_rate=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_rate=0.18)

ERROR

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

ERROR

'1 2 3 4 5'.isdigit() True or False

False

'HTTPS://google.com'.isalnum() True or False

False

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

False

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

False

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

A programmer can pass only string arguments to a user-defined function. 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

Each iteration of the loop will see y_loc increase. True or False

False

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return y = 1 + print_val(9.0) True or False

False

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return y = square_root() True or False

False

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return which of the following are valid statements? square_root(49.0) = z True or False

False

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

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

The main advantage of function stubs is that they ultimately lead to faster-running programs. 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 can not be in multiple namespaces. True or False

False

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

False

as_constant = 8.3144621 # Joules / (mol*Kelvin) def convert_to_temp(pressure, volume, mols): """Convert pressure, volume, and moles to a temperature""" return (pressure * volume) / (mols * gas_constant) press = float(input('Enter pressure (in Pascals): ')) vol = float(input('Enter volume (in cubic meters): ')) mols = float(input('Enter number of moles: ')) print('Temperature = %02f K' % convert_to_temp(press, vol, mols)) Function convert_to_pres() would likely return (temp * volume) / (mols * gas_constant). True or False

False

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

False

A function may return multiple objects. True or False

Fase

What is the output of each print? print('%4s %08d' % ('ID:', 860552))

ID: 00860552

Determine the output of the following code: my_str = 'The cat in the hat' print(my_str[0:3])

The

'HTTPS://google.com'.startswith('HTTP') True or False

True

'LINCOLN, ABRAHAM'.isupper() True or False

True

'\n \n'.isspace() True or False

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 or False

True

A function definition must be evaluated by the interpreter before the function can be called. 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

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

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

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

True

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return print_val(9.0) True or False

True

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return square_root(9.0) True or False

True

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return y = 1.0 + square_root(144.0) True or False

True

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return y = print_val(9.0) True or False

True

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return y=square_root(square_root(16.0)) True or False

True

Given: def square_root(x): return math.sqrt(x) def print_val(x): print(x) return which of the following are valid statements? y = square_root(49.0) True or False

True

If a function creates a local variable that has the same name as a parameter of that function, the name will refer to the local variable. True or False

True

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

True

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

True

Returning the incorrect variable from a function is 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

as_constant = 8.3144621 # Joules / (mol*Kelvin) def convert_to_temp(pressure, volume, mols): """Convert pressure, volume, and moles to a temperature""" return (pressure * volume) / (mols * gas_constant) press = float(input('Enter pressure (in Pascals): ')) vol = float(input('Enter volume (in cubic meters): ')) mols = float(input('Enter number of moles: ')) print('Temperature = %02f K' % convert_to_temp(press, vol, mols)) Function convert_to_temp uses a rewritten form of PV = nRT to solve for T, namely T = PV/nR. True or False

True

gas_constant = 8.3144621 # Joules / (mol*Kelvin) def convert_to_temp(pressure, volume, mols): """Convert pressure, volume, and moles to a temperature""" return (pressure * volume) / (mols * gas_constant) press = float(input('Enter pressure (in Pascals): ')) vol = float(input('Enter volume (in cubic meters): ')) mols = float(input('Enter number of moles: ')) print('Temperature = %02f K' % convert_to_temp(press, vol, mols)) Function convert_to_temp uses a global variable for the gas constant R. True or False

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

Complete the function definition requiring two arguments arg1 and arg2, and an arbitrary argument list args. def f(_____________): # ...

arg1, arg2, *args or arg2, arg1, *args

Given the following program, select the namespace that each name would belong to. str local global built-in

built-in

Given the following program, select the namespace that each name would belong to. player_name local global built-in

global

Given the following program, select the namespace that each name would belong to. roll local global built-in

global

Given the following program, select the namespace that each name would belong to. number local global built-in

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)

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

num1 + num2

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

print_age(21)

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)

What is the value of my_str after the following statements are evaluated: my_str = 'http://reddit.com/r/python' protocol = 'http://' my_str = my_str[len(protocol):]

reddit.com/r/python

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

return num*num*num

Assume the function below is defined: def split_check(amount=10, num_people=2, tax_rate=0.095, tip_rate=0.18): # ... When entering answers, use the same number of significant digits as the default parameter values in the split_check() definition. Write a statement that splits a $25 check amongst 3 people, and leaves a 25% tip. Use the default tax rate.

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

Assume the function below is defined: def split_check(amount=10, num_people=2, tax_rate=0.095, tip_rate=0.18): # ... When entering answers, use the same number of significant digits as the default parameter values in the split_check() definition. Write a statement that splits a $50 check amongst 4 people. Use the default tax rate and tip amount.

split_check(amount=50, num_people=4)

Write a statement that assigns str2 with a copy of str1.

str2 = str1[:]

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

user_age


संबंधित स्टडी सेट्स

Algorithms & Data Structures,ata Structures and Algorithms Midterm, Data Structures and Algorithms FINAL, Data Structure And Algorithms, Data Structures and Algorithms, Algorithms & Data Structures, Dictionary of Algori

View Set

Psych - Ch. 16 Therapy - Prep: Learning Curve

View Set

Amazon Cloud Practitioner Quiz Questions

View Set