Bird
0
0

You want to write a method that returns the maximum of two numbers a and b. Which code correctly does this?

hard📝 Application Q8 of 15
Python - Methods and Behavior Definition
You want to write a method that returns the maximum of two numbers a and b. Which code correctly does this?
Adef max_num(a, b): if a > b: return a else: return b
Bdef max_num(a, b): print(a if a > b else b)
Cdef max_num(a, b): return a + b
Ddef max_num(a, b): if a < b: return a else: return b
Step-by-Step Solution
Solution:
  1. Step 1: Understand logic to find maximum

    Return a if it is greater than b, else return b.
  2. Step 2: Check each option's correctness

    def max_num(a, b): if a > b: return a else: return b correctly returns max; B prints instead of returning; C returns sum; A returns minimum.
  3. Final Answer:

    def max_num(a, b): if a > b: return a else: return b -> Option A
  4. Quick Check:

    Return max value using if-else [OK]
Quick Trick: Use if-else with return to pick max [OK]
Common Mistakes:
  • Using print instead of return
  • Returning sum instead of max
  • Reversing comparison logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes