Bird
0
0

How would you declare a class Calculator with a static method add that returns the sum of two numbers a and b?

hard📝 Application Q9 of 15
PHP - Classes and Objects
How would you declare a class Calculator with a static method add that returns the sum of two numbers a and b?
Aclass Calculator { static add($a, $b) { return $a + $b; } }
Bclass Calculator { public static function add($a, $b) { return $a + $b; } }
Cclass Calculator { public function static add($a, $b) { return $a + $b; } }
Dclass Calculator { function add($a, $b) static { return $a + $b; } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand static method syntax

    Static methods are declared with public static function followed by the method name.
  2. Step 2: Verify correct syntax

    class Calculator { public static function add($a, $b) { return $a + $b; } } correctly declares the static method add with parameters and returns their sum.
  3. Final Answer:

    class Calculator { public static function add($a, $b) { return $a + $b; } } -> Option B
  4. Quick Check:

    Static method syntax = public static function name() [OK]
Quick Trick: Use 'public static function' to declare static methods [OK]
Common Mistakes:
  • Placing 'static' after function name
  • Omitting 'function' keyword
  • Incorrect order of keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes