Bird
Raised Fist0

Which of the following is the correct syntax for a method that returns the sum of two numbers a and b?

easy📝 Syntax Q12 of Q15
Python - Methods and Behavior Definition
Which of the following is the correct syntax for a method that returns the sum of two numbers a and b?
Adef add(a, b): return a - b
Bdef add(a, b): print(a + b)
Cdef add(a, b): return a + b
Ddef add(a, b): a + b
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct return usage

    The method must use return to send back the sum a + b.
  2. Step 2: Check each option

    The version with return a - b returns the difference. The version with print(a + b) prints but returns None. The version with just a + b lacks return. Only def add(a, b): return a + b correctly returns the sum.
  3. Final Answer:

    def add(a, b): return a + b -> Option C
  4. Quick Check:

    Return sum = def add(a, b): return a + b [OK]
Quick Trick: Return must be followed by value to send back [OK]
Common Mistakes:
MISTAKES
  • Using print instead of return
  • Omitting return keyword
  • Returning wrong expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes