Font size
WorksheetsUnderstanding If Statements in Python
Total questions: 70
Worksheet time: 39mins
What is the purpose of an if statement in Python?
To execute code conditionally based on a boolean expression.
To define functions in Python.
To create loops in the code.
To handle exceptions and errors.
How do you write a basic if statement in Python?
if condition: do_something
if condition do_something
if condition: do_something()
if (condition) { do_something }
What will the following code output: if x > 10: print('High')?
Depends on the value of x; prints 'High' if x > 10, otherwise no output.
Prints 'High' only if x == 10
Always prints 'High' regardless of x
Prints 'Low' if x < 10
What is the function of the else statement in Python?
The else statement is used to define a function in Python.
The else statement is used to handle exceptions in Python.
The else statement executes code when the if condition is false.
The else statement always executes regardless of the if condition.
How do you write an if-else statement in Python?
condition ? do_something : do_something_else;
if (condition) { do_something } else { do_something_else }
if condition then do_something;
if condition: do_something else: do_something_else
What will the following code output if x is 5: if x > 10: print('High') else: print('Low')?
10
5
High
Low
What are logical operators in Python?
The logical operators in Python are 'and', 'or', and 'not'.
else
if
while
Which logical operator represents 'and' in Python?
not
and
xor
or
What will the following code output if x is 5 and y is 10: if x < 10 and y > 5: print('True')?
False
True and False
None
True
What does the 'or' logical operator do in Python?
The 'or' operator returns True only if both operands are True.
The 'or' operator performs a bitwise operation on integers.
The 'or' operator returns True if at least one operand is True.
The 'or' operator is used to concatenate strings in Python.
How do you combine multiple conditions in an if statement?
Combine conditions using the '+' operator.
Use the 'if' statement without any conditions.
Use 'not' to combine all conditions.
Use logical operators like '&&' for 'and' and '||' for 'or'.
What will the following code output if x is 15: if x < 10 or x > 20: print('Out of range')?
Error: Invalid input
15
No output
Out of range
What is the output of this code: if not (x == 5): print('Not five')?
Five is not the answer
Five
Not five
Not equal to five
How do you write an if statement that checks for equality?
if (a != b) { // code }
if (a == b) { // code }
if a = b { // code }
if (a === b) { // code }
What will the following code output if x is 10: if x == 10: print('Equal') else: print('Not equal')?
Not equal
Equal to 5
Equal
Equal to 10
What will the output be if x = 10 and you have if x > 5: print('Greater')?
Less
Greater
Negative
Equal
Which of the following is the correct syntax for an `if` statement in Python?
`if x > 5 then:`
`if (x > 5):`
`if x > 5:`
`if x > 5;`
What will be the output of the following code? ```python x = 10 if x > 5: print("Greater") else: print("Smaller") ```
Greater
Smaller
Error
Nothing
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
B
C
Error
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") ```
Low
Medium
High
Error
Which of the following statements is true?
Python is a high level programming language.
Python is an interpreted language.
Python is an object-oriented language.
All of the above.
What is used to define a block of code (body of loop, fuction etc.) in Python?
Curly braces
Parenthesis
Indentation
Quotation
Which of the following is correct?
Variable name can start with an underscore.
Variable name can start with a digit.
Keywords cannot be used as a variable name.
Variable name can have symbols like: @, #, $ etc.
In the following code, n is a/an _______?
n = '5'
integer
string
tuple
operator
What is the output of the following code?
print(1, 2, 3, 4, sep='*')
1 2 3 4
1234
1*2*3*4
24
The statement using and operator results true if _______
both operands are true
both operands are false
either of the operands is true
first operand is true
Which of the following is the correct way to write an if statement that checks if a variable `day` is "Monday" or "Friday"?
if day == "Monday" or "Friday":
if day == "Monday" or day == "Friday":
if day == "Monday" || day == "Friday":
if day == "Monday" and day == "Friday":
Which of the following is a correct if statement to check if a variable `age` is equal to 18?
if age = 18:
if age == 18:
if age === 18:
if age => 18:
What is the correct way to start an if statement in Python?
if x > 5 then:
if x > 5:
if (x > 5)
if x > 5
What is the output?
True
False
condition1
condition2
What will be the output?
name = "Dave"
print (name)
Dave
'Dave'
name
(name)
holds whole numbers
holds a decimal point in a number
What is the output from the following code?
print ("hello world")
print(Hello world!)
print(9/3)
Which of the following is a command to have a message appear on the screen?
write
msg
input
Pick the Python command used to make the user type in some data (input the data)
get
input
int
float
Which of the following Python command can be used for WHOLE numbers?
input
int
float
Which of the following is a Python command to work with DECIMAL numbers?
input
int
float
What would be printed by the command
print('12-5')
12-5
'12-5'
7
'7'
Which of the operators do we use if we want at least one condition to be true?
and
or
else
not
Which of the following is used in Python to give a value to a variable
:=
==
=
What is the result of the following operation?
print(1 + 4*3)
11
12
13
15
What is the value(?) if theoutput is 12
x = 5
y = ?
print (x+y)
"7"
=7
7
/7
What is the correct file extension for Python files?
.py
.pt
.pyt
.pyth
How do you create a variable with the numeric value 5?
Both are correct
x=5
x=int(5)
What will be the output of this code?
x = “2”
y = “4”
z = int(x) + int(y)
print(z)
6
6.0
both are correct
Below is the code for printing student's name. What is the best answer to put in (?)
name = student
print ( ? )
text
name
student
What will be the output when a user enters WILLIAM
name = input("Enter your name: ")
print("Hello, " + name)
Hello name
Hello William
Helloname
Hello WILLIAM
Which keyword is used for function?
Define
Def
Fun
Function
Who developed the Python language?
Zim Den
Guido van Rossum
Niene Stom
Wick van Rossum
In which language is Python written?
C
ENGLISH
PHP
All of the above
What will the below code output?
cost = 2.5
quantity = 10
total = cost * quantity
print(total)
(a)
______________ is used to display output to the user.
#
Float
print ()
varName =
_________________ is a decimal number.
Integer
Variable
String
Float
What will be the output from the following Python code?
print("3+4")
7
3+4
34
SyntaxError
What is the output of the following Python code?
a = 13.5
b = -24.6
a, b = b, a
print(a, b)
13.5 -24.6
-24.6 13.5
-13.5 24.6
13.5 24.6
What is the output of the following Python code?
a = 13.5
b = -24.6
a, b = b, a
print(a, b)
13.5 -24.6
-24.6 13.5
-13.5 24.6
13.5 24.6
What is the output when the following Python code is executed?
a = 3
b = '5'
c = '2'
print(a * b + c)
17
352
5552
Error message
Complete the program to find greater of two numbers ?
What is the data type of ["Sunday","Monday","Tuesday"]
List
String
Numeric
Tuple
