Exercise: Python

Questions for: Conditional Statements

What will be the output of the following code?
x = 10
y = 5
if x > y:
    print("x is greater")
else:
    print("y is greater")
A:
x is greater
B:
y is greater
C:
No output
D:
Syntax Error
Answer: A
The code compares the values of x and y using the > (greater than) operator. Since x is greater than y, the code inside the if block will be executed, printing "x is greater."
Which keyword is used to introduce the else block in an if-else statement?
A:
elif
B:
else
C:
then
D:
or
Answer: B
In an if-else statement, the else keyword is used to define the block of code that should be executed if the condition in the if statement is false.
What is the purpose of the if statement?
A:
To declare a variable
B:
To define a function
C:
To control the flow of a program based on a condition
D:
To create a loop
Answer: C
The if statement is used to make decisions in Python. It allows you to execute a block of code if a certain condition is true.
Ad Slot (Above Pagination)
Quiz