wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Understanding If Statements in Python

Total questions: 70

Worksheet time: 39mins

Name
Class
Date
1.

What is the purpose of an if statement in Python?

a)

To execute code conditionally based on a boolean expression.

b)

To define functions in Python.

c)

To create loops in the code.

d)

To handle exceptions and errors.

2.

How do you write a basic if statement in Python?

a)

if condition: do_something

b)

if condition do_something

c)

if condition: do_something()

d)

if (condition) { do_something }

3.

What will the following code output: if x > 10: print('High')?

a)

Depends on the value of x; prints 'High' if x > 10, otherwise no output.

b)

Prints 'High' only if x == 10

c)

Always prints 'High' regardless of x

d)

Prints 'Low' if x < 10

4.

What is the function of the else statement in Python?

a)

The else statement is used to define a function in Python.

b)

The else statement is used to handle exceptions in Python.

c)

The else statement executes code when the if condition is false.

d)

The else statement always executes regardless of the if condition.

5.

How do you write an if-else statement in Python?

a)

condition ? do_something : do_something_else;

b)

if (condition) { do_something } else { do_something_else }

c)

if condition then do_something;

d)

if condition: do_something else: do_something_else

6.

What will the following code output if x is 5: if x > 10: print('High') else: print('Low')?

a)

10

b)

5

c)

High

d)

Low

7.

What are logical operators in Python?

a)

The logical operators in Python are 'and', 'or', and 'not'.

b)

else

c)

if

d)

while

8.

Which logical operator represents 'and' in Python?

a)

not

b)

and

c)

xor

d)

or

9.

What will the following code output if x is 5 and y is 10: if x < 10 and y > 5: print('True')?

a)

False

b)

True and False

c)

None

d)

True

10.

What does the 'or' logical operator do in Python?

a)

The 'or' operator returns True only if both operands are True.

b)

The 'or' operator performs a bitwise operation on integers.

c)

The 'or' operator returns True if at least one operand is True.

d)

The 'or' operator is used to concatenate strings in Python.

11.

How do you combine multiple conditions in an if statement?

a)

Combine conditions using the '+' operator.

b)

Use the 'if' statement without any conditions.

c)

Use 'not' to combine all conditions.

d)

Use logical operators like '&&' for 'and' and '||' for 'or'.

12.

What will the following code output if x is 15: if x < 10 or x > 20: print('Out of range')?

a)

Error: Invalid input

b)

15

c)

No output

d)

Out of range

13.

What is the output of this code: if not (x == 5): print('Not five')?

a)

Five is not the answer

b)

Five

c)

Not five

d)

Not equal to five

14.

How do you write an if statement that checks for equality?

a)

if (a != b) { // code }

b)

if (a == b) { // code }

c)

if a = b { // code }

d)

if (a === b) { // code }

15.

What will the following code output if x is 10: if x == 10: print('Equal') else: print('Not equal')?

a)

Not equal

b)

Equal to 5

c)

Equal

d)

Equal to 10

16.

What will the output be if x = 10 and you have if x > 5: print('Greater')?

a)

Less

b)

Greater

c)

Negative

d)

Equal

17.

Which of the following is the correct syntax for an `if` statement in Python?

a)

`if x > 5 then:`

b)

`if (x > 5):`

c)

`if x > 5:`

d)

`if x > 5;`

18.

What will be the output of the following code? ```python x = 10 if x > 5: print("Greater") else: print("Smaller") ```

a)

Greater

b)

Smaller

c)

Error

d)

Nothing

19.

What will be the output of the following code? ```python x = 3 if x > 5: print("A") elif x == 3: print("B") else: print("C") ```

a)

A

b)

B

c)

C

d)

Error

20.

What will be the output of the following code? ```python x = 7 if x < 5: print("Low") elif x < 10: print("Medium") else: print("High") ```

a)

Low

b)

Medium

c)

High

d)

Error

21.

Which of the following statements is true?

a)

Python is a high level programming language.

b)

Python is an interpreted language.

c)

Python is an object-oriented language.

d)

All of the above.

22.

What is used to define a block of code (body of loop, fuction etc.) in Python?

a)

Curly braces

b)

Parenthesis

c)

Indentation

d)

Quotation

23.

Which of the following is correct?

a)

Variable name can start with an underscore.

b)

Variable name can start with a digit.

c)

Keywords cannot be used as a variable name.

d)

Variable name can have symbols like: @, #, $ etc.

24.

In the following code, n is a/an _______?


n = '5'

a)

integer

b)

string

c)

tuple

d)

operator

25.

What is the output of the following code?


print(1, 2, 3, 4, sep='*')

a)

1 2 3 4

b)

1234

c)

1*2*3*4

d)

24

26.

The statement using and operator results true if _______

a)

both operands are true

b)

both operands are false

c)

either of the operands is true

d)

first operand is true

27.

Which of the following is the correct way to write an if statement that checks if a variable `day` is "Monday" or "Friday"?

a)

if day == "Monday" or "Friday":

b)

if day == "Monday" or day == "Friday":

c)

if day == "Monday" || day == "Friday":

d)

if day == "Monday" and day == "Friday":

28.

Which of the following is a correct if statement to check if a variable `age` is equal to 18?

a)

if age = 18:

b)

if age == 18:

c)

if age === 18:

d)

if age => 18:

29.

What is the correct way to start an if statement in Python?

a)

if x > 5 then:

b)

if x > 5:

c)

if (x > 5)

d)

if x > 5

30.

What is the output?

a)

True

b)

False

c)

condition1

d)

condition2

31.
What is a variable?
a)
A box(memory location) where you store values
b)
a type of graphics
c)
Data type
d)
a type of memory
32.
What symbol is used in python to assign values to a variable?
a)
equals =
b)
plus + 
c)
forward slash /
d)
asterisk *
33.

What will be the output?

name = "Dave"

print (name)

a)

Dave

b)

'Dave'

c)

name

d)

(name)

34.
Which statement correctly assigns the string "Tanner" to the variable name?
a)
name  = print( "Tanner")
b)
input("Tanner")
c)
name = "Tanner"
d)
name = input("Tanner")
35.
In the following code, "city" is an example of a what?
a)
List
b)
Loop
c)
Variable
d)
Array
36.
In python the ' FLOAT data type' can be defined as...?
a)
holds alphanumerical data
b)

holds whole numbers

c)

holds a decimal point in a number

d)
hold either true or false
37.
what is correct code for INTEGER in python?
a)
in
b)
ing
c)
int
38.

What is the output from the following code?

print ("hello world")

a)
Hello World
b)
hello world
c)
print hello world
39.
Which of the following is a float?
a)
new_var = 1234
b)
new_var = True
c)
new_var = 1.234
d)
new_var = "True"
40.
What will the output be from the following code?
print(Hello world!)
a)
Hello world!
b)
SyntaxError
c)
Hello world
d)
print(Hello world!)
41.
What will the output be from the following code?
print(9/3)
a)
3
b)
3.0
c)
9/3
d)
SyntaxError
42.
Which function takes a users input
a)
print()
b)
int()
c)
input()
d)
def
43.
A data type consisting of numbers, letters and symbols.
a)
String
b)
Integer
c)
Float
d)
Boolean
44.
Which is the most appropriate data type for: "13th December"
a)
Float
b)
Boolean
c)
Integer
d)
String
45.

Which of the following is a command to have a message appear on the screen?

a)

print

b)

write

c)

msg

d)

input

46.

Pick the Python command used to make the user type in some data (input the data)

a)

get

b)

input

c)

int

d)

float

47.

Which of the following Python command can be used for WHOLE numbers?

a)

input

b)

int

c)

float

d)

print

48.

Which of the following is a Python command to work with DECIMAL numbers?

a)

input

b)

int

c)

float

d)

print

49.

What would be printed by the command

print('12-5')

a)

12-5

b)

'12-5'

c)

7

d)

'7'

50.

Which of the operators do we use if we want at least one condition to be true?

a)

and

b)

or

c)

else

d)

not

51.

Which of the following is used in Python to give a value to a variable

a)

:=

b)

==

c)

=

52.

What is the result of the following operation?

print(1 + 4*3)

a)

11

b)

12

c)

13

d)

15

53.

What is the value(?) if theoutput is 12

x = 5

y = ?

print (x+y)

a)

"7"

b)

=7

c)

7

d)

/7

54.

What is the correct file extension for Python files?

a)

.py

b)

.pt

c)

.pyt

d)

.pyth

55.

How do you create a variable with the numeric value 5?

a)

Both are correct

b)

x=5

c)

x=int(5)

56.

What will be the output of this code?


x = “2”

y = “4”

z = int(x) + int(y)

print(z)

a)

6

b)

6.0

c)

both are correct

57.

Below is the code for printing student's name. What is the best answer to put in (?)


name = student

print ( ? )

a)

text

b)

print

c)

name

d)

student

58.

What will be the output when a user enters WILLIAM


name = input("Enter your name: ")

print("Hello, " + name)

a)

Hello name

b)

Hello William

c)

Helloname

d)

Hello WILLIAM

59.

Which keyword is used for function?

a)

Define

b)

Def

c)

Fun

d)

Function

60.

Who developed the Python language?

a)

Zim Den

b)

Guido van Rossum

c)

Niene Stom

d)

Wick van Rossum

61.

In which language is Python written?

a)

C

b)

ENGLISH

c)

PHP

d)

All of the above

62.

What will the below code output?

cost = 2.5

quantity = 10

total = cost * quantity

print(total)

(a)  

63.

______________ is used to display output to the user.

a)

#

b)

Float

c)

print ()

d)

varName =

64.

_________________ is a decimal number.

a)

Integer

b)

Variable

c)

String

d)

Float

65.

What will be the output from the following Python code?

print("3+4")

a)

7

b)

3+4

c)

34

d)

SyntaxError

66.

What is the output of the following Python code?

a = 13.5

b = -24.6

a, b = b, a

print(a, b)

a)

13.5 -24.6

b)

-24.6 13.5

c)

-13.5 24.6

d)

13.5 24.6

67.

What is the output of the following Python code?

a = 13.5

b = -24.6

a, b = b, a

print(a, b)

a)

13.5 -24.6

b)

-24.6 13.5

c)

-13.5 24.6

d)

13.5 24.6

68.

What is the output when the following Python code is executed?

a = 3

b = '5'

c = '2'

print(a * b + c)

a)

17

b)

352

c)

5552

d)

Error message

69.

Complete the program to find greater of two numbers ?

a)
b)
c)
d)
70.

What is the data type of ["Sunday","Monday","Tuesday"]

a)

List

b)

String

c)

Numeric

d)

Tuple