Comp Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Question: 12 What is the character that in-line Python comments begin with? # % - "

#

Which of the following is NOT a valid type of comment in Python? %% This is a comment # This is a comment """ This is a comment """

%% This is a comment

What will be printed to the screen when this program is run? num = 100 while num > 0: for i in range(100, 0, -25): num = num - i print(num) 100 0 -75 -125 -150 -150 -175 175

-150

What will be the output of this program? number = 5 less_than_zero = number < 0 if less_than_zero: print(number) number = number-10 less_than_zero = number < 0 if less_than_zero: print(number) 5 5 -------------- -5 ------------- 5 -5 ------------- Nothing will print

-5

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

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 0 2 0 1 0 1 0 1 1 2 1 2 1 2

0 1 0 1

What does the following program print? for i in range(2): for j in range(2): print(i + j)

0 1 1 2

What does this program print? for i in range(10): if i == 3: continue print(i) 0 1 2 0 1 2 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3

0 1 2 4 5 6 7 8 9

What will the following program print when run? number_one = 5 number_two = 10 if number_one == 5: print(1) elif number_one > 5: print(2) elif number_two < 5: print(3) elif number_one < number_two: print(4) elif number_one != number_two: print(5) else: print(6)

1

What will the following program print when run? number_one = 5 number_two = 10 if number_one == 5: print(1) if number_one > 5: print(2) if number_two < 5: print(3) if number_one < number_two: print(4) if number_one != number_two: print(5)

1 4 5

What is the difference between a for loop and a while loop? 1. A for loop repeats commands a specific number of times and a while loop repeats until a condition becomes false. 2. A for loop is used to make decisions between two options and a while loop is used to make decisions between 3 options or more. 3. A for loop is used when the variable is less than 10 and a while loop is used when the variable is 10 or greater. 4. A for loop is used when we only need to meet one condition and a while loop is used when there are two or more conditions.

1. A for loop repeats commands a specific number of times and a while loop repeats until a condition becomes false.

What would be the output of the following code? for i in range(2): print("Tracy") print("Turtle") 1. TracyTracyTurtle 2. TracyTracyTurtleTracyTracyTurtle 3. TracyTracyTurtle 4. TracyTurtle

1. TracyTracyTurtle

The following snippet is from a program that asks the user what he/she wants to order. The user's input is stored in the variable item_ordered. If the user enters 3, what will the following code snippet print? item_ordered = input("Enter the name of the item you wish to order: ") try: print("You have ordered the following item: " + item_ordered) except TypeError: print("I'm sorry. You have entered an invalid item.") 1. You have ordered the following item: 3 2. You have ordered the following item: three 3. You have ordered the following item:I'm sorry. You have entered an invalid item 4. I'm sorry. You have entered an invalid item

1. You have ordered the following item: 3

Which of the following assignment statements is equivalent to the following code snippet? coffee, tea, smoothie = ["kona", "mango", "raspberry"] 1. coffee = "kona" tea = "mango" smoothie = "raspberry" 2. coffee = "raspberry" tea = "mango" smoothie = "kona" 3. coffee = "kona" tea = "kona" smoothie = "kona" 4. coffee = ["kona", "mango", "raspberry"] tea = ["kona", "mango", "raspberry"] smoothie = ["kona", "mango", "raspberry"]

1. coffee = "kona" tea = "mango" smoothie = "raspberry"

Which of the following boolean expressions would make the while loop find the first animal in the list whose length is longer than 8? animals = ['snowy owl', 'salamander', 'bunny', 'octopus', 'squirrel', 'buffalo'] cur = 0 while ____________: cur +=1 print("First animal with length longer than 8 is " + animals[cur]) 1. len(animals[cur]) <= 8 2. len(cur) <= 8 3. len(animals) <= 8 4. animals[cur].len <= 8

1. len(animals[cur]) <= 8

Which of the following programs would alter my_list to consist of the following: ['red', 'green', 'blue', 'orange', 'yellow'] 1. my_list = ['red', 'green', 'blue', 'orange'] my_list.append('yellow') 2. my_list = ['red', 'green', 'blue', 'orange'] my_list.extend('yellow') 3. my_list = ['red', 'green', 'blue', 'orange'] my_list.replace('yellow') 4. my_list = ['red', 'green', 'blue', 'orange'] my_list.add('yellow')

1. my_list = ['red', 'green', 'blue', 'orange'] my_list.append('yellow')

Which of the following is a correct example of unpacking? 1. my_list = [1, 2, 3] x, y, z = my_list 2. my_list = [1, 2, 3] x = y = z = my_list 3. x = 1 y = 2 z = 3 my_list = [x, y, z] 4. x = 1 y = 2 z = 3 my_list = x = y = z

1. my_list = [1, 2, 3] x, y, z = my_list

Which of the following code snippets will correctly convert a string to a list? 1. my_string = "hello" str_as_list = list(my_string) 2. my_string = "hello" str_as_list = listify(my_string) 3. my_string = "hello" str_as_list = make_list(my_string) 4. my_string = "hello" str_as_list = new_list(my_string)

1. my_string = "hello" str_as_list = list(my_string)

Which of the following operations will cause an error? 1. ribbon_colors = ("blue", "red", "green") ribbon_colors[2] = "silver" 2. ribbon_colors = ["blue", "red", "green"] ribbon_colors[2] = "silver" 3. ribbon_colors = {"first place":"blue", "second place":"red", "third place":"green"} ribbon_colors["third place"] = "silver" 4. first_place = "blu" first_place = first_place + "e"

1. ribbon_colors = ("blue", "red", "green") ribbon_colors[2] = "silver"

Which of the following is the correct way to declare a tuple? 1. x = (1, 2, 3) 2. x = [1, 2, 3]

1. x = (1, 2, 3)

What is the result of the following code? total = 0 for i in range(5): total += i print(total)

10

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

12

What is the value of num when this loop completes? num = 0 for i in range(2, 8, 2): num = num + i

12

What is the final result of the expression 4 + 5 * 3? 27 12 21 19

19

How many possible values are there for a boolean variable? 1 2 3 There is an infinite number of possibilities.

2

What is wrong with the following function? def compute_total_pay(hourly_rate=10.5, overtime_rate=15, num_hours): if num_hours <= 40: return hourly_rate * num_hours return 40*hourly_rate + (num_hours - 40) * overtime_rate 1. Only one parameter can have a default value given 2. Any parameters that don't have default values must come before any parameters that do have default values 3. Default parameters must be the same type for every parameter 4. There is nothing wrong with the function

2. Any parameters that don't have default values must come before any parameters that do have default values

What does "unpacking" refer to in Python? 1. Creating a list 2. Initializing multiple variables at a time using a collection of values 3. Storing multiple variables' values by putting them in a collection, like a list or tuple 4. Cramming as many commands as possible onto a single line

2. Initializing multiple variables at a time using a collection of values

Given the following 2D list, which of the following expressions would get the element "cucumber"? food = [ ["apple", "banana", "kumquat"], ["cucumber", "onion", "squash"], ["candy", "soda", "sugar"] ] 1. foods[2][1] 2. foods[1][0] 3. foods[0][2] 4. foods[1][1]

2. foods[1][0]

Which of the following programs would produce the following output: 1. honey 2. bread 3. jelly 4. plates 1. my_list = ["honey", "bread", "jelly", "plates"] for index in range(len(my_list)): print(str(index) + ". " + my_list) 2. my_list = ["honey", "bread", "jelly", "plates"] for index in range(len(my_list)): print(str(index+1) + ". " + my_list[index]) 3. my_list = ["honey", "bread", "jelly", "plates"] for index in my_list: print(str(index+1) + ". " + my_list[index]) 4. my_list = ["honey", "bread", "jelly", "plates"] for index in len(my_list): print(str(index) + ". " + my_list[index])

2. my_list = ["honey", "bread", "jelly", "plates"] for index in range(len(my_list)): print(str(index+1) + ". " + my_list[index])

Which of the following list comprehensions will create the same list as the for loop below? my_list = [] for i in range(6): my_list.append(i*2) 1. my_list = [i for i in range(6)] 2. my_list = [i*2 for i in range(6)] 3. my_list = [i for i*2 in range(6)] 4. This cannot be done using a list comprehension statement.

2. my_list = [i*2 for i in range(6)]

Look at the following program: my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"] # You pick the code that goes here... # ... # ... print(my_list) Pick the code that results in the following output: ['apples', 'bananas', 'grapes', 'oranges', 'pineapples'] 1. my_list.sort() my_list.reverse() 2. my_list.sort() 3. my_list.reverse() 4. my_list.remove("grapes")

