Programming Midterm PYTHON

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

Which of the following while loops would continue to loop as long as num has values between the range of 3-12, exclusive?

while num < 12 and num > 3: # do something with num

Which of the following string operation is illegal? Assume that word = "music".

word[0] = "M"

Which of the following Python programs will not run?

x = 4 y = "hi" print(x + y)

What will print to the screen when the following program is run? number = 5 less_than_zero = number < 0 print(type(less_than_zero))

<type 'bool'>

What will print to the screen when the following program is run? number = 5 greater_than_zero = number > 0 print(type(number))

<type 'int'>

What will be the output of the following program? a = 4 def print_something(): a = "code" print(a * 3) print_something()

codecodecode

What is printed out by this program? word = "killer whale" print(word[0:100])

killer whale

What is the character that in-line Python comments begin with?

#

Which of the following options is NOT a valid Python function definition?

# computes the surface area of a rectangular prism def surface_area(length, width=10, height): return 2*(length * width + width * height + height * length)

Which of the following is NOT a valid type of comment in Python?

%% This is a comment

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("z")

-1

What does the following Python program print? x = 12 % 4 print(x)

0

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("Edsger")

0

What will be printed to the screen when this program is run? for j in range(2): for i in range(0, 2, 1): print(i)

0 1 0 1

What does the following Python program print? x = 9 + 6 / 3 * 2 - 1 print(x)

12

What does the following code print? x = 3.4 y = 1 print(int(x)) print(x + y)

3 4.4

What will be the output of this program? number = 5 greater_than_zero = number > 0 if greater_than_zero: print(number)

5

Which of the following inputs to the program below would print a statement containing; Your number: try: my_number = int(input("Enter an integer: ")) print("Your number: " + str(my_number)) except ValueError: print("That wasn't an integer!")

5

What will be printed when the program below is run? def add_two(x): return x + 2 def multiply_by_three(x): return x * 3 def my_function(x): return add_two(x) + multiply_by_three(x) print(my_function(12))

50

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("W")

7

What is the final result of the expression 2**3?

8

What is the difference between a binary operator and a unary operator?

A binary operator needs two things, while a unary operator only needs one.

What does this function do? # address: string containing an email address def mystery(address): if "@" not in address: return "" address = address.strip() return address

Checks to make sure the email address is valid by checking for '@' in the address. Returns the address without any leading or trailing whitespace.

Which of the following best describes the main purpose of comments?

Comments describe your program so other people can read it more easily.

What will be printed to the screen if the inputs given to the program below are 1.2, -4, hi, 5 ? def retrieve_positive_number(): while True: try: number = int(input("Enter a positive number: ")) if number > 0: return number print("The number must be positive!") except ValueError: print("That wasn't a number!") print(retrieve_positive_number())

Enter a positive number: 1.2 That wasn't a number! Enter a positive number: -4 The number must be positive! Enter a positive number: hi That wasn't a number! Enter a positive number: 5 5

What does the following Python program print? x = "I am" y = 6 z = "feet tall" print(x) print(y) print(z)

I am 6 feet tall

Which of the following expressions will get the last character in a string? Assume that word is a string variable. I. word[0] II. word[-1] III. word[len(word)] IV. word[len(word)-1]

II, IV

Which of the following statements is true about print statements?

In order to print a string literal, the string must be enclosed in quotes. Each print statement will be printed on its own line. Print statements are how you display text on the screen.

What does this program print? temperature = 65 while temperature < 80: print("It's still a nice day") if temperature < 70: temperature = temperature + 5 elif temperature > 75: break else: temperature = temperature + 3

It's still a nice day It's still a nice day It's still a nice day It's still a nice day

On which line of code will Python error? weight = input("How much do you weigh? ") oz_water = weight / 2 print("You should drink " + str(oz_water)) print("ounces of water every day if you weigh " + weight)

Line 2

On which line does this program throw an error? word = "killer whale" pos = word.find(" ") word = word + "!" word[pos] = "-"

Line 4

What will be the output of this program? number = 5 greater_than_zero = number > 0 if greater_than_zero: if number > 5: print(number)

Nothing will print

Which of the following choices is NOT a Python variable type?

Number

What would this program print? x_pos = 16 y_pos = 20 # Computes the midpoint between (0,0) # and the point def compute_distance(x_pos, y_pos): print("Original Point: " + str(x_pos) + ", " + str(y_pos)) print("Midpoint from Origin: " + str(x_pos/2) + ", " + str(y_pos/2)) compute_distance(12, 14) print("Point: " + str(x_pos) + ", " + str(y_pos))

Original Point: 12, 14 Midpoint from Origin: 6.0, 7.0 Point: 16, 20

What does this function do? # sentence: a string containing a sentence # char: a letter def mystery(sentence, char): res = "" for letter in sentence: if letter == " ": res = res + char else: res = res + letter return res

Returns the same sentence, but replaces every space with the letter char

Why should you use round when you compare two numbers that have type float?

Sometimes numbers can be rounded in unexpected ways based on how Python computes them. Therefore, it is best to use round in order to make sure both numbers are rounded the same way.

Suppose you have a variable defined a = "4". What is the variable type of a?

Str

What type is the following variable? x = "Hi there"

String

What does "immutable" mean with respect to Python strings?

The variable can be assigned a new value, but the string's value cannot be modified.

Why do programmers use try and except statements when their code might throw an exception?

To handle exceptions gracefully and either handle the error or print out a useful error message.

What is the purpose of exceptions?

To indicate that something has gone wrong, and the program does not know how to continue

What is the output of this program? Assume the user enters "kipp52", then "baseball45". # Checks if the user entered the correct username def validate_user(username): if username != "coder43" and username != "goodHacker90" and username != "baseball45": return False else: return True # Gets the user's username def get_username(): while True: username = input("Enter your username: ") if validate_user(username): print("Welcome!") return username else: print("That's not a valid username. Try again") print("Welcome to CodeHS!") get_username()

Welcome to CodeHS! Enter your username: kipp52 That's not a valid username. Try again Enter your username: baseball45 Welcome!

When would a while loop be a better control structure to use than a for loop?

When you don't know how many times something will need to repeat

Which one of the statements below will cause an error?

ans = "hi" + 9

What is printed out by this program? first = "abcde" second = "zyxwv" res = "" for i in range(len(first)): res = res + first[i] + second[i] print(res)

azbycxdwev

Which of the functions below will return a string that is in alternating caps? For example, alt_case("sheep") should return "sHeEp".

def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res

Which of the programs below will print the following output? I would like a green balloon.

def balloon_choice(color): print("I would like a " + color + " balloon.") balloon_choice("green")

Which of the following functions would return the word "CAT" when given "iCjnyAyT"?

def find_secret_word(message): hidden_word = "" for letter in message: if letter != letter.lower(): hidden_word = hidden_word + letter ' return hidden_word

Which of the following programs will NOT cause an error?

def say_hi(first_name="John", last_name="Doe"): print("hi " + first_name + " " + last_name) say_hi("Polly Pocket")

Which of the following for loops will give the same values for i as the loop below? for i in range(10):

for i in range(0, 10):

Which of the following for loops would print the following numbers? 3 5 7 9

for i in range(3, 10, 2): print(i)

Three of the for loops below will provide identical values for i. Which for loop will provide values that do not match the others?

for i in range(5, 0, 1):

Consider the following functions. def fn_a(num): if num > 3: print("Fizz") def fn_b(num): if num > 2 and num < 4: print("Fizz")

for i in range(6): fn_a(i)

What will be the output of the following program? name = "Tracy" def print_hello(name): name = "Turtle" print("hello " + name) print_hello(name)

hello Turtle

Which of the following if statements checks if the string variable sentence contains the word "the"?

if "the" in sentence: print("Contains 'the'")

Which of the following options is the best way to get a number from the user that you plan to use in a mathematical equation?

num = int(input("Enter a number: "))

Choose the correct declaration of a float variable with the value 3.14.

p = 3.14

Which of the following choices will print AeCl? Assume the following variables have been defined. first_name = "Alice" last_name = "Carmichael"

print(first_name[0] + first_name[-1] + last_name[0] + last_name[-1])

What line of code should be written after the function definition to add two single-digit values and print the sum? def get_single_digit(): while True: try: num = int(input("Enter a single-digit positive number: ")) if num > 9: print("Number must be less than 10") elif num < 0: print("Number must be positive.") else: return num except ValueError: print("That isn't a number!")

print(get_single_digit() + get_single_digit())

Choose the print statement below that will cause an error. Assume that num has the value 6, and name has the value Isabella.

print(name + ": " + num)

Which of the following expressions will result in "brown"? Let sentence = "The brown lazy dog"

print(sentence[4:9])

Which of the following expressions will print "dog"? Let sentence = "lazy dog"

print(sentence[5:])

Which of the following expressions will print "L"? Let word = "PINEAPPLE"

print(word[-2])

How would you round the number held in the variable pi to 3.14? pi = 3.14159265

round(pi, 2)

Which of the following while loops will cause an infinite loop?

secret_num = 10 while secret_num == 10: print(secret_num)

What is the type of the variable x in the following Python program? x = input("Enter something: ")

string

What is the output of the following program? sentence = "the dog" for letter in sentence: print(letter + letter)

tt hh ee dd oo gg

What function should be used in the blank to capitalize the first letter of the word stored in word? first_char = word[0] word = first_char.______() + word[1:]

upper

Which of the following choices is a properly formed Python variable name, meaning it is both legal in the Python language and considered good style?

user_age

Which of the following is NOT a program that will produce the following output? hellohellohello

var = "hello" print(var) print(var) print(var)


Conjuntos de estudio relacionados

Med Surg: Chapter 31: Nursing Management: Patients With Endocrine Disorders: PREPU

View Set

Psychology Unit 4 test Chapter 9

View Set

Modern Architecture & Design II Midterm

View Set

30. Long lived assets (web, sch, cfa)

View Set