Computer Science Principles Semester 1 Final Exam

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

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

#

Which program below includes no errors? # Draw a square # for i in range(4): left(90) forward(40) # Draw a square def draw square: for i in range(4): left(90) forward(90) # Draw a square for i in range(4): left(90) forward(90) # Draw a square def draw square(): for i in range(4): left(90) forward(90)

# Draw a square for i in range(4): left(90) forward(90)

Suppose you want to make Tracy draw a mountain range, like the one shown below, starting from the left side. Which of the following functions would be the most useful function to write in order to solve this problem?

# Has Tracy draw a single triangle def make_triangle():

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)

-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

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 this program print? for i in range(10): if i == 3: continue print(i)

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 will be the radii of the circles drawn from the following code? for i in range(10, 55, 10): circle(i)

10, 20, 30, 40, 50

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?

19

What does the following code print?

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

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

When the following for loop is complete, how many spaces will Tracy have moved? for i in range(5): forward(10)

50 spaces

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

8

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

9

What is the final result of the expression 7 / 2 + 6?

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

<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 does the following program print? a = "hi" b = 4 c = a * b print(type(c))

<type 'str'>

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 control structure would be best to use in the following code? backward(100) right(90) backward(100) right(90) backward(100) right(90) backward(100) right(90)

A for loop

What shape will be drawn with the following command? circle(50, 360, 3)

A triangle

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, C, D, B

Which of the following statements are true about for loops? A. By default, the range function starts at 0 B. Using for i in range(4) will result in i taking the values 0, 1, 2, 3, 4 C. For loops let you repeat something any number of times D. Statements in a for loop do not need to be indented E. It is not possible to have the range values count 6, 12, 18, 24F. It is not possible to have the range values count 1, 2, 4, 8, 16

A, C, and F

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

What will be the output of the following code? count = 5 while count > 0: circle(50) forward(100)

An infinite loop will occur

Suppose you write a function. How many times can you call the function in your code?

As many times as you want

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!

What is the difference between defining and calling a function?

Defining a function means you are teaching the computer a new word. Calling a function means you are commanding the computer to complete defined actions.

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

FlorenceFernandez

Which of the following is NOT a purpose of using functions?

Functions let you execute code a fixed number of times.

What does the number in the parentheses in a forward or backward command represent?

How far Tracy is supposed to move Answered

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, II, IV

Which of the statements below is true about indentation in Python?

Indentation always matters in Python. Every statement must be aligned correctly.

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

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

What kind of data does a float variable contain?

Numbers that can have decimal components

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 9

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.

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 program will break out of the loop as soon as the user enters the magic_number

What does this program do? speed = int(input("What is your speed? ")) if speed > 80: print("You will get a reckless driving charge. ") elif speed > 60: print("You will get a speeding ticket. ") else: print("You are driving at a safe speed. ")

This program lets the user know if they are driving at a safe speed or not. It asks the user for their speed, and if it it above 60, it prints 'You will get a speeding ticket.' If it is above 80, it will print "You will get a reckless driving charge.'

What will be the output of the following program? for i in range(3, 7, 2): circle(50, 360, i)

Two shapes- a triangle (3 sides) and a pentagon (5 sides). Answered

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

Can a user's input control the size of a circle? If so, how?

Yes radius = int(input("Radius: ")) circle(radius)

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

In which of the following situations would it be best to make a function?

You want Tracy to draw a blue line, and your program requires lots of blue lines.

Which one of the statements below will cause an error?

ans = "hi" + 9

Which command will Tracy perform when given the following code? count = 5 count = count * 2 if count < 10: forward(count) elif count < 20: backward(count) else: circle(count)

backward(10)

Which command will Tracy perform when given the following code? count = 200 count = count + 1 if count % 2 == 0: forward(count) backward(count)

backward(201)

What is the most effective way to draw three circles in a row of different colors?

def draw_circle(color_choice): pendown() color(color_choice) begin_fill() circle(50) end_fill() penup() forward(100) draw_circle("red") draw_circle("blue") draw_circle("yellow")

Which of the following functions is defined correctly? A. def draw_edge(): forward(100) left(90) B. def draw_edge(): forward(100) left(90) C. def draw_edge forward(100) left(90) D. def draw_edge(){ forward(100) left(90) }

def draw_edge(): forward(100) left(90)

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)

Which of the following commands can NOT be used to draw one square?

for i in range(4): circle(50, 360, 4)

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(6): print(i)

Which of the following pieces of code will make Tracy do the following actions three times: go forward, change colors, and then turn around.

forward(30) color("blue") left(180) forward(30) color("green") left(180) forward(30) color("orange") left(180)

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 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: "))

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

number

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

pi = 3.14

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)

How can we use variable to control the size of a circle?

radius = 50 circle(radius)

Which of the following commands can be used to turn Tracy to face North if she is initially facing South?

right(180)

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)

Write a program that will have the user determine what shape Tracy draws to the screen. 1. Ask the user to select either 'circle', 'triangle', or 'square.' 2. If they type 'circle', tracy draws a circle. If they type 'triangle', tracy draws a triangle, If they type 'square', she draws a square. 3. if they type anything else, they well be notified that they have entered an invalid input.

shape = input("What shape would you like to be drawn, Circle, Triangle, or Square? :") if shape == "Triangle": left(180) circle(90, 360, 3) elif shape == "Circle": circle(90) elif shape == "Square": left(135) circle(90,360,4) else: print("Invalid shape. Please try again.")

Rewrite this program to include a response of "You are driving too slow for the highway" if the driver's speed is under 40mph.

speed = int(input("What is your speed? ")) if speed > 80: print ("You will get a reckless driving charge. ") elif speed < 40: print("You are driving too slow for the highway. ") else: print ("You are driving at a safe speed. ")

Rewrite this program to include a variable that holds the speed limit value (set the value to 65). Calculate all responses based on the following: 1. Reckless driving: More than 20mph over the speed limit 2. Speeding ticket: Up to 20mph above speed limit 3. Too slow: 20mph under the speed limit.

speed_limit = 65 speed = int(input("What is your speed? ")) if speed > 85: print ("You will get a reckless driving charge. ") elif speed > 65 and speed <= 85: print ("You will get a speeding ticket. ") elif speed < 45: print ("You are driving too slow for the highway." ) else: print ("You are driving at a safe speed. ")

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

str

Which of the following is NOT a command you can give to Tracy?

turn

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


Conjuntos de estudio relacionados

Exercise Physiology Chapter 7 Nervous System

View Set

(LearningCurve 11b) Becoming Your Own Person- psy 200 - chapter 11

View Set

Weeks 8, 9, and 10 Practice quiz #1

View Set

Chapter 9 Establishment of a new government.

View Set

Topic 8: Water and Electrolyte Minerals

View Set

Simple Nursing: Congenital heart defects, stroke, PE, chest tubes

View Set