2. my_list.sort()

Which of the following assignment statements is equivalent to the following code snippet? red, blue, green = "BLUE", "GREEN", "RED" 1. red = "RED" blue = "BLUE" green = "GREEN" 2. red = "BLUE" blue = "GREEN" green = "RED" 3. red = ["BLUE", "GREEN", "RED"] blue = ["BLUE", "GREEN", "RED"] green = ["BLUE", "GREEN", "RED"] 4. red = "BLUE" blue = "BLUE" green = "BLUE"

2. red = "BLUE" blue = "GREEN" green = "RED"

What does the following code print? x = 3.4 y = 1 print(int(x)) print(x + y) 3.4 4.4 3 4.4 3 4 The code causes an error

3 4.4

How many times would the phrase 'codeHS' be printed in the following pseudocode: repeat 2 times repeat 4 times print "codeHS" 1. 2 times 2. 6 times 3. 8 times 4. 16 times

3. 8 times

What would be the output of the following code? text = "hello" if "a" in text: print("Yes") elif "ll" in text: print("Maybe") else: print("No") 1. Yes 2. No 3. Maybe 4. No output

3. Maybe

What does "packing" refer to in Python? 1. Creating a list 2. Initializing variables using values from a list 3. Storing multiple variables' values by putting them in a collection, like a list or tuple 4. Cramming as many commands as possible onto a single line

3. Storing multiple variables' values by putting them in a collection, like a list or tuple

What would the output of the following code be? x = "Tracy" print(x * 5) 1. Tracy5 2. Tracy Tracy Tracy Tracy Tracy 3. TracyTracyTracyTracyTracy 4. x5

3. TracyTracyTracyTracyTracy

What is the value of the list after this program runs? days = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri'] days.reverse() days.extend(['Sat', 'Sun']) 1. ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'] 2. ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', ['Sat', 'Sun']] 3. ['Fri', 'Thurs', 'Wed', 'Tues', 'Mon', 'Sat', 'Sun'] 4. ['Fri', 'Thurs', 'Wed', 'Tues', 'Mon', ['Sat', 'Sun']]

3. ['Fri', 'Thurs', 'Wed', 'Tues', 'Mon', 'Sat', 'Sun']

What list is produced using the following list comprehension? word = "butterfly" [w for w in word] 1. ['w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w'] 2. ['w', 'o', 'r', 'd'] 3. ['b', 'u', 't', 't', 'e', 'r', 'f', 'l', 'y'] 4. ['butterfly', 'butterfly', 'butterfly', 'butterfly', 'butterfly', 'butterfly', 'butterfly', 'butterfly', 'butterfly']

3. ['b', 'u', 't', 't', 'e', 'r', 'f', 'l', 'y']

What is the output of this program? plants = { "tree" : "oak", "flower" : "daisy", "weed" : "bindweed" } for plant in plants: print(plants[plant]) 1. tree flower weed 2. tree : oak flower : daisy weed : bindweed 3. oak daisy bindweed 4. tree oak flower daisy weed bindweed

3. oak daisy bindweed

Which of the following is a correct example of packing? 1. my_list = [1, 2, 3] x, y, z = my_list 2. my_list = [1, 2, 3] x = y = z = my_list 3. x = 1 y = 2 z = 3 my_list = [x, y, z] 4. x = 1 y = 2 z = 3 my_list = x = y = z

3. x = 1 y = 2 z = 3 my_list = [x, y, z]

Which of the following values is rounded to 3 decimal places? 2.3 3.000 3.0001 3

3.000

How many times does this program print That's a large class!? num_students = [23, 21, 33, 35, 24, 35] for num in num_students: if num > 23: print("That's a large class!") 0 1 4 6

4

x = 10 while x > 0: print(x) x = x - 3 How many lines will this print?

4

What is the total forward distance moved when the code below is run? def move_forward(): for i in range (2): forward(50) move_forward() move_forward() 1. 20 2. 50 3. 100 4. 200

4. 200

Which of the following is NOT true about tuples? 1. Tuples are ordered. 2. Tuples are immutable. 3. Tuples can contain elements of different types. 4. Tuples use parentheses to access individual elements, rather than square brackets.

