You are on page 1of 2

Q1. Which is the valid conditional statement among the following to check if a is greater than 2?

Ans: To check if a is greater than 2 the if statement is used. Here comparison operator (>) shall be
used to compare the value of variable a with 2. The ":" in the if statement is used to separate the
header of the compound statement from the body. So, the correct way of using the if statement, in
this case, is

if a > 2:

Q2. Which keyword do we use to add alternative conditions for the if statement?

else if

elseif

elif

all of the mentioned

Ans: The syntax for if conditions with elif statement is:

if test expression:

Body of if

elif test expression:

Body of elif

else:

Body of else

Here elif is used to check for a different test expressions as an alternative to write mulitple if
statements (all of the mentioned)

Q3. Identify the correct output of the following code

if 4 + 3 == 9:

print("The addition of 4 and 3 is correct")

else:

print("The addition of 4 and 3 is not correct")

print("Hello")

Ans: The addition of 4 and 3 is 7 and is not equal to 9 hence else statement will be executed. The last
print statement is outside if statement so it will be by default executed.

The addition of 4 and 3 is not correct

Hello

Q4. What will be the output of the following code?

a = b = TRUE

if (a == b):
print("Hello")

else:

print("Well done")

Ans: Here "TRUE" is not defined so can not be compared with a and b hence Python will prompt
NameError: name 'TRUE' is not defined.

You might also like