wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Subprograms & if statements

Total questions: 44

Worksheet time: 23mins

Name
Class
Date
1.

Which of the following is the correct way to write a basic if statement in Python to check if a variable `age` is greater than 14?

a)

if age > 14 then:

b)

if age > 14:

c)

if (age > 14)

d)

if age => 14:

2.

What does the following if statement do? ```python if temperature < 0: print("It's freezing!") ```

a)

Prints "It's freezing!" if the temperature is above 0°C

b)

Prints "It's freezing!" if the temperature is below 0°C

c)

Prints "It's freezing!" if the temperature is exactly 0°C

d)

Always prints "It's freezing!"

3.

Which of these is a comparative operator used in if statements?

a)

++

b)

==

c)

&&

d)

**

4.

What will be printed by the following code?

a)

Passed

b)

Failed

c)

Nothing

d)

Error

5.

Which of the following is NOT a valid comparative operator in Python?

a)

>

b)

!=

c)

=>

d)

<=

6.

What is the error in this if statement?

a)

The colon is missing

b)

The comparison operator should be ==

c)

The print statement is incorrect

d)

There is no error

7.

Which of the following if statements checks if the variable `weight` is less than 10 kg?

a)

if weight < 10:

b)

if weight < 10kg:

c)

if weight > 10:

d)

if weight == 10:

8.

What will the following code output?

a)

Not ten

b)

10

c)

Nothing

d)

Error

9.

Which of these is a common mistake when writing if statements in Python?

a)

Using a colon at the end of the if line

b)

Using = instead of == for comparison

c)

Indenting the code after the if statement

d)

Using comparative operators

10.

What does the following code do?

a)

Prints "False"

b)

Prints "True"

c)

Prints both "True" and "False"

d)

Generates an error

11.

Which of the following will check if the variable `distance` is exactly 1 mile?

a)

if distance = 1:

b)

if distance == 1:

c)

if distance != 1:

d)

if distance > 1:

12.

What is the output of this code?

a)

Hot

b)

Not hot

c)

20

d)

Error

13.

Which of the following is the correct way to check if `ounces` is not equal to 16?

a)

if ounces != 16:

b)

if ounces <> 16:

c)

if ounces =! 16:

d)

if ounces not 16:

14.

What will happen if you forget to indent the code after an if statement in Python?

a)

The code will run as normal

b)

Python will give an IndentationError

c)

The if statement will be ignored

d)

The code will print twice

15.

Which of these if statements will check if `marks` is less than or equal to 40?

a)

if marks < 40:

b)

if marks <= 40:

c)

if marks => 40:

d)

if marks == 40:

16.

What is wrong with this if statement?

a)

The colon is missing at the end of the if line

b)

The brackets are not allowed

c)

Missing indentation

d)

There is nothing wrong

17.

Which of the following will check if `volume` is exactly 8 ounces?

a)

if volume == 8:

b)

if volume = 8:

c)

if volume != 8:

d)

if volume > 8:

18.

What will the following code?

a)

Too fast

b)

Safe speed

c)

30

d)

Error

19.

Which of these is the correct way to check if `length` is not equal to 1 metre?

a)

if length != 1:

b)

if length =! 1:

c)

if length <> 1:

d)

if length not 1:

20.

Which of the following is the correct way to assign the value 5 to a variable called `apples` in Python?

a)

apples == 5

b)

apples = 5

c)

5 = apples

d)

apples := 5

21.

What will be the value of `total` after the following code runs?

a)

2

b)

3

c)

5

d)

6

22.

Which of these variable names is NOT valid in Python?

a)

my_score

b)

2ndPlace

c)

totalMarks

d)

student_name

23.

What is the value of `y` after this code runs?

a)

7

b)

2

c)

14

d)

9

24.

What type of value is stored in the variable `name` after this code? ```python name = "Alice" ```

a)

Integer

b)

String

c)

Boolean

d)

Float

25.

Which of the following is the correct way to increase the value of `count` by 1?

a)

count =+ 1

b)

count = count + 1

c)

count == count + 1

d)

count := 1

26.

What will be printed by the following code?

a)

7

b)

12

c)

43

d)

1

27.

Which of the following is the correct way to get input from a user and store it in a variable called `name`?

a)

name = input("Enter your name: ")

b)

input = name("Enter your name: ")

c)

name == input("Enter your name: ")

d)

name := input("Enter your name: ")

28.

If you want to ask the user for their age and store it as an integer in the variable `age`, which code should you use?

a)

age = input("Enter your age: ")

b)

age = int(input("Enter your age: "))

c)

age = str(input("Enter your age: "))

d)

age = float(input("Enter your age: "))

29.

What will be the type of the variable `number` after this code runs? ```python number = input("Type a number: ") ```

a)

Integer

b)

String

c)

Float

d)

Boolean

30.

What will be printed if the user enters `7` for the following code?

a)

10

b)

73

c)

Error

d)

3

31.

Which of the following will cause an error if the user enters the word `ten`?

a)

age = input("Enter your age: ")

b)

age = int(input("Enter your age: "))

c)

name = input("Enter your name: ")

d)

colour = input("Enter your favourite colour: ")

32.

What is wrong with this code?

a)

Nothing

b)

Colon

c)

Assignment Operator

d)

Indentation

e)

Something else.

33.

How many variables are in this subprogram?

a)

3

b)

4

c)

1

d)

2

34.

Identify the name of this subprogram:

a)

def

b)

values

c)

error

d)

values( )

35.

On which line does this program call the subprogram 'values'?

a)

1

b)

1,2,3,4

c)

6

d)

The program is not correctly called.

36.

'def' is short for?

(a)  

37.

What will happen when you run this program

a)

It will call a subprogram that asks you to enter two integers, then output each of these.

b)

It will define and call a subprogram that asks you to enter two integers, then output each of these.

c)

It will generate an error due to missing assignment operators.

d)

It will generate an error due to something else.

38.

Which subprogram runs first?

a)

'welcome' then 'goodbye'

b)

'goodbye' then 'welcome'

c)

Neither - programs are not correctly called.

d)

Error for a different reason.

39.

What software do we use when programming? (Use capital letters)

(a)  

40.

True or False? You can change values stored in a variable.

a)

True

b)

False

c)

Sometimes

41.

True or False? Variables can be declared within and outside of a subprogram.

a)

True

b)

False

c)

Yes but only using 'global'

42.

True or False? int( ) is used for casting.

a)

True

b)

False - it is used for turning something into an integer

c)

False - it is used for a different reason.

43.

True or False? You can only create three internal subprograms.

a)

True

b)

False

c)

Depends on memory.

44.

Subprograms are an example of

a)

Abstraction

b)

Decomposition

c)

Algorithmic Thinking

d)

Computational Thinking