Bird
0
0

You want to write a program that prints 'Even' if a number is even, and 'Odd' if it is odd. Which code correctly uses if-else to do this?

hard📝 Application Q15 of 15
Python - Conditional Statements
You want to write a program that prints 'Even' if a number is even, and 'Odd' if it is odd. Which code correctly uses if-else to do this?
Anum = 4 if num % 2: print('Even') else: print('Odd')
Bnum = 4 if num / 2 == 0: print('Even') else: print('Odd')
Cnum = 4 if num % 2 != 0: print('Even') else: print('Odd')
Dnum = 4 if num % 2 == 0: print('Even') else: print('Odd')
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to check even numbers

    A number is even if dividing by 2 leaves no remainder, so num % 2 == 0 is true for even numbers.
  2. Step 2: Check each option's condition

    num = 4 if num % 2 == 0: print('Even') else: print('Odd') correctly uses num % 2 == 0. num = 4 if num / 2 == 0: print('Even') else: print('Odd') uses division instead of modulo, which is wrong. num = 4 if num % 2: print('Even') else: print('Odd') treats nonzero remainder as even, which is incorrect. num = 4 if num % 2 != 0: print('Even') else: print('Odd') reverses the logic.
  3. Final Answer:

    num = 4 if num % 2 == 0: print('Even') else: print('Odd') -> Option D
  4. Quick Check:

    Modulo equals zero means even [OK]
Quick Trick: Use 'num % 2 == 0' to check even numbers [OK]
Common Mistakes:
MISTAKES
  • Using division instead of modulo
  • Reversing even and odd logic
  • Checking truthiness of modulo without comparison

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes