Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of implemented interface method
What is the output of this PHP code when run?
PHP
<?php interface Talker { public function sayHello(); } class Person implements Talker { public function sayHello() { echo "Hello from Person!"; } } $p = new Person(); $p->sayHello(); ?>
Attempts:
2 left
💡 Hint
Check how the interface method is implemented in the class.
✗ Incorrect
The class Person implements the interface Talker and defines the sayHello() method which prints 'Hello from Person!'. So the output is exactly that string.
❓ Predict Output
intermediate2:00remaining
Output when interface method is missing
What error or output will this PHP code produce?
PHP
<?php interface Flyer { public function fly(); } class Bird implements Flyer { // Missing fly() method } $b = new Bird(); $b->fly(); ?>
Attempts:
2 left
💡 Hint
Check if the class implements all interface methods.
✗ Incorrect
Since Bird does not implement the required fly() method from Flyer interface, PHP throws a fatal error saying Bird must implement the remaining methods or be declared abstract.
🔧 Debug
advanced2:00remaining
Identify the error in interface implementation
This PHP code tries to implement an interface but causes an error. What is the cause?
PHP
<?php interface Runner { public function run(int $speed); } class Athlete implements Runner { public function run($speed) { echo "Running at speed $speed"; } } $a = new Athlete(); $a->run(10); ?>
Attempts:
2 left
💡 Hint
Check if parameter types must match exactly in interface and implementation.
✗ Incorrect
In PHP, when implementing an interface, the method signature must be compatible, including parameter type hints. Athlete::run($speed) omits the 'int' type required by Runner::run(int $speed), causing a fatal error: 'Declaration of Athlete::run($speed) must be compatible with Runner::run(int $speed)'.
📝 Syntax
advanced2:00remaining
Which code causes a syntax error?
Which of these PHP code snippets will cause a syntax error?
Attempts:
2 left
💡 Hint
Look carefully at the semicolons inside the method.
✗ Incorrect
Option D is missing a semicolon after echo 'Hi', causing a syntax error. The others have correct syntax.
🚀 Application
expert2:00remaining
Number of methods in implemented interface
Given this PHP code, how many methods must class C implement to satisfy all interfaces?
PHP
<?php interface X { public function a(); } interface Y { public function b(); public function c(); } interface Z extends X, Y { public function d(); } class C implements Z { public function a() {} public function b() {} public function c() {} public function d() {} } ?>
Attempts:
2 left
💡 Hint
Remember that interface Z extends X and Y, so it includes all their methods.
✗ Incorrect
Interface Z extends X and Y, so it requires methods a(), b(), c(), and d(). Class C must implement all four methods.