Programming MCQs Feed

📊 PHP
Q. Which PHPUnit annotation runs a method before each test?
  • (A) @before
  • (B) @setup
  • (C) @init
  • (D) @start
💬 Discuss
✅ Correct Answer: (A) @before

Explanation: @before runs before each test.

📊 PHP
Q. Which OOP principle restricts direct access to class data?
  • (A) Inheritance
  • (B) Encapsulation
  • (C) Polymorphism
  • (D) Abstraction
💬 Discuss
✅ Correct Answer: (B) Encapsulation

Explanation: Encapsulation hides internal data using access modifiers.

📊 PHP
Q. Which access modifier allows access only within the same class?
  • (A) public
  • (B) protected
  • (C) private
  • (D) static
💬 Discuss
✅ Correct Answer: (C) private

Explanation: private members are accessible only inside the class.

📊 PHP
Q. Which keyword is used to implement an interface?
  • (A) extends
  • (B) implements
  • (C) interface
  • (D) uses
💬 Discuss
✅ Correct Answer: (B) implements

Explanation: implements keyword is used with interfaces.

📊 PHP
Q. Which keyword allows accessing parent class methods?
  • (A) this
  • (B) parent
  • (C) self
  • (D) static
💬 Discuss
✅ Correct Answer: (B) parent

Explanation: parent:: is used to access parent class members.

📊 PHP
Q. Which magic method is called when an object is converted to string?
  • (A) __string()
  • (B) __toString()
  • (C) __convert()
  • (D) __print()
💬 Discuss
✅ Correct Answer: (B) __toString()

Explanation: __toString() defines string representation.

📊 PHP
Q. Which magic method is called when invoking inaccessible methods?
  • (A) __get()
  • (B) __call()
  • (C) __invoke()
  • (D) __set()
💬 Discuss
✅ Correct Answer: (B) __call()

Explanation: __call() handles calls to inaccessible methods.

📊 PHP
Q. Which design pattern ensures a class has only one instance?
  • (A) Factory
  • (B) Observer
  • (C) Singleton
  • (D) Strategy
💬 Discuss
✅ Correct Answer: (C) Singleton

Explanation: Singleton restricts class instantiation.

📊 PHP
Q. Which design pattern provides an interface for creating objects?
  • (A) Singleton
  • (B) Factory
  • (C) Decorator
  • (D) Adapter
💬 Discuss
✅ Correct Answer: (B) Factory

Explanation: Factory abstracts object creation.

📊 PHP
Q. Which design pattern allows behavior to be added dynamically?
  • (A) Adapter
  • (B) Decorator
  • (C) Facade
  • (D) Observer
💬 Discuss
✅ Correct Answer: (B) Decorator

Explanation: Decorator adds responsibilities dynamically.