4. Tuples use parentheses to access individual elements, rather than square brackets.

What would be the output of the following code? colors = ["red", "green", "blue", "yellow"] colors.reverse() colors.append("orange") colors.sort() print(colors) 1. ["yellow", "red", "orange", "green", "blue"] 2. ["red", "green", "blue", "yellow", "orange"] 3. ["yellow", "blue", "green", "red", "orange"] 4. ["blue", "green", "orange", "red", "yellow"]

4. ["blue", "green", "orange", "red", "yellow"]

What does the following program print? my_grid = [[2, 5, 7, 10], [-3, "hello", 9, "hi"], ["hello", "hi", "hello", "hi"], [True, False, True, False]] print(my_grid[0]) 1. [5, 7, 10, -3, 'hello', 9, 'hi'] 2. [2, 5] 3. [[2, 5, 7, 10], [-3, 'hello', 9, 'hi']] 4. [2, 5, 7, 10]

4. [2, 5, 7, 10]

Which of the following code snippets will create this 2D list? [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] 1. board = [1, 2, 3] + [4, 5, 6] + [7, 8, 9] 2. board = [1, 2, 3] board.append([4, 5, 6]) board.append([7, 8, 9]) 3. board = [1, 2, 3, 4, 5, 6, 7, 8, 9] 4. board = [] board.append([1, 2, 3]) board.append([4, 5, 6]) board.append([7, 8, 9])

4. board = [] board.append([1, 2, 3]) board.append([4, 5, 6]) board.append([7, 8, 9])

What function would you use to get the position of a character in a string? 1. len 2. strip 3. remove 4. find

4. find

Given the following list: my_list = ["s", "Z", "e", "c", "c", "e", "r", "h", "e", "p", "t"] which program below would give the following output: s e c r e t 1. for index in my_list: if index % 2 == 0: print(my_list[index]) 2. for index in range(my_list): if index % 2 == 0: print(my_list[index]) 3. for index in len(my_list): if index % 2 == 0: print(my_list[index]) 4. for index in range(len(my_list)): if index % 2 == 0: print(my_list[index])

4. for index in range(len(my_list)): if index % 2 == 0: print(my_list[index])

A dictionary maps: 1. strings to strings 2. strings to numbers 3. indices to elements 4. keys to values

4. keys to values

Look at the following program: my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"] # You pick the code that goes here... # ... # ... print(my_list) Pick the code that results in the following output: ['pineapples', 'oranges', 'grapes', 'apples'] 1. my_list.remove("bananas") 2. my_list.sort() my_list.remove("bananas") 3. my_list.reverse() my_list.remove("bananas") 4. my_list.sort() my_list.reverse() my_list.remove("bananas")

4. my_list.sort() my_list.reverse() my_list.remove("bananas")

What is the value of num after this code runs? shapes = ["triangle", "square", "hexagon", "circle", "pentagon"] num = len(shapes) 0 4 5 35

5

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

5

What will be the output of this program? number = 5 greater_than_zero = number > 0 less_than_zero = number < 0 if greater_than_zero: print(number) if less_than_zero: print(number + 1) if greater_than_zero and less_than_zero: print(number + 2) if greater_than_zero or less_than_zero: print(number + 3)

5 8

What is the final result of the expression 2**3? 6 8 2 That is not a valid Python expression.

8

Evaluate the mathematical expression: 10 - 6 x (4 - 2) / 2 + 5.

9

What is the value of sum when this loop completes? sum = 0 for i in range(3): sum = sum + 5 for j in range(2): sum = sum - 1 8 9 20 0

9

What is the final result of the expression 7 / 2 + 6? 0 9.5 9 0.875

9.5

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

<type 'bool'>

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

<type 'int'>

What does the following program print? a = "hi" b = 4 c = a * b print(type(c))

<type 'str'>

Which of the following is not a comparison operator? <= != ? >

?

What is the difference between a binary operator and a unary operator? A computer can use binary operators, but it cannot use unary operators. A unary operator needs two things, while a binary operator only needs one. A binary operator needs two things, while a unary operator only needs one. Binary operators are used for arithmetic expressions, while unary operators are for strings.

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

Which of the following best describes the purpose of a for loop? A for loop is for doing something an indeterminate number of times. A for loop is doing something an infinite number of times. A for loop is for doing something a fixed number of times. A for loop is for doing something three times.

