Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Interfaces and Traits
What will be the output of this PHP code?
interface Animal {
  public function sound();
}

class Dog implements Animal {
  public function sound() {
    return "Bark";
  }
}

$dog = new Dog();
echo $dog->sound();
ABark
Bsound
CAnimal
DFatal error
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface and class implementation

    The interface Animal declares method sound(). Dog class implements Animal and defines sound() returning "Bark".
  2. Step 2: Check the output of calling $dog->sound()

    Calling $dog->sound() returns "Bark" which is then echoed.
  3. Final Answer:

    Bark -> Option A
  4. Quick Check:

    Implemented method returns "Bark" [OK]
Quick Trick: Implemented methods run normally, output their return [OK]
Common Mistakes:
  • Expecting interface name as output
  • Confusing method name with output
  • Thinking code causes error without implementation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes