Chapter 5 and 6 Flashcards
def main(): x = 1 y = 3.4 print(x,y) change_us(x,y) print(x,y) def change_us(a,b): a = 0 b = 0 print(a,b) main()
1 3.4
0 0
1 3.4
def class_function(a, b, c)
d = (a+c) /b print(d)
def my_function(a, b, c): d = (a + c) / b print(d) a. Write a statement that calls this function and uses keyword arguments to pass 2 into a, 4 into b, and 6 into c. b. What value will be displayed when the function call executes?
What will be the value displayed when this function call is executed?
2.0
Look at the following function header: def my_function(a, b, c):
my_function(3, 2, 1)
When this call executes def my_function(a, b, c): Now look at the following call to my_function: my_function(3, 2, 1) What value will be assigned to a? What value will be assigned to b? What value will be assigned to c?
3 passed to a
2 passed to b
1 passed to c
Write a statement that generates a random number in the range of 1 through 100 and assigns it to a
variable named rand.
rand = random.randint(1,100)
The following statement calls a function named half,
which returns a value that is half that of the argument.
(Assume the number variable references a float value.)
Write code for the function: result = half (number)
def half (number): return number/2
A program contains the following function definition: def cube (num) : return num num num Write a statement that passes the value 4 to this function and assigns its return value to the variable result.
result=cube(4)
Write a function named times_ ten that accepts
a number as an argument.
def times_ten(number):
When the function is called, it should return the value of
its argument multiplied times 10.
return (number) *10
get_first_name that asks the
user to enter his or her first name, and returns it.
def get_first_name(): first_name=input('Enter your first name: ') return(first_name)
def main():
print("The answer is", magic(5))
def magic(num):
answer = num + 2 * 10 return answer
main()
The function get_num() is expected to return a value for num1 and for num2.
A function definition specifies what a function does and causes the function to execute.
false
A local variable can be accessed from anywhere in the program.
false
def main(): print("The answer is", magic(5)) def magic(num): answer = num + 2 * 10 return answer main()
25
A function definition specifies what a function does and causes the function to execute.
false
def main():
print("The answer is", magic(5))
def magic(num):
answer = num + 2 * 10 return answer
main()
25
def pass_it(x, y): z = x + ", " + y return(z) name2 = "Tony" name1 = "Gaddis" fullname = pass_it(name1, name2) print(fullname)
Gaddis, Tony
How many types of files are there to be used in a program?
two
text , binary
Which method could be used to strip specific characters from the end of a string?
fstrip
which method can be used to convert a tuple to a list?
list
if the start index is _______ the end index, the slicing expression will return an empty sting
less than or equal
- Which method could be used to strip specific characters from the end of a string?
rstrip(), readline(), read()
- Which method will return an empty string when it has attempted to read beyond the end of a file?
ANS: readline()
- Which statement can be used to handle some of the runtime errors in a program?
ANS: try/except
- Given that the customer file references a file object, and the file was opened using the ‘w’ mode specifier, how would you write the string ‘Mary Smith’ to the file?
ANS: customer.write(‘Mary’ Smith’)
The remove method removes all occurrences of an item from a list
False
Removes the first occurrence of item in the list.
A list cannot be passed as an argument to a function.
FALSE
A list can be passed as an argument to a function
Lists are dynamic data structures such that items may be added to them or removed from them.
True
Which list will be referenced by the variable number after the following code is executed? Number = range(0,9,2)
ANS: [0, 2, 4, 6, 8]
What will be the value of the variable list after the following code executes?
list = [1, 2] list = list * 3
ANS: [1, 2, 1, 2 ,1, 2]
What will be the value of the variable list2 after the following code executes?
list1 = [1, 2, 3] list2 = [] for element in list1: list2.append(element) list1 = [4, 5, 6]
ANS: [1, 2, 3]
What is the first negative index in a list?
-1
What list will be referenced by the variable list_stripafter the following code executes?
my_string = ‘03/07/2018’
list_strip = my_string.split(‘/’)
ANS: [‘03’, ‘07’, ‘2018’]
What will be assigned to the variable s_stringafter the following code executes?
special = ‘1357 Country Ln.’
s_string = special[:4]
ANS: 1357
Function names should be as short as possible.
False
Calling a function and defining a function mean the same thing.
False
A flowchart shows the hierarchical relationships between functions in a program.
False