A for loop is for doing something a fixed number of times.

In what order should these statements be executed in order to get input from the user and print out the result? A) response = input("Do you like cheese? ")B) print("You have chosen " + confirm)C) print("You responded " + response)D) confirm = input("Are you sure? ") A, B, C, D B, C, A, D A, C, D, B C, B, A, D

A, C, D, B

What will the following program print when run? above_16 = True has_permit = True passed_test = False if above_16 and has_permit and passed_test: print("Issue Driver's License") elif above_16 or has_permit or passed_test: print("Almost eligible for Driver's License") else: print("No requirements met.")

Almost eligible for Driver's License

while True: print("hi") How many lines will this print?

An infinite number of lines

What does this program print? favorite_color = "blue" if favorite_color != "green": print("But green is the color of grass!") if favorite_color == "red": print("I like red, too!") if favorite_color == "blue": print("Blue is the best") if favorite_color != "yellow": print("But yellow is the color of school buses!")

But green is the color of grass!Blue is the bestBut yellow is the color of school buses!

Which of the following best describes the main purpose of comments? Comments create better spacing in programs. Comments describe your program so other people can read it more easily. Comments warn the people running the program about potential problems with the program.

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

Question: 2 Suppose you run the following Python program: x = input("Enter some text: ") print(x) While the program is running, after the prompt has been printed, the user types the following and presses Enter: Delaware ratified the U.S. Constitution in 1787. What does the program print?

Delaware ratified the U.S. Constitution in 1787.

What kind of data structure is user_data in the following declaration? user_data = {"name": "TJ", "age":24, "user_name":"artLover123"} Tuple List Dictionary 2D List

Dictionary

What does this Python expression evaluate to? 100 != 100 True False "True" "False"

False

What is the output of the following program? Assume the user enters "Florence", then "Fernandez". first_name = input("What is your first name? ") last_name = input("What is your last name? ") whole_name = first_name + last_name print(whole_name) Fernandez Florence FlorenceFernandez FlorenceFernandez Florence Fernandez

FlorenceFernandez

What does the following code print? time_of_day = ["morning", "afternoon", "evening"] for word in time_of_day: print("Good " + word)

Good morning Good afternoon Good evening

What does the following Python code display? print("Hello") print("World")

Hello World

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

Use the following definitions of foods and groceries. foods = ["apples", "eel", "kale"] groceries = ["apples", "eel", "kale"] Which of the following statements would evaluate to True about foods and groceries? I. foods == groceries II. foods is groceries I only II only Both I and II Neither I nor II

I only

Which of the following describes a task effectively broken into smaller parts? I. Writing a book by brainstorming, making an outline, and drafting the first chapter II. Comparing two different cameras III. Using your phone's calculator

I only

Question: 17 Which of the following statements is true about print statements? I. In order to print a string literal, the string must be enclosed in quotes.II. Each print statement will be printed on its own line.III. Print statements will not let you print string and number characters in the same statement.IV. Print statements are how you display text on the screen. I, IV II, III I, II, IV I, III, IV

I, II, IV

Eliza will wear a coat if it is raining or if it is less than 50° F outside. Otherwise, Eliza will leave her coat at home. In which of the following cases will Eliza leave her coat at home?

It is not raining and 50°F

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 It's still a nice day It's still a nice day It's still a nice day It's still a nice day It's still a nice day It's still a nice day It's still a nice day It's still a nice day It's still a nice day It's still a nice day

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 1 Line 2 Line 3 Line 4

Line 2

What kind of data structure is user_data in the following declaration? user_data = ["TJ", 24, "artLover123"] Tuple List String 2d List

List

What does this program print? favorite_color = "blue" if favorite_color != "blue": if favorite_color != "red": print("Must be yellow") elif favorite_color == "blue": if favorite_color != "yellow": print("Must be blue") elif favorite_color == "blue": if favorite_color != "yellow": print("I like purple") else: if favorite_color == "blue": print("Blue is the best")

Must be blue

What does this program print? sentence = "My favorite animal is a dog or chipmunk" sentence_list = sentence.split() sentence_list[-3] = "penguin" sentence = " ".join(sentence_list) print(sentence) My favorite animal is a dog or penguin My favorite animal is a penguin or chipmunk My favorite animal is a dog penguin chipmunk My favorite animal is a dog or chipmunk

