Chapter 5 and 6 Flashcards

1
Q
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()
A

1 3.4
0 0
1 3.4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

def class_function(a, b, c)

         d = (a+c) /b

         print(d)
A
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?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will be the value displayed when this function call is executed?

A

2.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Look at the following function header:
def my_function(a, b, c):
A

my_function(3, 2, 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
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?
A

3 passed to a
2 passed to b
1 passed to c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a statement that generates a random number in the range of 1 through 100 and assigns it to a
variable named rand.

A

rand = random.randint(1,100)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A
def half (number):
return number/2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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.
A

result=cube(4)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a function named times_ ten that accepts

a number as an argument.

A

def times_ten(number):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When the function is called, it should return the value of

its argument multiplied times 10.

A

return (number) *10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

get_first_name that asks the

user to enter his or her first name, and returns it.

A
def get_first_name():
first_name=input('Enter your first name: ')
return(first_name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

def main():

print("The answer is", magic(5))

def magic(num):

answer = num + 2 * 10

return answer

main()

A

The function get_num() is expected to return a value for num1 and for num2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A function definition specifies what a function does and causes the function to execute.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A local variable can be accessed from anywhere in the program.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
def main():
print("The answer is", magic(5))
def magic(num):
answer = num + 2 * 10
return answer
main()
A

25

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A function definition specifies what a function does and causes the function to execute.

A

false

17
Q

def main():

print("The answer is", magic(5))

def magic(num):

answer = num + 2 * 10

return answer

main()

A

25

18
Q
def pass_it(x, y):
z = x + ", " + y
return(z)
name2 = "Tony"
name1 = "Gaddis"
fullname = pass_it(name1, name2)
print(fullname)
A

Gaddis, Tony

19
Q

How many types of files are there to be used in a program?

A

two

text , binary

20
Q

Which method could be used to strip specific characters from the end of a string?

A

fstrip

21
Q

which method can be used to convert a tuple to a list?

A

list

22
Q

if the start index is _______ the end index, the slicing expression will return an empty sting

A

less than or equal

23
Q
  1. Which method could be used to strip specific characters from the end of a string?
A

rstrip(), readline(), read()

24
Q
  1. Which method will return an empty string when it has attempted to read beyond the end of a file?
A

ANS: readline()

25
Q
  1. Which statement can be used to handle some of the runtime errors in a program?
A

ANS: try/except

26
Q
  1. 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?
A

ANS: customer.write(‘Mary’ Smith’)

27
Q

The remove method removes all occurrences of an item from a list

A

False

Removes the first occurrence of item in the list.

28
Q

A list cannot be passed as an argument to a function.

A

FALSE

A list can be passed as an argument to a function
29
Q

Lists are dynamic data structures such that items may be added to them or removed from them.

A

True

30
Q

Which list will be referenced by the variable number after the following code is executed? Number = range(0,9,2)

A

ANS: [0, 2, 4, 6, 8]

31
Q

What will be the value of the variable list after the following code executes?

list = [1, 2]
list = list * 3
A

ANS: [1, 2, 1, 2 ,1, 2]

32
Q

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]
A

ANS: [1, 2, 3]

33
Q

What is the first negative index in a list?

A

-1

34
Q

What list will be referenced by the variable list_stripafter the following code executes?
my_string = ‘03/07/2018’
list_strip = my_string.split(‘/’)

A

ANS: [‘03’, ‘07’, ‘2018’]

35
Q

What will be assigned to the variable s_stringafter the following code executes?
special = ‘1357 Country Ln.’
s_string = special[:4]

A

ANS: 1357

36
Q

Function names should be as short as possible.

A

False

37
Q

Calling a function and defining a function mean the same thing.

A

False

38
Q

A flowchart shows the hierarchical relationships between functions in a program.

A

False