Hint: Use 'implements' and commas to list interfaces [OK]
Common Mistakes:
Using 'extends' instead of 'implements' for interfaces
Missing commas between interface names
Using 'uses' keyword incorrectly
3. What will be the output of the following PHP code?
interface A { public function foo(); } interface B { public function bar(); } class Test implements A, B { public function foo() { return 'foo'; } public function bar() { return 'bar'; } } $obj = new Test(); echo $obj->foo() . ' ' . $obj->bar();
medium
A. foo bar
B. foobar
C. Syntax error
D. Runtime error
Solution
Step 1: Check interface methods implemented
The class Test implements both interfaces A and B and defines both methods foo() and bar() correctly.
Step 2: Understand output of method calls
Calling foo() returns 'foo' and bar() returns 'bar'. Echo concatenates them with a space.
Final Answer:
foo bar -> Option A
Quick Check:
Implemented methods return strings combined with space [OK]
Hint: Check all interface methods are implemented to avoid errors [OK]
Common Mistakes:
Forgetting to implement all interface methods
Missing space in echo output
Expecting concatenation without space
4. Identify the error in this PHP code:
interface X { public function methodX(); } interface Y { public function methodY(); } class Demo implements X, Y { public function methodX() { return 'X'; } }
medium
A. Interface Y cannot be implemented with interface X.
B. Class Demo should extend interfaces, not implement.
C. Class Demo must implement methodY() from interface Y.
D. No error, code is correct.
Solution
Step 1: Check implemented interfaces and methods
Class Demo implements interfaces X and Y, so it must define all methods from both.
Step 2: Identify missing method
methodY() from interface Y is not implemented, causing an error.
Final Answer:
Class Demo must implement methodY() from interface Y. -> Option C
Quick Check:
All interface methods must be implemented [OK]
Hint: Implement all methods from every interface you list [OK]
Common Mistakes:
Implementing only some interface methods
Confusing extends with implements
Assuming interfaces can be partially implemented
5. Given these interfaces and classes:
interface Logger { public function log(string $msg); } interface Notifier { public function notify(string $msg); } class System implements Logger, Notifier { private array $logs = []; public function log(string $msg) { $this->logs[] = $msg; } public function notify(string $msg) { echo "Notify: $msg\n"; } public function getLogs(): array { return $this->logs; } } $sys = new System(); $sys->log('Start'); $sys->notify('Alert'); $sys->log('End'); print_r($sys->getLogs());
What is the output of this code?
hard
A. Notify: Alert
Array
(
[0] => Start
[1] => End
)
B. Array
(
[0] => Start
[1] => End
)
Notify: Alert
C. Notify: Alert
Array
(
[0] => Alert
)
D. Syntax error due to missing interface methods
Solution
Step 1: Trace method calls
First, log('Start') adds 'Start' to logs array. Then notify('Alert') echoes 'Notify: Alert'. Then log('End') adds 'End' to logs.
Step 2: Understand print_r output
print_r outputs the logs array with 'Start' and 'End' entries after the echoed notify message.
Final Answer:
Notify: Alert
Array
(
[0] => Start
[1] => End
) -> Option A
Quick Check:
Echo notify first, then print logs array [OK]
Hint: Echo outputs appear before print_r array display [OK]
Common Mistakes:
Assuming print_r output appears before echo
Confusing array contents logged
Expecting syntax errors without missing methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.