My favorite animal is a penguin or chipmunk

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

Nothing will print

What kind of data does a float variable contain? Whole numbers Words Numbers that can have decimal components A float is not a Python variable type

Numbers that can have decimal components

Why should you use round when you compare two numbers that have type float? If you don't, Python will only compare the whole part and discard the decimal part. You don't have to, but it's good style. All of the comparisons will still work out the same way. 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. You should always use round when you compare two numbers, even if they are of type int.

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.

What is the benefit of using the break command as in the program below? magic_number = 10 while True: guess = int(input("Guess my number: ")) if guess == magic_number: print("You got it!") break print("Try again!") The user can enter as many answers as they want The program will break out of the loop as soon as the user enters the magic_number The loop will prompt the user to enter a number no matter what input they give The loop will give the magic_number after a certain number of tries

The program will break out of the loop as soon as the user enters the magic_number

What kind of data structure is user_data in the following declaration? user_data = ("TJ", 24, "artLover123") Tuple List String 2d List

Tuple

Which of the following data structures is immutable? Tuple List Dictionary 2d List

Tuple

When would a while loop be a better control structure to use than a for loop? When you need to repeat multiple commands When you need to repeat something 5 times When you don't know how many times something will need to repeat When the user is inputting how many times to repeat something

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

What does this program print? height = 65 gender = "female" if height < 62 or (height < 69 and gender == "male"): print("You are below average height") elif height > 69 or (height > 62 and gender == "female"): print("You are above average height") else: print("You are average height")

You are above average height

What does this program print? height = 65 gender = "female" if height < 62: print("You are below average height") elif height > 69: print("You are above average height") else: print("You are average height")

You are average height

What does this program print? numbers = [x + x for x in range(4)] print(numbers) [0, 1, 2, 3, 4] [0, 1, 2, 3] [2, 4, 6, 8] [0, 2, 4, 6]

[0, 2, 4, 6]

What will be the output of the following program? my_list = [1, 2, 3, 4] num = 5 for index in range(len(my_list)): my_list.append(num + index) print(my_list) [1, 2, 3, 4, 5, 5, 5, 5] [1, 2, 3, 4, 5] [1, 2, 3, 4, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8]

[1, 2, 3, 4, 5, 6, 7, 8]

given the following list: my_list = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] what will be printed when the following line of code is called? print(my_list[3][1:]) [7, 8] [9, 10, 11] [6, 7, 8] [10, 11]

[10, 11]

What does this program print? numbers = [x % 2 == 0 for x in range(0, 6)] print(numbers) [True, False, True, False, True, False] [0, 1, 0, 1, 0, 1, 0] [0, 1, 0, 1, 0, 1] [0, 1, 2, 3, 4, 5]

[True, False, True, False, True, False]

What is the result of word[-3] if word = "chalkboard"? o l a r

a

Which values entered into 'my_list' would print a value of '-1' after the program has run? my_list = [...] num = 0 for thing in my_list: num = num - thing print(num) a. 5, -10, 6 b. -10, -5, 6 c. -5, 10, 6 d. -5, 0, 10, 6

a. 5, -10, 6

What will the value of sum be after the following code runs? sum = 0 for i in range (0, 6, 2): for j in range (2): sum = sum + 1 a. 6 b. 12 c. 24 d. 0

a. 6

Which of the following lines of code will cause an error? Use the following definition of ages: ages = (12, 5, 8) ages = ages + (1, 3, 5) print(ages[2]) ages = ages[2:] ages[0] = 3

ages[0] = 3

Which one of the statements below will cause an error? ans = "hi" * 8 ans = "hi" + 9 ans = "hi" + "hi" + "hi" ans = ("a" * 4) + "b"

ans = "hi" + 9

What is the output of this program? dimensions = [ [12, 3, 5], [45, 7, 8], [1, 2, 3] ] for row in dimensions: for num in row: print(num) a. 12 45 1 3 7 2 5 8 3 b. 12 3 5 45 7 8 1 2 3 c. 12 3 5 d. 3 2 1 8 7 45 5 3 12

b. 12 3 5 45 7 8 1 2 3

What does this code snippet print? fruit = ["b", "n", "n"] print("a".join(fruit)) bnn ba na na banan banana

