Final

Ace your homework & exams now with Quizwiz!

Which mode specifier will erase the contents of a file if it already exists and creates it if it does not exist?

'w'

What will be the output after the following code is executed? def some_func(a, b): c = a, ", ", b n1 = 4 n2 = 5 answer = some_func(n1, n2) print(answer)

None

What will the output after the following code is executed? def pass_it(x, y): z = x, " , " , y num1 = 3 num2 = 9 answer = pass_it(num1, num2) print(answer)

None

Different functions can have local variables with the same names.

True

If a whole paragraph is included in a single string, the split() method can be used to obtain a list of sentences in the paragraph

True

In python you can have a list of variables on the left side of the argument operator

True

It is possible to create a while loop that determines when the end of a file has been reached.

True

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

True

Strings can be written directly to a file with the write method, but numbers must be converted to strings before they can be written.

True

what will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} for key,value in stuff: print(key,value)

TypeError

What will the following code display? x = [4, 6, 10, 12) y = ([6, 7, 8, 9]) print(x ^ y)

TypeError?

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]

[1, 2, 3]

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

[1, 3, 5, 7]

What is the equivalent list comprehension for the following? sq_list = [ ] for x in range(1, 11): if x%2 == 0: sq_list.append(x * x)

[4, 16, 36, 64, 100]

what does the following code display? numbers = [1, 2, 3, 4, 5, 6, 7, 8] print(numbers[-4])

[5, 6, 7, 8]

A value-returning function is

a function that will return a value back to the part of the program that called it

Look at the following function definition: 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 4 into a, 2 into b, and 6 into c. b) What value will be displayed when the function call executes?

a) my_function(a=4, b=2, c=6) b) 5

what is the result of the following boolean expression, given that a = 8, b = 5, and c = 3? (a < b or c > a) and b > c

false

The process known as the _____ cycle is used by the CPU to execute instructions in a program.

fetch-decode-execute

what does the following statement mean? a1, a2 = get_num()

get_num() is expected to return a value for each a1 and a2

A ______ variable is accessible to all the functions in a program file.

global

If this start index is ______ the end index, the slicing expression will return an empty string.

greater than

Which of the following is the correct if clause to determine whether y is in the range 0 through 25, inclusive.

if y >= 0 and y <= 25

Python provides a special version of a decision structure known as the _______________ statement, which makes the logic of the nested decision structure simpler to write

if-elif-else

What does the get method do if the specified key is not found in the dictionary?

it returns a default value

which method would you use to get all the elements in a dictionary returned as a list of tuples?

items

what will be the output after the following code is executed and the user enters 16 and -2 as the first two prompts? def main(): try: total = int(integer("Enter total cost of items? ") num_items = int(input("Number of items ")) average = total / num_items except ZeroDivisionError: print ('Error: cannot have 0 items') except ValueError: print('Error: number of items cannot be negative') main()

nothing

which of the following will display 123,456.78?

nothing?

Which of the following will assign a random integer in the range of 1 through 50 to the variable number?

number = random.randint(1, 50)

Assume the list numbers1 has 100 elements, and numbers2 is an empty list. Write code that copies the values in numbers1 to numbers2

numbers1 = list(range(1, 101)) numbers2 = [] numbers2 += numbers1 numbers2 == numbers1

The primary difference between a tuple and a list is that

once a tuple is created, is cannot be changed

Which step creates a connection between a file and a program?

open the file

Which logical operators perform short-circuit evaluation?

or, and

Which method would you use to get the value associated with a specific key and remove that key-value pair from the dictionary?

pop

What is an advantage of using a tuple rather than a list?

processing a tuple is faster

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

readline

The ____ of a local variable is the function in which that variable is created

scope

The _____ of a local variable is the function in which that variable is created

scope

What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.)cities = {'GA' : 'Atlanta', 'NY' : 'Albany', 'CA' : 'San Diego'}if 'FL' in cities:del cities['FL']cities['FL'] = 'Tallahassee'print(cities)

{'GA': 'Atlanta', 'FL': 'Tallahassee', 'NY': 'Albany', 'CA': 'San Diego'}

Which method or operator can be used to concatenate lists?

+

What is the first negative index in a list?

-1

What are the valid indexes for the string 'New York'?

0 through 7

what will the following program display? def main() x = 1 y = 2 print(x, y) change_x_y(x,y) print(x,y) def change_x_y(x,y) x = 3 y = 4 print(x,y) main()

1 2 or syntax error 3 4 1 2

A local variable can be accessed from anywhere in the program

False

The following code will display 'yes + no': mystr = 'yes' yourstr = 'no' mystr += yourstr print(mystr)

False

The following expression is valid: string[i] = 'i'

False

The index of the first element in a list is 1, the index of the second element is 2, and so forth.

False

What will be assigned to the variable some_nums after the following code executes? special = '0123456789' some_nums = special[0:10:2]

'02468'

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

'1357'

What will be assigned to the variable s_string after the following code executes? special = '1357 Country Ln.' s_string = special [-3:]

'Ln.'

Which mode specifier will open a file but will not let you change the file or write to it?

'r'

