Intro to Python - Final Exam Prep

¡Supera tus tareas y exámenes ahora con Quizwiz!

What will be assigned to the variable s_string after the following code executes? special = '1357 Country Ln.' s_string = special[4:] a) ' Country Ln.' b) 'Coun' c) '1357' d) '57 C'

a) ' Country Ln.'

What will be assigned to the variable some_nums after the following code executes? special = '0123456789' some_nums = special[0:10:2] a) '02468' b) '24682468' c) '0123456789' d) '02020202020202020202'

a) '02468'

Which mode specifier will erase the contents of a file if it already exists and create the file if it does not already exist? a) 'w' b) 'a' c) 'e' d) 'r'

a) 'w'

What are the valid indexes for the string 'New York'? a) 0 through 7 b) 0 through 8 c) -1 through -8 d) -1 through 6

a) 0 through 7

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) a) 14 b) 12 c) 9 d) Nothing, this code contains a syntax error

a) 14

What is the decimal value of the following binary number? 10011101 a) 157 b) 156 c) 8 d) 28

a) 157

What are the values that the variable num contains through the iterations of the following for loop? for num in range(2, 9, 2): a) 2, 4, 6, 8 b) 1, 3, 5, 7, 9 c) 2, 5, 8 d) 2, 3, 4, 5, 6, 7, 8, 9

a) 2, 4, 6, 8

What will display after the following code is executed? def main(): print("The answer is", magic(5)) def magic(num): answer = num + 2 * 10 return answer main() a) 25 b) 100 c) 70 d) The statement will cause a syntax error.

a) 25

What is the largest value that can be stored in one byte? a) 255 b) 8 c) 65535 d) 128

a) 255

What will be displayed after the following code is executed? total = 0 for count in range(1,4): total += count print(total) a) 6 b) 1 3 6 c) 1 4 d) 5

a) 6

What will be the output after the following code is executed? def pass_it(x, y): z = y**x return(z) num1 = 3 num2 = 4 answer = pass_it(num1, num2) print(answer) a) 64 b) 12 c) 81 d) None

a) 64

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() a) ERROR: cannot have 0 items b) 0 c) ERROR: number of items can't be negative d) Nothing; there is no print statement to display average.

a) ERROR: cannot have 0 items

What will be the output after the following code is executed? def pass_it(x, y): z = x + ", " + y return(z) name2 = "Tony" name1 = "Gaddis" fullname = pass_it(name1, name2) print(fullname) a) Gaddis, Tony b) Tony, Gaddis c) Gaddis Tony d) Tony Gaddis

a) Gaddis, Tony

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!') a) Invalid, must contain one number. b) Invalid, must contain one number. Invalid, cannot be all uppercase characters. c) Invalid, must have one non-numeric character. d) Your password is secure!

a) 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 a) It accepts 3 test scores for each of 3 students and outputs the average for each student. b) It accepts 4 test scores for 2 students, then averages and outputs all the scores. c) It accepts 4 test scores for 3 students and outputs the average of the 12 scores. d) It accepts one test score for each of 3 students and outputs the average of the 3 scores.

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

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) [1, 2, 3] b) [1, 2, 3, 4, 5, 6] c) [4, 5, 6] b) Nothing; this code is invalid

a) [1, 2, 3]

What symbol is used to mark the beginning and end of a string? a) a quote mark (") b) an asterisk (*) c) a comma (,) d) a slash (/)

a) a quote mark (")

Which method would you use to determine whether a certain substring is the suffix of a string? a) endswith(substring) b) startswith(substring) c) replace(string, substring) d) find(substring)

a) endswith(substring)

The process known as the ________ cycle is used by the CPU to execute instructions in a program. a) fetch-decode-execute b) decode-fetch-execute c) fetch-execute-decode d) decode-execute-fetch

a) fetch-decode-execute

After the execution of the following statement, the variable sold will reference the numeric literal value as (n) ________ data type. sold = 256.752 a) float b) currency c) str d) int

a) float

Which of the following is the correct if clause to determine whether y is in the range 10 through 50, inclusive? a) if y >= 10 and y <= 50: b) if y >= 10 or y <= 50: c) if 10 > y and y < 50: d) if 10 < y or y > 50:

a) if y >= 10 and y <= 50:

Which of the following statements causes the interpreter to load the contents of the random module into memory? a) import random b) upload random c) load random d) download random

a) import random

Which of the following will assign a random integer in the range of 1 through 50 to the variable number? a) number = random.randint(1, 50) b) random(1, 50) = number c) number = random(range(1, 50)) d) randint(1, 50) = number

a) number = random.randint(1, 50)

Which of the following is the correct way to open a file named users.txt to write to it? a) outfile = open('users.txt', 'w') b) outfile = open('users.txt') c) outfile = open('w', users.txt) d) outfile = write('users.txt', 'w')

a) outfile = open('users.txt', 'w')

Which of the following will display 20%? a) print(format(0.2, '.0%')) b) print(format(0.2, '%')) c) print(format(0.2 * 100, '.0%')) d) print(format(20, '.0%'))

a) print(format(0.2, '.0%'))

Which method will return an empty string when it has attempted to read beyond the end of a file? a) readline b) getline c) read d) input

a) readline

Which method can be used to convert a list to a tuple? a) tuple b) list c) append d) insert

a) tuple

Select all that apply. Which of the following are steps in the program development cycle? a) correct logic errors b) design the program c) write the code and correct syntax errors d) test the program

a,b,c,d

What will be assigned to the variable s_string after the following code executes? special = '1357 Country Ln.' s_string = special[ :4] a)'7 Country Ln.' b) '1357' c) '7' d) 5

b) '1357'

What is the first negative index in a list? a) -0 b) -1 c) 0 d) the size of the list minus 1

b) -1

Which language is referred to as a low-level language? a) Java b) Assembly language c) C++ d) Python

b) Assembly language

What is the number of the first index in a dictionary? a) 0 b) Dictionaries are not indexed by number. c) the size of the dictionary minus one d) 1

b) Dictionaries are not indexed by number.

What will be the output after the following code is executed? import matplotlib.pyplot as plt def main(): x_crd = [0, 1 , 2, 3, 4, 5] y_crd = [2, 4, 5, 2] plt.plot(x_crd, y_crd) main() a) It will display a simple line graph. b) Nothing; the number of x-coordinates do not match the number of y-coordinates. c) It will display a simple bar graph. d) Nothing; plt is not a Python method.

b) Nothing; the number of x-coordinates do not match the number of y-coordinates.

What is an advantage of using a tuple rather than a list? a) Tuples are not limited in size. b) Processing a tuple is faster than processing a list. c) There is never an advantage to using a tuple. d) Tuples can include any data as an element.

b) Processing a tuple is faster than processing a list.

What is the result of the following Boolean expression, given that x = 5, y = 3, and z = 8? x < y or z > x a) 8 b) True c) False d) 5

b) True

Which list will be referenced by the variable number after the following code is executed? number = range(0, 9, 2) a) [1, 3, 5, 7, 9] b) [0, 2, 4, 6, 8] c) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] d) [2, 4, 6, 8]

b) [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 a) [1, 2], [1, 2], [1, 2] b) [1, 2, 1, 2, 1, 2] c) [1, 2] * 3 d) [3, 6]

b) [1, 2, 1, 2, 1, 2]

The smallest storage location in a computer's memory is known as a a) switch b) bit c) ketter d) byte

b) bit

Which method would you use to determine whether a certain substring is present in a string? a) startswith(substring) b) find(substring) c) endswith(substring) d) replace(string, substring)

b) find(substring)

If the start index is ________ the end index, the slicing expression will return an empty string. a) less than or equal to b) greater than c) equal to d) less than

b) greater than

Which of the following is the correct if clause to determine whether choice is anything other than 10? a) if not(choice < 10 and choice > 10): b) if choice != 10: c) if choice != 10 d) if choice <> 10:

b) if choice != 10:

In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the ________ operator. a) included b) in c) isnotin d) isin

b) in

Which method would you use to get all the elements in a dictionary returned as a list of tuples? a) pop b) items c) keys d) list

b) items

Which method can be used to convert a tuple to a list? a) append b) list c) tuple d) insert

b) list

Which method could be used to convert a numeric value to a string? a) num b) str c) chr d) value

b) str

When will the following loop terminate?while keep_on_going != 999: a) when keep_on_going refers to a value less than 999 b) when keep_on_going refers to a value equal to 999 c) when keep_on_going refers to a value greater than 999 d) when keep_on_going refers to a value not equal to 999

b) when keep_on_going refers to a value equal to 999

What will be the value of the variable string after the following code executes? string = 'abcd' string.upper() a) 'abcd' 'b) Abcd' c) 'ABCD' d) Nothing; this code is invalid

c) 'ABCD'

What will be the value of the variable string after the following code executes? string = 'Hello' string += ' world!' a) Nothing; this code is invalid b) ' world!' c) 'Hello world!' d) 'Hello'

c) 'Hello world!'

What will be assigned to the variable s_string after the following code executes? special = '1357 Country Ln.' s_string = special[-3:] a) 'y Ln' b) '753' c) 'Ln.' d) '135'

