Python Unit 1 (Mod 1, 2, 3, 4)

Ace your homework & exams now with Quizwiz!

What does type(1024) return? A all of these B str C flt D int

D (int)

Which is NOT a way to run a code cell in a Jupyter Notebook? A Shift + R B Ctrl + Enter C Shift + Enter D Menu: Cell... > Run Cells

A (Shift + R)

Which is a valid Python code comment? A # use comments to describe the function of code. B // comments make code easier to update. C [comment] comments can provide use instructions for other developers. D (comment) warnings can be placed in comments.

A (# use comments to describe the function of code.)

if True: if False: print("Apple") else: print("Banana") else: if True: print("Dates") else: print("Corn") What is the output of the above code? A Banana B Apple C Dates D Corn

A (Banana)

Given the following code: hot_plate = True if hot_plate: print("Be careful, plate is hot!") else: print("The plate is cool to touch.") What is the output from running the above code? A Be careful, plate is hot! B The plate is cool to touch. C True D NameError

A (Be careful, plate is hot!)

The code: print("It's time to code") Will out which of the following? A It's time to code B It"s time to code C Its time to code D an Error

A (It's time to code)

Given the following code: assignment = 25 if assignment = 25: print('use "=" to assign values') else: pass What is the output from running the above code? A SyntaxError B use "=" to assign values C True D There will be No Output

A (SyntaxError)

Given the following function definition code: def bln_answer(user_response): if user_response == 3: return True elif user_response <= 0: return True else: return False The elif, like the else, can execute only when the previous if conditional evaluates False A True B False

A (True)

Python functions either built in like print(), input(), type() or programmer created using the def function_name(), DO NOT need to have the returned value stored in a variable when they are called. Example given: str_bucket = input("Enter Name: ") or input("Enter Name: ") Both work without giving an error. A True B False

A (True)

The double equal sign (==) is used to compare values. A True B False

A (True)

True and False are Boolean keyword datavalues in Python. A True B False

A (True)

name = "SKYE HOMSI" print("y" in name.lower()) What is the output from the above code? A True B False C "skye homsi" D 3

A (True)

What does type(3.55) return? A flt B int C str D all of these

A (flt)

1. 2. score = add_score(190) 3. 4. print(name, "your score is", score) 5. At which line number in the above code should the following add_score() function definition be inserted so the program runs without error? def add_score(current): return current + 10 A line 1 B line 3 C line 5 D any of these

A (line 1)

It's Python Time! Choose the code that will not create the output shown above. In this question "\ escape character" will be shown as the word backslash and the escape character letter. A print("It's" + "Python Time!") B print("It's Python Time!") C print("It's" + " Python Time!") D print("ItBackslash 's Python Time!")

A (print("It's" + "Python Time!"))

Given: x = 3 y = "3" # get_diff takes an int and a str as parameters, and returns the numerical difference y - x. def get_diff(xint, ystr): # <function code needed here> # main program = calls to get_diff function and tests for the numerical difference to be zero if (y.isdigit() == False): print('"y" is not an integer string') elif (get_diff(x,y) == 0): print('x equal to y') else: print('x is NOT equal to y') To make this program work, printing "x equal to y", choose the best code to replace the above comment "# <function code needed here> " in the get_diff function. A return (int(ystr) - xint) B return (str(ystr) - in(xint)) C return (ystr - x) D return (int(xint) - y)

A (return (int(ystr) - xint))

Function calls should be placed at the beginning of a code page, and function definitions/declarations should be placed later and toward the end of the code. A True B False

B (False)

Escape sequences, such as new line and tab, start with a forwardslash (/). T True F False

F (False)

In Python we can use the "+" operator to add numbers but not to add/combine strings. T True F False

F (False)

In Python, strings can contain ONLY letters a-z and A-Z. T True F False

F (False)

The string method .swapcase() converts from string to integer. T True F False

F (False)

For the code: x = 3 + 9 * 2, the value of x is 21 when applying order of operations. A True B False

A (True)

Function names cannot begin with a number as the first character. A True B False

A (True)

count = 1 while (count >= 1): print("loop") count +=1 What is the output from running the above code? A loop - printed infinitely runaway forever true B loop C loop - printed 2 times D no putput

A (loop - printed infinitely runaway forever true)

x = 0 while True: if (x < 10): print('run forever') x += 1 else: break What is the output from running the above code most like? A run forever - printed 10 times B run forever - printed 9 times C run forever D No Output

A (run forever - printed 10 times)

Given the following nested if - else code: if True: print (1) if True: print (2) if True: print(3) else: print("False condition3") else: print("False condition2") else: print("False condition1") Which of the following is the correct output produced by the code above: A 1 False condition2 B 1 2 3 C 2 False condition3 D none of these

B (1 2 3)

Given the following function definition: def add_numbers(num_1, num_2 = 10): return num_1 + num_2 Choose the correct output after calling the function above using the following code: print(add_numbers(100)) A 100 B 110 C 200 D SyntaxError

B (110)

num_1 = "" num_temp = "" num_final = "" while True: num_1 = input("Enter an integer: ") if (num_1.isdigit() == True): num_final = num_temp + num_1 num_temp = num_final else: print(num_final) break What is the output from running the above code using the following input at the Enter an integer: prompt? 1st input: 3 2nd input: 4 3rd input: five6 A 34five B 34 C 34five6 D 7

B (34)

Given the following code: id = 3556 if id > 2999: print(id, "is a new student") else: print(id, "is an existing student") What is the output from running the above code? A id, is a new student B 3556 is a new student C id, is an existing student D 3556 is an existing student

B (3556 is a new student)

In Python string comparisons, lower case "a" is less than upper case "A" A True B False

B (False)

Which version of Python does this course cover? A Python 2 B Python 3 C Python 4 D Python 5

B (Python 3)

Given the following code: vehicle_type = "Truck" if vehicle_type.upper().startswith("P"): print(vehicle_type, 'starts with "P"') else: print(vehicle_type, 'does not start with "P"') What is the output from running the above code? A Truck starts with "P" B Truck does not start with "P" C True D False

B (Truck does not start with "P")

"Hello".isalpha() What is the output of the above code? A "Hello" B True C False D Error

B (True)

A function with 2 integer parameters would start with which one of the following def syntax? A funct add_numbers(num_1, num_2): B def add_numbers(num_1, num_2): C def 2_numbers(num_1, num_2): D FUNCTION add_numbers(x, y):

B (def add_numbers(num_1, num_2):)

1. greet(name) 2. name = input("enter your name: ") 3. greet(name) 4. def greet(person): 5. print("Hi,", person) 6. greet(name) Which line or lines need to be removed from the above code so the program prints a greeting without error? A lines 1 and 2 B lines 1 and 3 C line 1 only D line 3 only

B (lines 1 and 3)

Which is an example of Python string addition that will run without error? A new_string = Hello + World! B new_string = "Hello " + "World!" C new_string = "Hello ' + "World!' D all of these

B (new_string = "Hello " + "World!")

Given the code below and entering "Colette" for user_name input: total_cost = 22.95 user_name = input("Enter your name: ") Choose the print statement that will output: Colette - the total is $ 22.95 for today's purchase A print(Colette, "- the total is $", total_cost, "for today's purchase") B print(user_name, "- the total is $", total_cost, "for today's purchase") C print(Colette, '- the total is $', total_cost, 'for todays purchase') D print("user_name", "- the total is $", "total_cost", "for today's purchase")

B (print(user_name, "- the total is $", total_cost, "for today's purchase"))

Given x = 9, which line of code will evaluate False? A x == 9 B x < 3 C 3 < x D x = 3

B (x < 3)

if (x > 0): print("-") else: if (y >= 0): print("+/+") else: print("+/-") Which values for x and y result in the output "+/+"? (Choose the best answer.) A x = 1 and y = 0 B x = -1 and y = 0 C x = 0 and y = -1 D x = -1 and y = -1

B (x = -1 and y = 0)

if (x > 0): print("-") else: if (y >= 0): print("+/+") else: print("+/-") The nested (if (y >= 0):) conditional check code is sure to be run if ___. (Choose the best answer.) A x = 1 B x = -1 C y = -1 D y = 1

B (x = -1)

Which contains only integers? (ignore the commas) A Q, W, E, R, T, Y, a, s, d, f B -2, -1, -0.5, 0, 0.5, 1, 2 C 1, 2, 0, -3, 2, -5 D 3, 2, 1, *, +, =, -

C (1, 2, 0, -3, 2, -5)

Given: x = 3 y = 4 calculation = x*y print(calculation) What is the answer for the output of the above code? A 12.0 B 9.0 C 12 D 9

C (12)

Given: calculation = 5 + 15 / 5 + 3 * 2 - 1 print(calculation) What is the best estimate for the output of the above code? A 13 B 9 C 13.0 D 9.0

C (13.0)

Given the following function definition header def add_num(num_1 = 10): print(num_1 + num_1) Choose the correct output that will occur after calling the above function using the following code: add_num() A 0 B 10 C 20 D SyntaxError

C (20)

Given the following function definition: def add_num(num_1 = 10): return num_1 + num_1 Choose the correct output after calling the function above using the following code: print(add_num(100)) A 20 B 100 C 200 D SyntaxError

C (200)

Given the folowing incorrect code statement sequence: 1. name = input("enter your name: ") 2. print(name, "your score is", score) 3. score = 199 Select the correct code statement sequence from the choices below. A 1, 2, 3 B 2, 3, 1 C 3, 1, 2 D 3, 2, 1

C (3, 1, 2)

Given the following code: name = "Jin Xu" if type(name) == type("Hello"): print(name, "is a string entry") else: print(name, "is not a string entry") What is the output from running the above code? A Hello Jin Xu B Jin Xu is a not string entry C Jin Xu is a string entry D string

C (Jin Xu is a string entry)

PriNt("Hello World!") will not output the message "Hello World!" Instead, it will result in which syntax error message (Only one)? A TypeError B SyntaxError C NameError D none of these

C (NameError)

What does the Python code: score = 3 + "45" result in? A 48 B 345 C TypeError: unsupported operand type(s) for +: 'int' and 'str' D type()

C (TypeError: unsupported operand type(s) for +: 'int' and 'str')

Choose the correct statement. A Variables only represent strings in Python. B The same variable in Python can be represented in either upper or lower case. C Variables in Python can be initialized or assigned values using the equals sign (=). D New variables always start out as zero and do not need to be defined or declared before use.

C (Variables in Python can be initialized or assigned values using the equals sign (=).)

Given the following code: answer = input("enter your answer: ") print("You entered " + answer) If user input is 38, what is the output? A TypeError B SyntaxError C You entered 38 D You entered enter your answer

C (You entered 38)

Which is an example of the proper use of the Python input() function syntax? answer = input(enter your answer: ) B answer = INPUT(enter your answer: ) C answer = input("enter your answer: ") D answer = INPUT("enter your answer: ")

C (answer = input("enter your answer: "))

x = 0 while (x < 5): x += 1 print('loop') What is the output from running the above code? A loop B loop - printed 4 times C loop - printed 5 times D loop - printed infinitely because the while is a runaway

C (loop - printed 5 times)

Which is a properly formatted Python print() function example? A print("Welcome the", 3, New students!) B print("Welcome the", 3 + New students!) C print("Welcome the", 3, "New students!") D print("Welcome the", 3 + " New students!")

C (print("Welcome the", 3, "New students!"))

\\\\ What is the best choice of code to create the output shown above? A print("'\''\''\''\''\'") B print("\\\\") C print("\\\\\\\\") D print(\\\\)

C (print("\\\\\\\\"))

Given the following function definition: def low_case(words_in): return words_in.lower() Choose the correct output after calling the function above using the following code: words_lower = low_case("Return THIS lower") print(words_lower) A Return THIS lower B No Output will be generated C return this lower D SyntaxError

C (return this lower)

name = "skye homsi" name_1 = name.title() name_2 = name_1.swapcase() print(name_2) What is the output from the above code? A "Skye homsi" B "SKYE HOMSI" C "skye homsi" D "sKYE hOMSI"

D ("sKYE hOMSI")

Which code formats the string "Green" to "GREEN"? A .isupper() B .capitalize() C .istitle() D .upper()

D (.upper())

Given: x = 3 y = 3 calculation = x/y print(calculation) What is the answer for the output of the above code? A 9 B 9.0 C 1 D 1.0

D (1.0)

Given the following code: year = "2001" if year.isdigit(): print(year, "is all digits") else: pass What is the output from running the above code? A True B False C There will be No Output D 2001 is all digits

D (2001 is all digits)

Given the following code: name = "Tobias" print(name == "Alton") What is the output from running the above code? A Tobias = Alton B Alton C True D False

D (False)

Given the following code: day = "monday" if day.capitalize() == "Monday": print("Start the week!") else: pass What is the output from running the above code? A True B False C No output D Start the week!

D (Start the week!)

length = "33" length.isalnum() What is the output of the above code? A "33" B Error C False D True

D (True)

The following code will result is which one of the outputs? answer = input("enter your answer: ") print(10 + answer) A 48 B 1038 C 10answer D TypeError

D (TypeError)

Choose the correct statement. A A variable name in Python cannot contain numbers. B A variable value in Python cannot be updated. C Variables are updated in Python using the keyword "new". D Variable values in Python can change type, from integer to string by using another assignment (=).

D (Variable values in Python can change type, from integer to string by using another assignment (=).)

Given the following function: def ignore_case(name): answer = "" while (answer.isalpha() == False): answer = input("enter last name: ") return answer.lower() == name.lower() When will the ignore_case function exit the while loop inside it? A When answer == name B When answer.lower() == name.lower() C When a digit is entered at the input prompt D When only letters are entered at the input prompt

D (When only letters are entered at the input prompt)

Hello World! What is the best choice of code to create the output shown above? In this question "\ escapecharacter" will be shown as the word backslash and the escape character letter. A print("HelloBackslash pWorld!") B print("Hello World!") C print("HelloBackslash nWorld!") D print("HelloBackslash tWorld!")

D (print("HelloBackslash tWorld!"))

Given the following function definition: def show_name(): print("Tobias Ledford") Which one of the following choices calls the function correctly? A show_name.run() B open(show_name) C run show_name D show_name()

D (show_name())

Given: size_num = "8 9 10" size = "8" # user input if size.isdigit() == False: print("Invalid: size should only use digits") elif int(size) < 8: print("size is too low") elif size in size_num: print("size is recorded") else: print("size is too high") What is the output from running the above code? A Invalid: size should only use digits B size is too high C size is too low D size is recorded

D (size is recorded)

What does type("Hello World!") return? A all of these B int C flt D str

D (str)

time = 3 while True: print('time is', time) if (time == 3): time = 4 else: break What is the output from running the above code? A time is 3 B time is 4 C no output D time is 3 time is 4

D (time is 3 time is 4)

num_1 = 3 num_2 = "8" What is the minimum code to mathematically/numerically add num_1 to num_2 and assign the result to the container named total? A total = num_1 + str(num_2) B total = int(num_1) + str(num_2) C total = int(num_1) + int(num_2) D total = num_1 + int(num_2)

D (total = num_1 + int(num_2))

Python cannot display quotes in strings using print(). T True F False

F (False)

The code: print("I am", 17, "years old") will print I am17years old T True F False

F (False)

The type() function is used to change from one data type to another. T True F False

F (False)

while True: will loop forever but can be interrupted using the Python statement "end" inside the loop. T True F False

F (False)

Jupyter Notebooks have code cells and markdown cells. T True F False

T (True)

Note: 3 and 5 are hardcoded into the boolean while loop. Given: while (3 < 5): print("looping") Is equivalent to the forever loop while True: print("looping") T True F False

T (True)

The input() function in Python ALWAYS returns user input as a string. T True F False

T (True)

Using comma separation, we can combine strings and numbers (int or floats) in a single Python print() function without a TypeError. T True F False

T (True)


Related study sets

Lesson 12/Chapter 27: Human Development and Inheritance

View Set

SOCIOLOGY EXAM 2: Chapter 9: Constructing Gender and Sexuality

View Set

Chapter 12: Demand Planning & Forecasting

View Set

Ch 4 Social Statistics: Variability

View Set

Economic Statistical Analysis Final Review

View Set

Chapter 29 - Fair Housing & Human Rights

View Set