Comp Sci Midterm

Ace your homework & exams now with Quizwiz!

What is the proper format for a single line comment in Karel?

# This is a comment

Why do we use functions in Karel programs?

-Break down our program into smaller parts -Avoid repeating code -Make our program more readable -*All of the above*

*Super Karel starts at Street 1, Avenue 1, facing East in a 5x5 world. What will happen after this code runs?* move() move() turn_right() move()

Karel will crash into a wall

Say you want to write a program to have Karel put down 300 tennis balls. Which control structure would you use?

for loop

Which of the following commands is a valid Karel command?

move()

*If you were given the following variables:* distance = 2 time = 30 *What line of code would print the speed in km/hr if the distance is given in km and the time is given in minutes? (Speed = distance/time)*

print(distance/(time/60))

How many total times will Karel move in this program? move() for i in range(5): move() put_ball()

6

Which of the following is not a valid condition to go in an if statement for Karel?

turn_left()

You are splitting up all of your apples equally between 3 people. Which statement below will calculate how many apples will be left over?

left_over = num_apples % 3

We want to print the phrase "CodeHS is the best" exactly 25 times. What kind of control structure should we use?

For Loop

*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

In a graphics canvas with a size of WIDTH and a height of HEIGHT, what are the coordinates of the center of the window?

WIDTH / 2, HEIGHT / 2

In a graphics canvas with a size of WIDTH and a height of HEIGHT, what are the coordinates of the bottom right corner of the window?

WIDTH, HEIGHT

*What condition should be used in this while loop to get Karel to pick up all the tennis balls on the current location?* while ________ : take_ball()

balls_present()

Choose the best response: A function passed to an event handler that is called every time a certain event happens is called a _______.

callback function.

"It's a bird! It's a plane! No, it's Superman!" We want to write a function is_superman that takes in two parameters is_bird and is_plane and returns True if it is in fact Superman, and False otherwise. If it's not a bird and it's not a plane, it must be Superman. Which of the following functions is the correct implementation of is_superman?

def is_superman(is_bird, is_plane): return not is_bird and not is_plane; }

Which of the following is the correct way to create a function in Python?

def my_function()

Which of the following is the correct way to define a turn_right function in Karel?

def turn_right(): turn_left() turn_left() turn_left()

What is the output of the following program? def sum_to(num): sum = 0 for i in range(num+1): sum += i print(sum) x = 5 sum_to(x) print(x)

15 5

True or false: Functions should have parameters.

False

*Which of the following functions will correctly return True if it is passed an odd integer value for x?* I. def is_odd (int x) : return x % 2 == 1 II. def is_odd (int x) : return x / 2 == 1 III. def is_odd (int x) : if x % 2 == 1: return True else: return False

I and III only

Which statement allows us to return values from functions?

return

*What is printed to the screen if the user enters '5' when the following program is run?* user_num = int(input("Enter your favorite number: ")) calculated_num = user_num * user_num + user_num print(calculated_num)

30

*The following code continually asks the user for a password until they guess the correct password, then ends. But there is one problem.* 1 SECRET_PASSWORD = "karel" 2 3 while True: 4 password = input("Enter your password: ") 5 if password == SECRET_PASSWORD: 6 print("You got it!") 7 print("Incorrect password, please try again.") *Which of the following will fix this program?*

Add a break; statement after line 6 so that the program doesn't loop infinitely

*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") else: if 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

In the program below, the initial value of a is 0 and the initial value of b is 1. IF (b = 0) { IF (b = 1) { DISPLAY ("Karel") } } } ELSE { IF (a = 1) { DISPLAY ("Tracy") } ELSE { DISPLAY ("Dog") } DISPLAY ("Turtle") }

Dog Turtle

*What is printed by the following program?* num_apples = 10 num_oranges = 5 if num_apples < 20 or num_oranges == num_apples: print("Hello, we are open!") else: print("Sorry, we are closed!") print("Sincerely, the grocery store")

Hello, we are open!Sincerely, the grocery store

*What is printed by the following program?* is_raining = False is_cloudy = False is_sunny = not is_raining and not is_cloudy is_summer = False is_warm = is_sunny or is_summer print("Is it warm: " + str(is_warm))

Is it warm: True

*What does the mystery function do?* def mystery(): while no_balls_present(): move()

Karel moves until it is on a ball

What is the purpose of a return statement in a function?

Stops executing the function and returns the value

Consider the code segment below. x ← RANDOM (1, 10) y ← RANDOM (10, 20) DISPLAY (x = y) Which of the following best describes the behavior of the the code?

The program could display either true or false

What does the following code snippet do? Choose the best answer. def click(event): size = 20 circle = Circle(size) circle.set_position(event.x, event.y) add(circle) add_mouse_click_handler(click(event))

This code snippet has an error and will not do anything

*Karel starts at Street 1, Avenue 1, facing East in a 5x5 world. What will happen after this code runs?* move() putball() move() move() move() move() move()

This code won't run because of a syntax error

*In the following code below from the Cleanup Karel example, what is the purpose of If Statement #2?* # This program has Karel walk down the # row and clean up all of the tennis balls # on the way. while front_is_clear(): # If statement #1 if balls_present(): take_ball() move() // If statement #2 if balls_present(): take_ball()

To pick up the ball that is in the last spot, if there is one

What is top down design?

Top down design is a way of designing your program by starting with the biggest problem and breaking it down into smaller and smaller pieces that are easier to solve.

Suppose we've written a function draw_square that we want to call every time the mouse is clicked. How can we do this?

add_mouse_click_handler(draw_square)

*Consider the code snippet:* def print_numbers(first, second, third): print(first) print(second) print(third) print_numbers(12, 17, 65) print_numbers(1, 2, 3) print_numbers(3, 3, 20) *What are names of the parameters?*

first, second, third

Which of the following could be considered a computer?

-Smart phone -Digital coffee maker -Cars -*All of these choices*

*Consider the code snippet:* def draw_circle(radius, color, x, y): circle = Circle(radius) circle.set_color(color) circle.set_position(x, y) add(circle) *What is the return value for this function?*

this function has no return value

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

*Say Karel is on a location with one tennis ball. After the following code runs, how many tennis balls will there be at that location?* for i in range(3): if balls_present(): take_ball() else: put_ball() put_ball()

1

On the AP Exam, the modulus operator is denoted by MOD. The MOD operator has the same precedence as the * and / operators. What will c evaluate to in the following code segment? a ← 18 b ← 4 c ← a MOD b

2

Consider the code segment below. PROCEDURE Mystery (number) { RETURN ((number MOD 2) = 0) } Which of the following best describes the behavior of the Mystery PROCEDURE?

Returns whether or not number is even

*Which of the following is true for an API?* I. Allows different devices to connect with each other II. Allows for Interactivity between devices and software III. Messenger program that takes requests and delivers the response back to the user IV. Allows the creation of applications that access the features or data of an operating system, application, or other servicesV. Specifies how software components should interact VI. Used when programming graphical user interface (GUI) components

I, II, III, IV, V, and VI

*Why do we write functions?* I. Make our code easier to understand by giving a readable name to a group of instructions II. Avoid writing repeated code III. Make our code reusable

I, II, and III

Consider the code segment below. PROCEDURE Mystery (number) { DISPLAY ("WOW") REPEAT number TIMES { DISPLAY ("!") } } What is the result of calling the Mystery procedure with an input of 3?

WOW ! ! !

*We want to position a Circle on our screen to be at a random position, but we want to keep the entire shape on the screen. Which of the following will accomplish this given the following function? (Assume SIZE, HEIGHT, and WIDTH are properly defined)* def draw_circle(x, y, radius): circle = Circle(radius) circle.set_position(x, y) add(circle)

x = random.randint(SIZE,WIDTH-SIZE) y = random.randint(SIZE,HEIGHT-SIZE) draw_circle(x, y, SIZE)

What will the following program print when run? for j in range(2): for i in range(6, 4, -1): print (i)

6 5 6 5

*What is printed by the following program?* def product(x, y): return x * y def difference(x, y): return x - y x = 2 y = 5 value1 = product(x, y) value2 = difference(y, x) result = difference(value1, value2) print(result)

7

*What does the following program do?* radius = int(input("What is the radius of the circle? ")) circle = Circle(radius) circle.set_position(radius, radius) a dd(circle)