c) 'Ln.'

Which method or operator can be used to concatenate lists? a) % b) concat c) + d) *

c) +

What will be displayed after the following code is executed? total = 0 for count in range(4,6): total += count print(total) a) 4 5 b) 4 5 6 c) 4 9 d) 9

c) 4 9

After the execution of the following statement, the variable price will reference the value ________. price = int(68.549) a) 68.6 b) 69 c) 68 d) 68.55

c) 68

What will be the output after the following code is executed and the user enters 75 and -5 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() a) ERROR: cannot have 0 items b) ERROR: number of items can't be negative c) ERROR: cannot have 0 itemsERROR: number of items can't be negative d) Nothing; there is no print statement to display average. The ValueError will not catch the error.

d) Nothing; there is no print statement to display average. The ValueError will not catch the error.

What does the following statement mean?num1, num2 = get_num() a) This statement will cause a syntax error. b) The function get_num() will receive the values stored in num1 and num2. c) The function get_num() is expected to return one value and assign it to num1 and num2. d) The function get_num() is expected to return a value for num1 and for num2.

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

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('/') a) ['03', '/', '07', '/', '2018'] b) ['3', '/', '7', '/', '2018'] c) ['3', '7', '2018'] d) ['03', '07', '2018']

d) ['03', '07', '2018']

A value-returning function is a) a function that receives a value when called b) called when you want the function to stop c) a single statement that performs a specific task d) a function that will return a value back to the part of the program that called it

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

What type of loop structure repeats the code based on the value of Boolean expression? a) count-controlled loop b) number-controlled loop c) Boolean-controlled loop d) condition-controlled loop

d) condition-controlled loop

What type of loop structure repeats the code a specific number of times? a) condition-controlled loop b) number-controlled loop c) Boolean-controlled loop d) count-controlled loop

d) count-controlled loop

What will be displayed after the following code is executed? count = 4 while count < 12: print("counting") count = count + 2 a) counting counting b) counting counting counting counting c) counting counting counting d) counting counting counting counting

d) counting counting counting counting

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) customer.input('Mary Smith') b) customer file.write('Mary Smith') c) customer.write('w', 'Mary Smith') d) customer.write('Mary Smith')

d) customer.write('Mary Smith')

Which would you use to delete an existing key-value pair from a dictionary? a) unpair b) delete c) remove d) del

d) del

Which method can be used to place an item at a specific index in a list? a) append b) index c) add d) insert

d) insert

The Python standard library's ________ module contains numerous functions that can be used in mathematical calculations. a) string b) number c) random d) math

d) math

A(n) ________ access file is also known as a direct access file. a) text b) numbered c) sequential d) random

d) random

Which of the following represents an example to calculate the sum of numbers (that is, an accumulator), given that the number is stored in the variable number and the total is stored in the variable total? a) total = number b) total + number = total c) number += number d) total += number

d) total += number

Which method can be used to add a group of elements to a set? a) addgroup b) addset c) add d) update

d) update

What will be displayed after the following code executes? mystr = 'yes' yourstr = 'no' mystr += yourstr * 2 print(mystr) a) yesnoyesno b) yes + no yes + no c) yes + no * 2 d) yesnono

d) yesnono

What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)? a) { 1 ; 'January', 2 ; 'February', ... 12 ; 'December'} b) { 1, 2,... 12 : 'January', 'February',... 'December' } c) [ '1' : 'January', '2' : 'February', ... '12' : 'December' ] d) { 1 : 'January', 2 : 'February', ... 12 : 'December' }

d) { 1 : 'January', 2 : 'February', ... 12 : 'December' }

The ________ chart is an effective tool used by programmers to design and document functions.

IPO

A(n) ________ loop usually occurs when the programmer does not include code inside the loop that makes the test condition false.

infinite

The top-down design breaks down the overall task of a program into a series of ________.

subtasks

A(n) ________ block includes one or more statements that can potentially raise an exception.

try block

What is the output of the following print statement? print 'I\'m ready to begin' a) I\'m ready to begin b) 'I\'m ready to begin' c) Im ready to begin d) I'm ready to begin

d) I'm ready to begin

Select all that apply. Assume you are writing a program that calculates a user's total order cost that includes sales tax of 6.5%. Which of the following are advantages of using a named constant to represent the sales tax instead of simply entering 0.065 each time the tax is required in the code? a) If the tax amount changes to 7.0%, the value will only have to be changed in one place. b) It allows the end-user to always see the value of the sales tax. c) It will be easier for another programmer who may need to use this code to understand the purpose of the number wherever it is used in the code. d) It avoids the risk that any change to the value of sales tax will be made incorrectly or that an instance of the tax value might be missed as might occur if the number had to be changed in multiple locations.

