Bird
0
0

Given these interfaces and classes:

hard📝 Application Q15 of 15
PHP - Interfaces and Traits
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?
ANotify: Alert Array ( [0] => Start [1] => End )
BArray ( [0] => Start [1] => End ) Notify: Alert
CNotify: Alert Array ( [0] => Alert )
DSyntax error due to missing interface methods
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Understand print_r output

    print_r outputs the logs array with 'Start' and 'End' entries after the echoed notify message.
  3. Final Answer:

    Notify: Alert Array ( [0] => Start [1] => End ) -> Option A
  4. Quick Check:

    Echo notify first, then print logs array [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes