Python Test Unit 1

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

id = 3556 if id > 2999: print(id, "is a new student") else: print(id, "is an existing student") What is the output of the above line of code?

"3556 is a new student"

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

"Apple"

hot_plate = True if hot_plate: print("Be careful, hot plate!") else: print("The plate is ready.") What is the output from running the above code?

"Be careful, hot plate!"

def low_case(words_in): return words_in.lower() Choose the correct output of calling the function above using the following code: words_lower = low_case("Return THIS lower") print(words_lower)

"return this lower"

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?

"run forever" (10 times)

name = "Tobias" print(name == "Alton") What is the output from running the above code?

False

answer = input("enter your answer: ") print(10 + answer) If user input is 38, what is the output of the above code?

TypeError

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?

When only letters are entered at the input prompt

def add_numbers(num_1, num_2): Could a function with 2 integer parameters start with this definition?

Yes

print("Welcome the", 3, "New students!") Is this a properly formatted Python print() function example?

Yes

x = 3 y = "3" # get_diff takes an int and a str def get_diff(xint, ystr): # <function code needed here> 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.

return int(ystr) - xint

The string method .swapcase() converts from string to integer.

False

The type() function is used to change from one data type to another.

False

Given the code below and entering "Colette" for user_name input: python total_cost = 22.95 user_name = input("Enter your name: ") What print statement that will output: Colette - the total is $ 22.95 for today's purchase

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

year = "2001" if year.isdigit(): print(year, "is all digits") else: pass What is the output from running the above code?

"2001 is all digits"

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 line of code?

"Jin Xu is a string entry"

x = 0 while x < 5: x += 1 print('loop') What is the output from running the above code most like?

"loop" (5 times)

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.)

x = 0 and y = -1

1. greet(name) 2. name = input("enter your name: ") 3. greet(name) 4. def greet(person): u 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?

lines 1 and 3

\\\\ What is the best choice of code to create the output shown above?

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

Function calls should be placed at the beginning of a code page, and functions should be placed later and toward the end of the code.

False

In Python, strings can contain only letters a-z and A-Z.

False

Using comma separation, we can combine strings and numbers (int & float) in a single Python print() function without a TypeError.

True

Variable values in Python can change type, from integer to string

True

Variables in Python can be initialized using the equals sign (=).

True

What does type(3.55) return?

float

Python Function return values must be stored in variables.

False

Falsenew_string = "Hello " + "World!" This will run without an error, true or false?

True

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

True

Function names cannot begin with a number as the first character.

True

name = "SKYE HOMSI" print("y" in name.lower()) What is the output from the above code?

True

while 3 < 5: is a forever loop just like while True: is.

True

Given x = 9, which line of code will evaluate False?

x<3

Which is a valid Python code comment?

# use comments to describe the function of code.

Which contains only integers?

1, 2, 0, -3, 2, -5

print("It's time to code") What is the output of the above code?

It's time to code

Which version of Python does this course cover?

Python 3

"Hello".isalpha()

True

What does the Python code: score = 3 + "45" result in?

TypeError

answer = input("enter your answer: ") print("You entered " + answer) If user input is 38, what is the output of the above code?

You entered 38

x = 3 y = 3 calculation = x/y print(calculation) What is the best estimate for the output of the above code?

1.0

def add_numbers(num_1, num_2 = 10): return num_1 + num_2 Choose the correct output of calling the function above using the following code: print(add_numbers(100))

110

calculation = 5 + 15 / 5 + 3 * 2 - 1 print(calculation) What is the best estimate for the output of the above code?

13.0

def add_num(num_1 = 10): print(num_1 + num_1) Choose the correct output of calling the above function using the following code: add_num()

20

1.name = input("enter your name") 2. print(name, "your score is", score) 3. score = 199 Order the above numbered code lines into the proper sequence.

3,1,2

num_1 = "" num_temp = "" num_final = "" while True: num_1 = input("Enter an integer: ") if num_1.isdigit(): 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: five 4th input: 6

34

The code print("I am", 17, "years old") will output: I am17years old

False

In Python string comparisons, lower case "a" is less than upper case "A".

True

count = 1 while count >= 1: print("loop") count +=1 What is the output from running the above code most like?

forever loop

It's Python Time! Choose the code that will not create the output shown above.

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

def show_name(): print("Tobias Ledford") Which code calls the function in the above code?

show_name()

What does type("Hello World!") return?

str

day = "monday" if day.capitalize() == "Monday": print("Start the week!") else: pass What is the output from running the above code?

"Start the week!"

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?

"Truck does not start with "P""

name = "skye homsi" name_1 = name.title() name_2 = name_1.swapcase() print(name_2) What is the output from the above code?

"sKYE hOMSI"

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

x=1

In Python, we can use the "+" operator to add numbers, but not to add strings.

False

Nested Conditional code always runs all sub-conditions that are indented under if statements.

False

Python cannot display quotes in strings using print().

False

Which code formats the string "Green" to "GREEN"?

.upper()

while True: will loop forever but can be interrupted using the Python keyword "end" inside the loop.

False

x = 3 y = 4 calculation = x*y print(calculation) What is the best estimate for the output of the above code?

12

def add_num(num_1 = 10): return num_1 + num_1 Choose the correct output of calling the function above using the following code: print(add_num(100))

200

num_1 = 3 num_2 = "8" What is the minimum code to mathematically add num_1 to num_2?

num_1 + int(num_2)

PriNt("Hello World!") will not output the message "Hello World!". What will it result in?

NameError

What does type(1024) return?

int

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

line 1

Hello World! What is the best choice of code to create the output shown above?

print("Hello\nWorld!")

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?

"size is recorded"

time = 3 while True: print('time is', time) if time == 3: time = 4 else: break What is the output from running the above code most like?

"time is 3" & "time is 4"

Which is not a way to run a code cell in a Jupyter Notebook?

Shift + R

assignment = 25 if assignment = 25: print('use "=" to assign values') else: pass What is the output of the above code?

SyntaxError

Escape sequences start with a backslash (\).

True

Jupyter Notebooks have code cells and markdown cells.

True

Like else, the elif can execute only when the previous if conditional evaluates False (shown in the below code sample). if response == 3: return "Correct" elif response <= 0: return "Correct" else: return "Incorrect"

True

The double equal sign (==) is used to compare values.

True

The input() function in Python returns user input as a string.

True

True and False are Boolean keywords in Python.

True

answer = input("enter your answer: ") True or false, this is an example of a correct function in Python.

True

length = "33" length.isallnum() What is the output of the above code?

True


Ensembles d'études connexes

Chapter 12-Tax Administration and Planning

View Set

Microeconomics chapter 6,7,8,9 AIC

View Set

Astronomy Ch01.6: Key Concept: Our Cosmic Origins

View Set