Draw a circle on the canvas that is radius distance in the x and y direction from the top left corner

temp ← x x ← y y ← temp Based on the code segment, how would you best describe what the program is doing to variable x and variable y?

The program swaps the value of x and y

*How can we improve the following program?* move() move() move() move() move() move() move() move() move()

Use a for loop to repeat the move command

*What will be the output when the following code runs?* logged_in = False print("User logged in?: " + str(not logged_in))

User logged in?: True

Consider the code segment below. DISPLAY ("What is your name?") name ← INPUT () DISPLAY ("Hello") DISPLAY (name) What is displayed as a result if the user inputs "Karel" to the program?

What is your name? Hello Karel

*On the AP exam, INPUT() is used to accept a value from the user, and DISPLAY() is used to display a value. Values that are displayed are NOT started on a new line, but a space is added after the value.* *Consider the code segment below.* DISPLAY ("What is your name?") name ← INPUT () DISPLAY ("Hello") DISPLAY (name) *What is displayed as a result if the user inputs "Rowan" then hits enter into the program?*

What is your name? Rowan Hello Rowan

We want to simulate constantly flipping a coin until we get 3 heads in a row. What kind of loop should we use?

While Loop

The following program should draw a circle on the screen def draw_circle(x, y): circle = Circle(100) circle.set_position(x, y) *But when we run this code, we don't see the circle. What is missing?*

You need to add the circle with the add(circle) command.

Which of the following lines of code would be considered the highest level of code?

move()

*What is printed by the following program?* def print_numbers(two, one, zero): print(two) print(one) print(zero) zero = 0 one = 1 two = 2 print_numbers(zero, one, two)

0 1 2

*What is printed to the screen when the following program is run?* num = 13 % 3 print(num)

1

*What will the following code segment evaluate to?* a = 10 b = 5 c = 2 expression = ((a - b) * c) % 3 print(expression)

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 output of the following program?* result = 0 max = 5 for i in range(max): result += i print(result)

10

*Consider the following code snippet:* WIDTH = 300 HEIGHT = 300 def draw_circle(radius, color, x, y): circle = Circle(radius) circle.set_color(color) circle.set_position(x, y) add(circle) draw_circle(40, "red" 100, 300) draw_circle(50, "green", 50, 100) draw_circle(70, "blue", WIDTH/2, HEIGHT/2) draw_circle(25, "yellow", WIDTH/2, 300) *In the third call to draw_circle(), what values are passed to the coordinates of the center of the circle?*

150, 150

*What is the last thing printed by the following program?* start = 30 stop = 10 for i in range(start, stop - 1, -5): if i % 2 == 0: print(i * 2) else: print(i)

20

On the AP exam, the ← operator is used for variable assignment. For example, a ← 10 assigns the value 10 to the variable a. The value stored in a variable will always be the most recent value assigned. Consider the following code segment. a ← 12 b ← 3 c ← 27 a ← b b ← c c ← a What value is stored in variable c?

3

*How many parameters go into the function sum, and how many return values come out of the function sum?* def sum(first, second, third): result = first + second + third print(first) print(second) print(third) return result

3 parameters go in, 1 return value comes out

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

5

What is wrong with this for loop? for i in range = 5; move() A. The for loop uses a semicolon(;) instead of colon(:) B. It should say range(5) instead of range = 5

A and B

What does API stand for in computer science?

Application Programming Interface

*In this code, how many times is the dance function called and how many times is it defined?* def dance(): turn_left() move() turn_left() turn_left() move() turn_left() move() dance() move() move() turn_left() dance() dance()

Called 3 times, defined 1 time

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

FlorenceFernandez

Which of the following is true about return values?

Functions can have return values, but they are optional.

In the following code, what would be a good Postcondition to write? """ Precondition: Karel is on a spot with a tennis ball facing East * Postcondition: ... """ def get_on_top(): turn_left() move() turn_right()

Karel ends one spot above a tennis ball facing East

*If Karel starts at Street 1 and Avenue 1, facing East, where will Karel be, and what direction will Karel be facing after running the following code? (Assume the world is 10x10 in size)* move() turn_left() put_ball() turn_left() turn_left() turn_left() move() turn_left()

Street 1, Avenue 3, Facing North

*Karel starts at Street 1 and Avenue 1, facing East. After calling the stair_step function twice, where will Karel be and what direction will Karel be facing? (assume this is a SuperKarel program and the world is 10x10 in size)* def stair_step(): move() turn_left() move() turn_right()

Street 3, Avenue 3, Facing East

*In the following code:* size = 20 x = 100 y = 200 ball = Circle(size) ball.set_position(x, y) *What is the meaning of the x variable?*

The x coordinate of the center of the circle

*How many times will the following program print "hello"?* i = 0 while i < 10: print("hello")

This code will loop infinitely

*Consider the following code snippet:* def mystery1(x): result = x + 1 return result def mystery2(x, y): result = x + y return result def mystery3(x): result = x * x return result *What would the following print?* y = mystery2(12) print(y)

This would generate an error

What is the purpose of using a for loop in code?

To repeat something a fixed number of times

The AP Exam uses the following relational (comparison) operators: =, ≠, >, <, ≥, and ≤.As well, AND, OR and NOT are used instead of and, or and not. A comparison using a relational operator evaluates to a Boolean value. For example, a = b evaluates to true if a and b are equal; otherwise, it evaluates to false. Determine what the following expression would evaluate to.

a AND (b OR c)

A store has 20 apples in its inventory. How can you store this information in a Python variable?

num_apples = 20

*What does the mystery function do in the code snippet below?* def mystery(x, y): if x < y: return x else: return y a = mystery(10, 14) b = mystery(82, 28) print(a) print(b)

returns the smaller of two values passed to it

In the following function print_three_times: def print_three_times(word): print(word) print(word) print(word) *What is the parameter of the function?*

word

*If you wanted to use the following code to draw a circle to the canvas with the radius, and x and y coordinates given by the user, how would you ask for this information?* circle = Circle(radius) circle.set_position(x, y)

x = int(input("What is the x coordinate? ")) y = int(input("What is the y coordinate? ")) radius = int(input("What is the radius of the circle? "))

How can we teach Karel new commands?

Define a new function

*What is the value of the boolean variable can_vote at the end of this program* age = 17 is_citizen = True can_vote = age >= 18 and is_citizen

False

*What's wrong with this code?* def go(): move() move() def go(): move() move() move() go() go()

The go function has been defined twice

*The following code should draw a circle on the screen.* def draw_circle(x, y, radius): circle = Circle(radius) circle.set_position(x, y) *But when we run this code, we don't see the circle. What is missing?*

To add the circle to the canvas, you need to use the add(circle) command

You want to read input from the user to know how many apples they would like to buy. Which statement should you use to read in a number from the user?

apples_to_buy = int(input("How many apples would you like? "))

If a user enters a diameter value in the variable diameter, how would we use this to draw the correct sized circle?

circle = Circle(diameter / 2)

*In the following code:* MAX_NUMBER = 10 def sum(x, y): result = x + y //Point A return result num1 = random.randint(0, MAX_NUMBER); num2 = random.randint(0, MAX_NUMBER); print( sum(num1, num2) ); *Which variables exist at point A? In other words, which variables are in scope at point A? Which variables could you type at point A and the program would still work?*

result, x, y, and MAX_NUMBER

Why does a programmer indent their code?

-Helps show the structure of the code. -Easier for other people to understand. -In Python, it is used to help determine what parts of a code are included in a code block, such as an if statement or for loop. -*All of the above*

*Consider the following code snippet. What is returned and printed for the value of a?* def mystery(x, y) { if x < y: return x else: return y a = mystery(10, 14) b = mystery(82, 28) print(a) print(b)

10

What will be printed from the following: x = 4 * 3 + 2 / 5 print(x)

12.4

What makes the following command an invalid Karel command?

The L should be a lower l

In the following code: size = 20 x = 100 y = 200 ball = Circle(size) circle.set_position(x, y) *What is the meaning of the x variable?*

The x position of the center circle


Related study sets

Biology Ch. 1-3 Concepts and Connections

View Set

Ch. 33 - Disclosures, Notices, and Inspections

View Set

PEDs: Nursing Care of the Child with a Cardiovascular Disorder

View Set

Intro to Business: Chapter 14 - Developing and Pricing Goods and Services

View Set

Practice Questions: Hypertension

View Set