Bird
0
0

You want to create a PHP class 'Car' that can store the car's color and speed, and has a method to accelerate by increasing speed. Which approach best uses OOP principles?

hard📝 Application Q8 of 15
PHP - Classes and Objects
You want to create a PHP class 'Car' that can store the car's color and speed, and has a method to accelerate by increasing speed. Which approach best uses OOP principles?
ACreate separate functions for color and speed without a class
BUse global variables for color and speed, and a function accelerate() outside the class
CDefine properties for color and speed, and a method accelerate() that increases speed
DStore color and speed in arrays and update speed by array index
Step-by-Step Solution
Solution:
  1. Step 1: Identify OOP principles

    OOP uses classes with properties and methods to represent real-world objects and their behaviors.
  2. Step 2: Match options to OOP style

    Define properties for color and speed, and a method accelerate() that increases speed uses a class with properties and a method, fitting OOP. Others use procedural or non-OOP approaches.
  3. Final Answer:

    Define properties for color and speed, and a method accelerate() that increases speed -> Option C
  4. Quick Check:

    OOP = class with properties + methods [OK]
Quick Trick: Use classes to bundle data and behavior together [OK]
Common Mistakes:
  • Using global variables instead of class properties
  • Writing functions outside classes for object behavior
  • Not grouping related data and functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes