0
0
PHPprogramming~20 mins

Interface declaration and implementation in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
?>
AHello from Person!
BFatal error: Class Person must implement method sayHello()
CHello from Talker!
DNo output
Attempts:
2 left
💡 Hint
Check how the interface method is implemented in the class.
Predict Output
intermediate
2: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();
?>
ANo output
BCall to undefined method Bird::fly()
CFatal error: Class Bird contains 1 abstract method and must therefore be declared abstract or implement the remaining methods
DFly method called
Attempts:
2 left
💡 Hint
Check if the class implements all interface methods.
🔧 Debug
advanced
2: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);
?>
AWarning: Argument 1 passed to run() must be of the type int
BFatal error due to missing type hint in method parameter
CNo error, code runs and outputs 'Running at speed 10'
DParse error: syntax error in interface declaration
Attempts:
2 left
💡 Hint
Check if parameter types must match exactly in interface and implementation.
📝 Syntax
advanced
2:00remaining
Which code causes a syntax error?
Which of these PHP code snippets will cause a syntax error?
A?php interface A { public function foo(); } class B implements A { public function foo() { echo 'Hi'; } } ?>
B<?php interface A { public function foo(); } class B implements A { public function foo() { echo 'Hi'; } } ?>
C>? } } ;'iH' ohce { )(oof noitcnuf cilbup { A stnemelpmi B ssalc } ;)(oof noitcnuf cilbup { A ecafretni php?<
D<?php interface A { public function foo(); } class B implements A { public function foo() { echo 'Hi' } } ?>
Attempts:
2 left
💡 Hint
Look carefully at the semicolons inside the method.
🚀 Application
expert
2: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() {}
}
?>
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Remember that interface Z extends X and Y, so it includes all their methods.