If Flashcards

(26 cards)

1
Q

What keyword is used to start an if statement in Python?

A

if

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: The condition in an if statement must always evaluate to a boolean value.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fill in the blank: An if statement can be followed by an ______ statement to execute code if the condition is false.

A

else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of an if statement?

A

To execute a block of code conditionally based on whether a specified condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What will be the output of the following code: if 5 > 3: print(‘Hello’)?

A

‘Hello’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What keyword is used to introduce an alternative block in an if statement?

A

else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False: You can have multiple elif statements in a single if-else structure.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the syntax for an if statement with an elif clause?

A

if condition1: # code block 1 elif condition2: # code block 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will happen if none of the conditions in an if-elif-else structure are true?

A

The code in the else block will be executed if it is present.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In the following code, what will be printed? if x = 10: print(‘Ten’)

A

Syntax error (should use ‘==’ for comparison)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the output of the following code? x = 10; if x < 5: print(‘Low’); else: print(‘High’)

A

‘High’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or False: Indentation is not important in Python when writing if statements.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the correct way to write a nested if statement?

A

if condition1: if condition2: # code block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which of the following is a valid if statement? A) if (x > 10) B) if x > 10: C) if x > 10 {}

A

B) if x > 10:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will this code output? if True: print(‘Always runs’)

A

‘Always runs’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you check if a variable ‘a’ is equal to 5 in an if statement?

17
Q

Fill in the blank: The ______ statement is used to check multiple expressions for truthiness in a compact form.

18
Q

What is the correct format for an if statement that checks if a number is positive?

A

if number > 0:

19
Q

True or False: An if statement can directly check for multiple conditions using logical operators.

20
Q

What does the ‘and’ operator do in an if statement?

A

It evaluates to True if both conditions are True.

21
Q

What does the ‘or’ operator do in an if statement?

A

It evaluates to True if at least one of the conditions is True.

22
Q

What will be the output of the following code? if False and True: print(‘This will not run’)

23
Q

What is the output of this code? if 2 + 2 == 4: print(‘Math works’)

A

‘Math works’

24
Q

What is the difference between ‘==’ and ‘=’ in Python?

A

’==’ is used for comparison, while ‘=’ is used for assignment.

25
What is the output of the following code? x = 3; if x > 5: print('Greater'); else: print('Lesser')
'Lesser'
26
True or False: A single if statement can be used without an else or elif clause.
True