Option D: OOP Flashcards
What is a class in OOP, and how is it different from an object?
A class is a blueprint that defines fields and methods for a group of similar objects. An object is an instance of a class, created in memory with its own set of data and accessible methods
Explain encapsulation with an example.
Encapsulation hides data within a class and only exposes it through methods. For example, a BankAccount class might have a private balance field and a public deposit() method, which controls how the balance is updated.
How does inheritance support code reuse?
Inheritance allows a subclass to inherit methods and fields from a superclass, reducing duplicate code. For instance, a Car class might extend a Vehicle superclass, inheriting general attributes like speed and fuel, and adding specific attributes like numDoors.
Describe polymorphism and provide a practical example
Polymorphism allows objects to be treated as instances of their parent class, enabling method overriding. Example: Both Dog and Cat classes override an Animal superclass’s speak() method. Calling speak() on a list of Animal objects will produce species-specific responses, like “Woof” or “Meow.”
Why is reducing dependencies between objects beneficial?
Reducing dependencies (tight coupling) increases reusability and flexibility. For example, using an InputStream instead of a File class in a method allows reading data from various sources, like files, networks, or databases.
How does the “is-a” relationship differ from the “has-a” relationship in OOP?
“Is-a” (inheritance) shows that a class is a type of another class, like Dog is-a Animal. “Has-a” (aggregation) shows a class containing another, like a Car has-a Engine.
Define the term “UML diagram” and name two types relevant to OOP.
UML diagrams visually represent classes and relationships. Class Diagrams show fields and methods, while Object Diagrams show specific instances (objects) of these classes and their connections.
Explain encapsulation’s advantages with a security example.
Encapsulation protects data, reducing misuse or errors. In a UserAccount class, marking password as private prevents external classes from directly accessing or altering it, securing sensitive data.
What are the main advantages of using libraries of objects in OOP?
Libraries offer tested, reusable code that saves development time. For instance, Java’s Math library provides functions like sqrt() and pow(), which can be imported and used without rewriting the logic.
What are some limitations of OOP, especially for certain problem types?
OOP may add unnecessary complexity or slow performance for simple tasks or algorithm-heavy applications. It’s also unsuitable for problems that don’t easily map to real-world objects, where procedural or functional programming may be more efficient.
Describe recursion with an example and its pros and cons.
Recursion is when a method calls itself to solve subproblems, like calculating a factorial. It’s concise for certain tasks but can lead to stack overflow errors if overused and may be harder to debug than loops.
Why are coding conventions important in team projects?
They ensure code consistency, readability, and maintainability, allowing team members to understand each other’s work more easily. Examples include naming conventions, indentation, and clear comments.
Explain the difference between static fields/methods and instance fields/methods in Java.
Static fields/methods belong to the class rather than any instance, so they’re shared across all instances. For example, a counter static field tracks the total objects created, while instance fields like name are unique to each object.
Describe the Queue data structure and one real-life example where it’s used.
A Queue uses First-In-First-Out (FIFO) order. An example is a print queue, where print jobs are processed in the order they were added, ensuring fair processing without skipping ahead.
How does a Stack differ from a Queue, and where is a Stack commonly used?
A Stack follows Last-In-First-Out (LIFO) order. Common uses include tracking function calls in recursion, where each call waits until the previous call finishes, resembling a stack of plates.