c) It will be easier for another programmer who may need to use this code to understand the purpose of the number wherever it is used in the code.

What will be the result of the following code? ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 } value = ages['Brianna'] a) False b) -1 c) KeyError d) 0

c) KeyError

What type of volatile memory is usually used only for temporary storage while running a program? a) TMM b) ROM c) RAM d) TVM

c) RAM

What will be the value of the variable list after the following code executes? list = [1, 2, 3, 4] list[3] = 10 a) [1, 10, 10, 10] b) [1, 2, 10, 4] c) [1, 2, 3, 10] d) Nothing; this code is invalid

c) [1, 2, 3, 10]

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) [4, 5, 6] b) [1, 2, 3, 4, 5, 6] c) [1, 2, 3] d) Nothing; this code is invalid

c) [1, 2, 3]

Which of the following would you use if an element is to be removed from a specific index? a) a slice method b) a remove method c) an index method d) a del statement

c) a del statement

Which statement can be used to handle some of the runtime errors in a program? a) a try statement b) an exception handler statement c) a try/except statement d) an exception statement

c) a try/except statement

Which of the following functions returns the largest integer that is less than or equal to its argument? a) ceil b) greater c) floor d) lesser

c) floor

In order to create a graph in Python, you need to include a) import pyplot b) import matplotlib import pyplot c) import matplotlib.pyplot d) import matplotlib

c) import matplotlib.pyplot

Which of the following is the correct way to open a file named users.txt in 'r' mode? a) infile = readlines('users.txt', r) b) infile = open('r', users.txt) c) infile = open('users.txt', 'r') d) infile = read('users.txt', 'r')

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

The ________ function reads a piece of data that has been entered at the keyboard and returns that piece of data, as a string, back to the program. a) output () b) eval_input () c) input () d) str_input ()

c) input ()

Which would you use to get the number of elements in a dictionary? a) sizeof b) length c) len d) size

c) len

The following is an example of an instruction written in which computer language? 10110000 a) C# b) Assembly language c) machine language d) Java

c) machine language

Which step creates a connection between a file and a program? a) close the file b) process the file c) open the file d) read the file

c) open the file

Which method would you use to get the value associated with a specific key and remove that key-value pair from the dictionary? a) list b) items c) pop d) popitem

c) pop

When a file has been opened using the 'r' mode specifier, which method will return the file's contents as a string? a) write b) input c) read d) get

c) read

In a value-returning function, the value of the expression that follows the keyword ________ will be sent back to the part of the program that called the function. a) sent b) def c) return d) result

c) return

Which method could be used to strip specific characters from the end of a string? a) estrip b) strip c) rstrip d) remove

c) rstrip

What is the return value of the string method lstrip()? a) the string with all whitespaces removed b) the string with all leading spaces removed c) the string with all leading whitespaces removed d) the string with all leading tabs removed

c) the string with all leading whitespaces removed

What is the value of the variable phones after the following code executes? phones = {'John' : '5555555', 'Julie' : '5557777'} phones['John'] = '5556666' a) {'John' : '5555555', 'Julie' : '5557777'} b) This code is invalid. c) {'John' : '5556666', 'Julie' : '5557777'} d) {'John' : '5556666'}

c) {'John' : '5556666', 'Julie' : '5557777'}

What will be the output after the following code is executed? def pass_it(x, y): z = x , ", " , y num1 = 4 num2 = 8 answer = pass_it(num1, num2) print(answer) a) 8, 4 b) 4, 8 c) 48 d) None

d) None

Which mode specifier will open a file but not let you change the file or write to it? a) 'w' b) 'a' c) 'e' d) 'r'

d) 'r'

What are the values that the variable num contains through the iterations of the following for loop? for num in range(4): a) 1, 2, 3 b) 1, 2, 3, 4 c) 0, 1, 2, 3, 4 d) 0, 1, 2, 3

d) 0, 1, 2, 3

What will be displayed after the following code is executed? for num in range(0, 20, 5): num += num print(num) a) 25 b) 5 10 15 c) 0 5 10 15 20 d) 30

d) 30

What is the result of the following Boolean expression, given that x = 5, y = 3, and z = 8? x < y and z > x a) 5 b) True c) 8 d) False

d) False


Conjuntos de estudio relacionados

Dental Radiography Chapter 10 Quality Assurance in the Dental Office

View Set

Into the Wild Chapter 3-4 Vocabulary

View Set

Med. Term - Chapter 15, The Nervous System

View Set