What will be displayed after the following code is executed? def pass_it(x, y): z = x*y result = get_result(z) return(result) def get_result(number): z = number + 2 return(z) num1 = 3 num2 = 4 answer = pass_it(num1, num2) print(answer)

14

What will be displayed after the following code is executed? for num in range(0, 12, 4): num += num print(num)

16

What is the decimal value of the following binary number? 00010010

18

what will be displayed after the following code is executed? total = 0 for count in range(3, 5): total += count print(total)

3 7

After the execution of the following statement, the variable price will reference the value _____ price = int(32.549)

32

what will display after the following code is executed? def main(): print("The answer is", guess(5)) def guess(num): answer = num***2 + 2**10 return answer main()

45

What will be the output after the following code is executed and the user enters 75 and 0 at the first two prompts? def main(): try:total = int(input("Enter total cost of items? ")) num_items = int(input("Number of items ")) average = total / num_items except ZeroDivisionError: print('ERROR: cannot have 0 items') except ValueError:print('ERROR: number of items cannot be negative') main()

ERROR: cannot have 0 items

the following expression is valid x = 'Country' ? = ? x[?] = '?'

Error

What will display after the following code executes? password = 'ILOVEPYTHON' if password.isalpha(): print('Invalid, must contain one number.') elif password.isdigit(): print('Invalid, must have one non-numeric character.') elif password.isupper(): print('Invalid, cannot be all uppercase characters.') else: print('Your password is secure!')

Invalid, must contain one number.

What does the following program do? student = 1 while student <= 3: total = 0 for score in range(1, 4): score = int(input("Enter test score: ") total += score average = total/3 print("Student ", student, "average: ", average) student += 1

It accepts 3 test scores for each 3 students and outputs the average for each student.

what does the following program do? import turtle def main() turtle.hideturtle() shape(100, 0, 50, 'red') def shape[x, y, radius, ):

It draws a red cirlce at the coordinates (100,0) 50 pixels wide, in the lower left corner

What will be the output after the following code is executed? def pass_it(x_y): z=x + " , " + y return(z) name2 = "Sue" name1 = "Smith" fullname = pass_it(name1, name2) print(fullname)

Smith, Sue

What will be the output after the following code is executed?import matplotlib.pyplot as pltdef main(): x_crd = [0, 1 , 2, 3, 4, 5] y_crd = [2, 4, 5, 2] plt.plot(x_crd, y_crd) main()

Syntax error when calling the plot function

What does the following statement mean? num1, num2 = get_num()

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

what is the output of the following print statement? print('The path is D:\\sample \\test.' )

The path is D:\sample\test.

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

['03', '07', '2018']

What will be the value of the variable list after the following code executes? list = [1, 2] list = list * 3

[1, 2, 1, 2, 1, 2]

What will be the value of the variable list after the following code executes? list = [1, 2, 3, 4] list[2] = 10

[1, 2, 10, 4]

what will be displayed after the following code is executed? count = 2 while count < 11: print('blank') count = count + 4

blank blank blank

Assume list1 references a list. After the following statement executes, list1 and list2 will reference two identical but separate lists in memory: list2 = list1

false

A(n) _____ structure is a logical design that controls the order in which a set of statements execute

control

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?

customer.write('Mary Smith')

in python there is nothing that can be done if the program tries to access a file to read that does not exsist

false

The following statement calls a function named half, which returns a value that is half of the argument result = half(number)

def half(value): return value /2.0

what is the number of the first index in a dictionary

dictionaries are not indexed by number

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

false

which of the following is the correct way to open a file named users.txt in 'r' mode?

infile = open('users.txt', 'r')

Which method can be used to place an item at a specific index in a list?

insert

The python _____ is a program that can read Python programming statements and execute them.

interpreter

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

list

A ____ variable is created inside a function

local

Where does a computer store a program and the data that the program is working with while the program is running?

main memory

assume the following statement appears in a program mylist = [ ] which of the following statements would you use to add the string ' ' to the list at index 0

mylist.insert(0, 'insert string')

look at the following statements: my_string = 'test1<test2<test3<test4<test5' write a statement that splits this string, creating the following line.

new_string = my_string.split('<')

Which of the following is associated with a specific file and provides a way for the program to work with that file?

the file object

This string method returns true if a string contains only alphabetic characters and is at least one character in length.

the isalpha method

In slicing if the end index specifies a position beyond the end of the list. Python will use the length of the list instead.

true

which method can used to add a group of elements to a set?

update

write code that prompts the user to enter a number between 0 and 10 and validates the input.

user_input = int(input('Enter a number between 0 and 10: ')) while user_input < 1 or 10 < user_input: print(user_input, 'is not between 1 and 10. Try again.') user_input = int(input('Enter a number between 0 and 10: '))

Which of the following statements creates a tuple?

value = (1,)


Related study sets

BAM410 - ORGANIZATIONAL THEORY AND BEHAVIOR - Unit Exam 4

View Set

Stats Test 3 (Ch. 12-14) Class Notes

View Set

Medical Terminology Chapter 9 Nervous System LO 9.8 Epilepsy

View Set

Intermediate Accounting Chapter 15

View Set

Chapter 21 Learnsmart Assignment

View Set