Option D: OOP Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a class in OOP, and how is it different from an object?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain encapsulation with an example.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does inheritance support code reuse?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe polymorphism and provide a practical example

A

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.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is reducing dependencies between objects beneficial?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the “is-a” relationship differ from the “has-a” relationship in OOP?

A

“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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define the term “UML diagram” and name two types relevant to OOP.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain encapsulation’s advantages with a security example.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the main advantages of using libraries of objects in OOP?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are some limitations of OOP, especially for certain problem types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe recursion with an example and its pros and cons.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why are coding conventions important in team projects?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the difference between static fields/methods and instance fields/methods in Java.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe the Queue data structure and one real-life example where it’s used.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does a Stack differ from a Queue, and where is a Stack commonly used?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an Abstract Data Type (ADT) List, and what are two of its common implementations?

A

An ADT List is a data structure supporting ordered collections. ArrayLists (dynamic arrays) and LinkedLists (nodes pointing to the next element) are two Java implementations.

17
Q

Explain binary trees and their benefit in data organization.

A

Binary trees store data hierarchically, with each node having at most two children. They enable efficient searching and sorting, as smaller values are stored to the left and larger values to the right.

18
Q

How does the concept of modularity improve program development?

A

Modularity divides a program into independent parts (modules), making it easier to develop, test, debug, and maintain by focusing on individual components rather than the whole system at once.

19
Q
A