banan

Which of the following is not a logical operator in Python? and or not because

because

If I am using the following condition in my program: while True: which keyword should I use to make sure I don't create an infinite loop? break continue Check

break

What would be printed to the screen when the following program is run? def return_number(x): return x + 3 print(return_number(2)) a. 2 b. 3 c. 5 d. 6

c. 5

What would the following program print to the screen when run? my_list = [7, 0, 0, "d", "n", "o", "B"] my_list.reverse() for thing in my_list: print(thing) a. 7 0 0 d n o B b. 0 0 7 B o n d c. B o n d 0 0 7 d. 700donB

c. B o n d 0 0 7

What does this program print? heights = [65, 56, 67, 48, 64] heights.sort() heights.extend([60, 61, 62]) heights.remove(67) print(heights) a. [48, 56, 64, 65, [60, 61, 62]] b. [48, 56, 60, 61, 62, 64, 65] c. [48, 56, 64, 65, 60, 61, 62] d. The program will error.

c. [48, 56, 64, 65, 60, 61, 62]

What does the following program print? my_grid = [[2, 5, 7, 10], [-3, "hello", 9, "hi"], ["hello", "hi", "hello", "hi"], [True, False, True, False]] print(my_grid[:2]) a. [5, 7, 10, -3, 'hello', 9, 'hi'] b. [2, 5] c. [[2, 5, 7, 10], [-3, 'hello', 9, 'hi']] d. [2, 5, 7, 10]

c. [[2, 5, 7, 10], [-3, 'hello', 9, 'hi']]

Which Python keyword skips back to the beginning of a loop? break continue

continue

Which of the following does not properly nest control structures? for i in range(3): for j in range(6): print(j) for i in range(3): if i > 2: break else: print(i) count = 0 if count < 10: for i in range(3): print(count) count = count + 1 count = 10 for i in range(3): if count > 0: print(i) else: print(count)

count = 10 for i in range(3): if count > 0: print(i) else: print(count) bc of indentation of else

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(11, 0): for i in range(10, 0, -1): for i in range(0, 10): for i in range(0, 11, 1):

for i in range(0, 10):

Which of the following programs prints ten lines? for i in range(10): print("hi") for i = 1 to 10: print("hi") for i in 1 - 10: print("hi") for i from 0 to 9: print("hi")

for i in range(10): print("hi")

Which of the following for loops would print the following numbers? 3 5 7 9 for i in range(3, 10, 2): print(i) for i in range(3, 9, 2): print(i) for i in range(9): print(i) for i in range(3,9): print(i)

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(0, 5): for i in range(5): for i in range(0, 5, 1): for i in range(5, 0, 1):

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

Which of the following for loops would print the following numbers? 0 1 2 3 4 5 for i in range(5): print(i) for i in range(1, 5, 1): print(i) for i in range(6): print(i) for i in range(0, 5, 1): print(i)

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

Which of the following programs will run but will not print anything? x = True if x: print("hi") ----------------- if False: print("hi") ----------------- x = False if x: print("hi") else: print("hello") ----------------- if True: print("hi") else: print("hello")

if False: print("hi")

Assume you are writing a program, and you have a boolean variable called b, defined like so: b = True Pick the correct if statement to follow the code above. The if statement should be correct Python, and the body of the if statement should only run if b is True. if b: print("b is True!") if b: print("b is True!") if True: print(b) if True: print(b)

if b: print("b is True!")

Which of the following conditions tell me to sleep if my alarm did not go off (alarm = False) and I'm tired (tired = True)? if alarm and tired: sleep if not alarm or tired: sleep if not alarm and tired: sleep if not alarm and not tired: sleep

if not alarm and tired: sleep

What is the type of the variable x if x = 5?

int

Choose the option that correctly prints out the variable(s). x = "codehs" print(int(x)) num = 8 print("num") name = "Alyx" age = 32 print(name + "is " + age) language = "Python" print("I'm learning " + language)

language = "Python" print("I'm learning " + language)

Which of the following Python programs creates a list with the numbers 1 through 5? my_list = (1, 2, 3, 4, 5) my_list = [1, 2, 3, 4, 5] my_list = 1, 2, 3, 4, 5 my_list = "1, 2, 3, 4, 5"

my_list = [1, 2, 3, 4, 5]

What code below will print the following? Letter 1: T Letter 2: i Letter 3: m

name = "Tim" name_list = list(name) for index, value in enumerate(name_list): print("Letter " + str(index+1) + ": " + value)

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 = str(input("Enter a number: ")) num = number(input("Enter a number: ")) num = input("Enter a number: ") num = int(input("Enter a number: "))

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

Which of the following choices is NOT a Python variable type? int str float number

number

If the user enters '4', I want the loop to exit. Where should the break command be placed in order to end the loop when the user enters this value? 1. while True: 2. num = int(input("Enter a number: ")) 3. 4. if num == 10: 5. 6. elif num > 10: 7. 8. else: 9. On line 3 On line 5 On line 7 On line 9

on line 9

Choose the correct declaration of a float variable with the value 3.14. pi = "3.14" pi = int(3.14) pi = 3.14 float pi = 3.14

pi = 3.14

Question: 1 Which Python code segment will display "Hello, world!" on the screen? display Hello, world! print("Hello, world!") print "Hello, world!" "Hello, world!"

print("Hello, world!")

Suppose you have the following dictionary, which stores the names and ages of people: my_dictionary = { "Bo": 32, "Fiona": 28, "Carlos": 45 } Which line correctly prints Bo's age? print(my_dictionary[0]) print(my_dictionary[0][1]) print(my_dictionary["Bo"]) print(my_dictionary["Bo"][1])

print(my_dictionary["Bo"])

Given the following list, my_list = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] which line of code will print the following output? [[3, 4, 5], [6, 7, 8]] print(my_list[1:3]) print(my_list[1:2]) print(my_list[2:3]) print(my_list[2:4])

print(my_list[1:3])

Given the following list, my_list = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ] Which line of code would print the number '5' to the screen? print(my_list[1]) print(my_list[1:2]) print(my_list[1][2]) print(my_list[2][3])

print(my_list[1][2])

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 + ":" ) print(num) print(name + " wants " + "num " + "candies") print(name + ": " + str(num)) print(name + ": " + num)

print(name + ": " + num)

How would you round the number held in the variable pi to 3.14? pi = 3.14159265 round(pi) round(pi, 3) round(3, pi) round(pi, 2)

round(pi, 2)

Which of the following evaluates to the variable x rounded to two decimal places? round(x, 2) round(2, x) round(x), 2 round(2), x

round(x, 2)

Which of the following while loops will cause an infinite loop? secret_num = 10 while secret_num == 10: secret_num = secret_num - 1 secret_num = 10 while secret_num == 10: print(secret_num) secret_num = 10 while secret_num > 0: secret_num = secret_num - 1 secret_num = 10 while secret_num != 10: print(secret_num)

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

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

str

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

string

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

string

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 uSeRaGe user!age! 1user_age

user_age

Which of the following is NOT a program that will produce the following output? hellohellohello var = "hello" * 3 print(var) var = "hello" + "hello" + "hello" print(var) var = "hello" print(var) print(var) print(var) In the following code, assume the user enters 3. num_times = int(input("How many times?: ")) var = "hello" * num_times print(var)

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

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 while num < 12 or num < 3: # do something with num while num <= 12 and num >= 3: # do something with num while num < 12 and num > 3: # do something with num

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

Question: 1 Which of the following Python programs will not run? x = 4 y = 5 print(x + y) x = 4 y = "hi" print(x + y) x = 4 y = 5.5 print(x + y) x = 4 print(x + 5)

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

Refer to the following program. Are x and y equivalent? x = [1, 2, 3] y = x

yes

Refer to the following program. Are x and y equivalent? x = [1, 2, 3] y = x

yes


Set pelajaran terkait

Laskentatoimen kaavat ja tunnusluvut

View Set

Állampolgársági interjú - Magyarországról és a magyar kulturáról / Citizenship interview - About Hungary and Hungarian culture

View Set

Ch. 4 Using Methods and Skills: Studying and Testing

View Set

Lección 8 Adelante: Flash cultura ;Emparejar

View Set

Ch. 7.2: Types and Amounts of Social Security Benefits Available

View Set

Disease, Diagnostic test and screening

View Set

Pre-Calculus Algebra Final Exam Review

View Set

6-1 Risk Assessment and Strategies

View Set