wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Code.org Foundations of AI Programming Unit 2 Lesson 6-9

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

Which of the following correctly defines a function with a parameter?

a)

def greet(name): print("Hello, " + name)

b)

print("Hello, name")

c)

name = input("Hello")

d)

greet = "Hello, name"

2.

Which of the following is an example of a syntax error?

a)

Forgetting to define a function

b)

Writing an infinite loop

c)

Running a program without output

d)

Using incorrect indentation

3.

Which type of loop runs as long as a condition remains true?

a)

if loop

b)

while loop

c)

else loop

d)

function loop

4.

Which of the following best describes the difference between a function and a method in Python?

a)

A method performs a task, while a function cannot

b)

A function belongs to an object, but a method does not

c)

A method is defined with a different key word than a function

d)

A method is attached to an object, while a function is not.

5.

What is missing from this function definition?

def greet()

print("Hello")

a)

The def keyword

b)

A colon : at the end of the function header

c)

Parentheses around "Hello!"

d)

The function name

6.

The best definition of snake_case?

a)

An action that an object can do

b)

A naming style where all letters are lowercase and words are separated by underscores

c)

A named block of code that performs a task.

d)

A blueprint or set of instructions for creating something that can do specific tasks

7.

The best definition of method?

a)

An action that an object can do

b)

A naming style where all letters are lowercase and words are separated by underscores

c)

A named block of code that performs a task.

d)

A blueprint or set of instructions for creating something that can do specific tasks

8.

The best definition of function?

a)

An action that an object can do

b)

A naming style where all letters are lowercase and words are separated by underscores

c)

A named block of code that performs a task.

d)

A blueprint or set of instructions for creating something that can do specific tasks

9.

The best definition of class in Python programming?

a)

An action that an object can do

b)

A naming style where all letters are lowercase and words are separated by underscores

c)

A named block of code that performs a task.

d)

A blueprint or set of instructions for creating something that can do specific tasks

10.

How is a parameter defined in Python Programming?

a)

A placeholder in your function

b)

A .py file that contains Python code

c)

The actual value you pass into a function when you call it

d)

A named block of code that performs a task.

11.

How is an argument defined in Python Programming?

a)

A placeholder in your function

b)

A .py file that contains Python code

c)

The actual value you pass into a function when you call it

d)

A named block of code that performs a task.

12.

Which of the following best explains the difference between a parameter and an argument?

a)

A parameter is a placeholder in a function definition; an argument is the value passed to that parameter when the function is called.

b)

A parameter is the value passed to a function; an argument is the variable in the function definition.

c)

A parameter and an argument are always the same thing in programming.

d)

A parameter is used only in loops; an argument is used only in conditionals.

13.

Which of the following best describes a Python module?

a)

A built-in Python library that cannot be modified by the user

b)

A .py file that contains Python code and can be imported into other Python files.

c)

A .txt file that stores variables for use in other Python programs

d)

A folder that contains multiple Python programs and executes them automatically.

14.

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?

a)

paint_dot(kai)

b)

kai.paint_dot()

c)

paint_dot(this_painter) = "kai"

d)

this_painter(kai)

15.

What is the purpose of the function paint_dot shown below?

def paint_dot(this_painter):

this_painter.take_paint()

this_painter.paint("SkyBlue")

a)

It creates a new painter object named SkyBlue.

b)

It instructs this_painter to pick up paint and paint using the color SkyBlue.

c)

It returns the color that the painter is currently using.

d)

It changes the painter’s name to “SkyBlue”

16.

What are two things that must be true for a while loop to work correctly in Python?

a)

The loop must include at least three lines of code and use the keyword repeat.

b)

The loop must use the while keyword with a Boolean condition and indent the code you want to repeat.

c)

The loop must have a condition that ends with a semicolon and use all capital letters.

d)

The loop must only include one command and use a variable that increases by one each time.

17.

What is the correct definition of loop?

a)

A way to tell a computer to repeat a set of instructions

b)

Something that has attributes (what it knows) and methods (what it can do)

c)

A rule that the computer checks to decide if it should keep going

d)

An action that an object can do

18.

What is the correct definition of object?

a)

A way to tell a computer to repeat a set of instructions

b)

Something that has attributes (what it knows) and methods (what it can do)

c)

A rule that the computer checks to decide if it should keep going

d)

An action that an object can do

19.

What is the correct definition of condition?

a)

A way to tell a computer to repeat a set of instructions

b)

Something that has attributes (what it knows) and methods (what it can do)

c)

An action that an object can do

d)

A rule that the computer checks to decide if it should keep going

20.

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?

a)

The Painter will move and paint each tile until it reaches a wall.

b)

The Painter will paint the starting tile once, then stop.

c)

The Painter will keep painting the same tile over and over

d)

The Painter will paint the tile, but it won’t move forward and will crash.

21.

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?

a)

It will print hello world successfully

b)

It will run with no errors but print nothing

c)

It will cause an IndentationError because the print statement is not indented.

d)

It will create a function but automatically call it.

22.

Write the command that will create zoe as a Painter?

a)

zoe = Painter()

b)

Painter = zoe()

c)

create Painter zoe

d)

zoe.create(Painter)

23.

Which of the following is the correct way to define a turn_right() function that includes a parameter for the painter's name?

a)

def turn_right(painter_name):

b)

def turn_right():

c)

def turn_right(name, painter):

d)

def turn_right(painter):

24.

Write the command to import the turn_right and keep_moving functions from custom.

a)

from custom import turn_right, keep_moving

b)

import turn_right, keep_moving from custom

c)

from custom import turn_right keep_moving

d)

import custom.turn_right, custom.keep_moving

25.

Which function turns the Painter to the right?

a)

def turn_right(painter): painter.turn_left() painter.turn_left() painter.turn_left()

b)

def turn_right(painter): painter.turn_right() painter.turn_right() painter.turn_right()

c)

def turn_right(painter): painter.turn_left() painter.turn_left()

d)

def turn_right(painter): painter.turn_left() painter.turn_right() painter.turn_left()

26.

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

a)

If painter.can_paint():

painter.turn_right()

painter.move()

b)

if zoe.is_on_paint():

turn_right(zoe)

zoe.move()

c)

if zoe.is_on_paint():

zoe.turn_right()

zoe.move_forward()

d)

If painter.has_paint():

turn_right(painter)

move(painter)

27.

What is a rule that the computer checks to decide if it should keep going?

a)

condition

b)

condition statement

c)

parameter

d)

argument

28.

Which of the following is required immediately after the condition in a Python If statement?

a)

A question mark (?)

b)

A period (.)

c)

Parentheses ()

d)

A colon (:)

29.

What will happen if the condition in an if statement is false?

a)

The program stops running entirely

b)

Python will raise an error

c)

The indented block under the if is skipped

d)

The indented block runs twice

30.

What will happen when this code runs?

while painter.can_move():

painter.paint("blue")

a)

The Painter will move and paint each tile until it reaches a wall

b)

The Painter will paint the starting tile once, then stop

c)

The Painter will keep painting the same tile over and over, but it won't move

d)

The Painter will paint each tile, but it won't move forward and may crash