Symfony 7 – PHP – Abstract Classes

20 multiple-choice questions about abstract classes in PHP (up to 8.2): purpose, syntax, method rules, inheritance, and small code examples.


Question 1: What is an abstract class used for in PHP?

Question 2: Can you instantiate an abstract class directly?

Question 3: Which statement about abstract methods is correct?

Question 4: When would you prefer an abstract class over an interface?

Question 5: What is wrong?\nabstract class A { abstract public function foo(): int; }\nclass B extends A {}

Question 6: Can a class be both abstract and final in PHP?

Question 7: Can a method be both abstract and final?

Question 8: Which statement about constructors in abstract classes is TRUE?

Question 9: Are abstract static methods allowed in PHP?

Question 10: Which override is valid?\nabstract class A { protected abstract function run(): void; }\nclass B extends A { /* ? */ }

Question 11: Which is TRUE about properties and constants in abstract classes?

Question 12: Is this valid?\nabstract class Shape {\n abstract public function area(): float;\n public function unit(): string { return "cm2"; }\n}

Question 13: If a subclass does not implement all abstract methods, what must it be?

Question 14: Can an abstract class implement an interface?

Question 15: Which override is valid per covariance?\nabstract class Repo { abstract public function all(): iterable; }\nclass UserRepo extends Repo { /* ? */ }

Question 16: Which parameter override is compatible?\nabstract class F { abstract public function setVal(mixed $v): void; }

Question 17: Can an abstract class use traits?

Question 18: Is this valid? abstract protected function run() { return 1; }

Question 19: Are typed properties allowed in abstract classes?

Question 20: Consider:\nabstract class Animal { abstract public function sound(): string; }\nabstract class DogBase extends Animal {}\nclass Beagle extends DogBase { public function sound(): string { return "woof"; } }\necho (new Beagle())->sound();