NEW
Font size
WorksheetsCode.org Foundations of AI Programming Unit 2 Lesson 6-9
Total questions: 30
Worksheet time: 15mins
Which of the following correctly defines a function with a parameter?
def greet(name): print("Hello, " + name)
print("Hello, name")
name = input("Hello")
greet = "Hello, name"
Which of the following is an example of a syntax error?
Forgetting to define a function
Writing an infinite loop
Running a program without output
Using incorrect indentation
Which type of loop runs as long as a condition remains true?
if loop
while loop
else loop
function loop
Which of the following best describes the difference between a function and a method in Python?
A method performs a task, while a function cannot
A function belongs to an object, but a method does not
A method is defined with a different key word than a function
A method is attached to an object, while a function is not.
What is missing from this function definition?
def greet()
print("Hello")
The def keyword
A colon : at the end of the function header
Parentheses around "Hello!"
The function name
The best definition of snake_case?
An action that an object can do
A naming style where all letters are lowercase and words are separated by underscores
A named block of code that performs a task.
A blueprint or set of instructions for creating something that can do specific tasks
The best definition of method?
An action that an object can do
A naming style where all letters are lowercase and words are separated by underscores
A named block of code that performs a task.
A blueprint or set of instructions for creating something that can do specific tasks
The best definition of function?
An action that an object can do
A naming style where all letters are lowercase and words are separated by underscores
A named block of code that performs a task.
A blueprint or set of instructions for creating something that can do specific tasks
The best definition of class in Python programming?
An action that an object can do
A naming style where all letters are lowercase and words are separated by underscores
A named block of code that performs a task.
A blueprint or set of instructions for creating something that can do specific tasks
How is a parameter defined in Python Programming?
A placeholder in your function
A .py file that contains Python code
The actual value you pass into a function when you call it
A named block of code that performs a task.
How is an argument defined in Python Programming?
A placeholder in your function
A .py file that contains Python code
The actual value you pass into a function when you call it
A named block of code that performs a task.
Which of the following best explains the difference between a parameter and an argument?
A parameter is a placeholder in a function definition; an argument is the value passed to that parameter when the function is called.
A parameter is the value passed to a function; an argument is the variable in the function definition.
A parameter and an argument are always the same thing in programming.
A parameter is used only in loops; an argument is used only in conditionals.
Which of the following best describes a Python module?
A built-in Python library that cannot be modified by the user
A .py file that contains Python code and can be imported into other Python files.
A .txt file that stores variables for use in other Python programs
A folder that contains multiple Python programs and executes them automatically.
You wrote the following function in main.py
def paint_dot(this_painter):
this_painter.take_paint()
this_painter.paint("SkyBlue")
What do you need to do to call this function with a Painter named kai?
paint_dot(kai)
kai.paint_dot()
paint_dot(this_painter) = "kai"
this_painter(kai)
What is the purpose of the function paint_dot shown below?
def paint_dot(this_painter):
this_painter.take_paint()
this_painter.paint("SkyBlue")
It creates a new painter object named SkyBlue.
It instructs this_painter to pick up paint and paint using the color SkyBlue.
It returns the color that the painter is currently using.
It changes the painter’s name to “SkyBlue”
What are two things that must be true for a while loop to work correctly in Python?
The loop must include at least three lines of code and use the keyword repeat.
The loop must use the while keyword with a Boolean condition and indent the code you want to repeat.
The loop must have a condition that ends with a semicolon and use all capital letters.
The loop must only include one command and use a variable that increases by one each time.
What is the correct definition of loop?
A way to tell a computer to repeat a set of instructions
Something that has attributes (what it knows) and methods (what it can do)
A rule that the computer checks to decide if it should keep going
An action that an object can do
What is the correct definition of object?
A way to tell a computer to repeat a set of instructions
Something that has attributes (what it knows) and methods (what it can do)
A rule that the computer checks to decide if it should keep going
An action that an object can do
What is the correct definition of condition?
A way to tell a computer to repeat a set of instructions
Something that has attributes (what it knows) and methods (what it can do)
An action that an object can do
A rule that the computer checks to decide if it should keep going
A student writes the following loop to move the Painter forward and paint each tile until the Painter reaches a wall:
while painter.can_move():
painter.paint("blue")
What will happen when this code runs?
The Painter will move and paint each tile until it reaches a wall.
The Painter will paint the starting tile once, then stop.
The Painter will keep painting the same tile over and over
The Painter will paint the tile, but it won’t move forward and will crash.
A student writes the following code to create a hello_world function.:
def hello_world():
print("hello world")
What will happen when this code runs?
It will print hello world successfully
It will run with no errors but print nothing
It will cause an IndentationError because the print statement is not indented.
It will create a function but automatically call it.
Write the command that will create zoe as a Painter?
zoe = Painter()
Painter = zoe()
create Painter zoe
zoe.create(Painter)
Which of the following is the correct way to define a turn_right() function that includes a parameter for the painter's name?
def turn_right(painter_name):
def turn_right():
def turn_right(name, painter):
def turn_right(painter):
Write the command to import the turn_right and keep_moving functions from custom.
from custom import turn_right, keep_moving
import turn_right, keep_moving from custom
from custom import turn_right keep_moving
import custom.turn_right, custom.keep_moving
Which function turns the Painter to the right?
def turn_right(painter): painter.turn_left() painter.turn_left() painter.turn_left()
def turn_right(painter): painter.turn_right() painter.turn_right() painter.turn_right()
def turn_right(painter): painter.turn_left() painter.turn_left()
def turn_right(painter): painter.turn_left() painter.turn_right() painter.turn_left()
Write an if statement that checks if the Painter zoe is on the paint bucket. If the condition is true, zoe should turn_right() and move()
If painter.can_paint():
painter.turn_right()
painter.move()
if zoe.is_on_paint():
turn_right(zoe)
zoe.move()
if zoe.is_on_paint():
zoe.turn_right()
zoe.move_forward()
If painter.has_paint():
turn_right(painter)
move(painter)
What is a rule that the computer checks to decide if it should keep going?
condition
condition statement
parameter
argument
Which of the following is required immediately after the condition in a Python If statement?
A question mark (?)
A period (.)
Parentheses ()
A colon (:)
What will happen if the condition in an if statement is false?
The program stops running entirely
Python will raise an error
The indented block under the if is skipped
The indented block runs twice
What will happen when this code runs?
while painter.can_move():
painter.paint("blue")
The Painter will move and paint each tile until it reaches a wall
The Painter will paint the starting tile once, then stop
The Painter will keep painting the same tile over and over, but it won't move
The Painter will paint each tile, but it won't move forward and may crash
