OOP Reviewer PDF
Document Details
Tags
Summary
This is a review quiz for Object-Oriented Programming (OOP). It includes identification and multiple choice questions, covering topics such as classes, objects, constructors, destructors, inheritance, polymorphism, and access modifiers.
Full Transcript
REVIEW QUIZ (OOP) PART 1. IDENTIFICATION 1. A blueprint or template used to create objects in OOP. - CLASS 2. A function used to initialize the state of an object when it is created. - CONSTRUCTOR 3. The term used for variables defined insid...
REVIEW QUIZ (OOP) PART 1. IDENTIFICATION 1. A blueprint or template used to create objects in OOP. - CLASS 2. A function used to initialize the state of an object when it is created. - CONSTRUCTOR 3. The term used for variables defined inside a class but outside any methods. - ATTRIBUTES 4. The process by which one class inherits attributes and methods from another class. - INHERITANCE 5. A function that modifies an object’s private attribute. - SETTER 6. A function that retrieves the value of an object’s private attribute. - GETTER 7. The OOP concept where an object or class can have different forms (e.g., method overriding). - POLYMORPHISM 8. The special function is called automatically when an object is destroyed. - DESTRUCTOR 9. A mechanism that controls the visibility of class members to the outside world. - ACCESS MODIFIER 10. A class member that can be accessed from outside the class but cannot be modified. - READ-ONLY 11. The process of providing a specific implementation for a method in a subclass that already exists in the parent class. - METHOD OVERRIDING 12. The keyword used in many languages to indicate that one class derives from another. - EXTENDS/INHERITS 13. The feature of OOP that promotes code reuse and the reduction of redundancy. - INHERITANCE 14. The term for the actual instance of a class that is created in memory. - OBJECT 15. The keyword used to call a method from the parent class in the child class. - SUPER/BASE PART 2. MULTIPLE CHOICE 1.What does OOP stand for? - A. Object-Oriented Programming 2.Which of the following best describes a class in OOP? - B. A blueprint for creating objects 3. In OOP, what is an object? - C. An instance of a class 4. Which of the following correctly defines encapsulation? - B. The hiding of internal object details from the outside 5. What is the purpose of a constructor in a class? - B. To initialize object attributes 6. Which of these access modifiers restricts access to class members only within the class itself? - B. private 7. What is a destructor used for in OOP? - B. To clean up memory or resources when an object is destroyed 8. Which of the following is NOT a feature of OOP? - D. Compilation 9. In a class, which of the following methods allows external access to a private attribute? - C. Getter 10. Which term refers to the ability of different objects to respond to the same method in different ways? - B. Polymorphism 11. A software company is designing a program for an online shopping platform. Each product has attributes such as name, price, and stock availability. Which of the following should be implemented to ensure that the price of a product is always greater than zero when setting its value? - B. Use a private attribute for price with a setter method to validate the value 12. In a video game development project, you have a Player class and an Enemy class. Both classes need to have a move() method, but the behavior should be different for each class. What OOP concept should be applied? - A. Method overriding 13. Consider a car dealership management system. There are Sedan and SUV classes that inherit common functionality from a parent Car class. If the Car class has a method displayDetails(), but you want each type of car to show its specific details, what should you do? - B. Override the displayDetails() method in each subclass 14. A bank application includes an Account class that has an attribute for the account balance. If you want to prevent direct modification of the balance but allow controlled access, what should you implement? - B. Provide a private attribute with public getter and setter methods 15. In a hospital management system, there is a Doctor class and a Patient class. Both classes share some common properties, such as name and age, but their behavior differs. What should be implemented to handle this? - B. Inheritance from a common Person class 16. What is the correct syntax for creating an instance of a class Person in many OOP languages? - B. new Person(); 17. Which of the following allows subclasses to modify the behavior of inherited methods? - C. Method overriding 18. What is the purpose of using the protected access modifier in a class? - C. To allow access to derived classes and the class itself 19. In OOP, what does method overriding allow? - B. Defining multiple methods with the same name but different behaviors in different classes 20. Which OOP principle enables you to define a single interface, but allows multiple implementations? - A. Polymorphism PART 3. TRUE OR FALSE 1. A class is an instance of an object. - FALSE 2. A constructor is automatically called when an object is created. - TRUE 3. Inheritance allows one class to take on attributes and methods from another class. - TRUE 4. An object can access private attributes directly without using methods. - FALSE 5. Method overriding is used to change the behavior of a method in a subclass. - TRUE 6. A destructor is called manually to destroy an object. - FALSE 7. Encapsulation allows internal details of a class to be hidden from the outside. - TRUE 8. Public members of a class are accessible from outside the class. - TRUE 9. Method overloading refers to multiple methods having the same name but different parameter lists. - TRUE 10. A private method can be accessed from outside the class if a public method calls it. - FALSE 11. In OOP, an object is the blueprint for creating classes. - FALSE 12. Access modifiers define the scope and visibility of class members. - TRUE 13. The constructor and destructor can have the same name in some programming languages. - FALSE 14. The protected keyword ensures that class members are only accessible within the same class. - FALSE 15. Polymorphism allows objects of different types to be treated as instances of the same class. - TRUE INTRODUCTION TO OOP OOP (OBJECT- ORIENTED PROGRAMMING) Procedural programming is about writing procedures or functions that perform operations on the data Object-oriented programming is about creating objects that contain both data and functions. Core concepts of OOP - Class - Object - Member Variables - Member Function - Inheritance - Parent class - Child class - Polymorphism - Overloading - Data abstraction - Encapsulation - constructor - Destructor Advantages of OOP over Procedural - Faster and easier to execute - Provides clear structure for the programs - Helps to keep DRY “Don’t Repeat Yourself”, makes code easier to maintain, modify and debug - Makes it possible to create full reusable applications with less code and shorter development time - Two main aspects of OOP Class - blueprint for creating objects. It defines a set of attributes and methods that the objects created from the class will have Object - instance of a class. It represents a specific realization of the class with actual values for the attributes defined by the class. - An individual object created from the class. It has specific values for the properties defined by the class. - It usually created outside of the class. Attributes - declared as variables within the class definition using keywords that match their visibility. Methods - a procedure associated with a class. A method defines the behavior of the objects that are created from the class. Another way to say this, is that, a method is an action that an object is able to perform. Define a Class A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces Define a Object Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values. Objects of a class is created using the new keyword. Objects of a class have: – Same operations, behaving the same way – Same attributes representing the same features, but values of those attributes can vary from object to object. $this keyword - refers to the current object, and is only available inside methods. _____________________________________________________________________________________________ GETTER AND SETTER Getter method - a special function that allows you to access the value of a private or protected property of a class from outside the class Setter method - a special function used to set or modify the value of a private or protected property within a class. Getters and Setters: What Are They? Getters and setters are methods used to define or retrieve the values of variables, normally private ones. Getter and setter strategies are utilized when we need to restrict the direct access to the variables by end-users. A getter method is a technique that gets or recovers the value of an object. A setter method is a technique that sets the value of an object. Why Use Getters and Setters? 1. Encapsulation - keeping the internal details of an object hidden from the outside world. 2. Validation - allow us to add checks or rules before changing the value of a variable. 3. Read-Only or Write-Only Access - sometimes, we want to allow reading a variable but not changing it, or vice versa. Getters and setters let us control this access. 4. Lazy Initialization - getters can be used to initialize a variable only when it's needed, which can help with performance. SUMMARY Getters and Setters are special methods that provide a controlled way to access and modify private variables in a class. Getter Methods are used to retrieve the value of a private variable. Setter Methods are used to update or set the value of a private variable. These methods help maintain the integrity of the data, enforce rules or validation, and keep the internal workings of an object hidden from the outside. _____________________________________________________________________________________________ INHERITANCE Inheritance - when a class derives from other class - child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. - An inherited class is defined by using the extends keyword. ____________________________________________________________________________ CONSTRUCTOR, DESTRUCTOR, AND ACCESS MODIFIERS Constructor - allows you to initialize an object’s properties upon creation of the object - If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Destructor - called when the object is destructed or the script is stopped or exited - If you create a __destruct() function, PHP will automatically call this function at the end of the script. Access Modifiers Properties and methods can have access modifiers which control where they can be accessed. There are three access modifiers: – public - the property or method can be accessed from everywhere. This is default – protected - the property or method can be accessed within the class and by classes derived from that class – private - the property or method can ONLY be accessed within the class ____________________________________________________________________________ METHOD OVERRIDING Function Overriding - both parent and child classes should have the same function name with and number of arguments. - is used to replace the parent method in child class. - purpose of overriding is to change the behavior of parent class method. - two methods with the same name and same parameter is called overriding. - purpose of overriding a function in object-oriented programming (OOP) is to provide a new implementation for a method that is already defined in a parent class. This allows a subclass to customize or extend the behavior of inherited methods. - Inherited methods can be overridden by redefining the methods (use the same name) in the child class Final Keyword - can be used to prevent class inheritance or to prevent method overriding. - ensures that a method is locked down and cannot be changed by child classes. - If a method is marked final in the parent class, it cannot be overridden by the child class, so you cannot have the same final method in both the parent and child classes.