OPO Flashcards
What is an example of dynamic binding?
Method overriding
For which case would the use of a static attribute be appropriate?
The weather conditions for each house in a small neighborhood
Why would you create an abstract class, if it can have no real instances?
to avoid redundant coding in children. and to have common behavior in derived classes
When does static binding happen?
at compile time
What is the best reason to use a design pattern?
It will result in code that is more extensible and maintainable
What is encapsulation?
hiding the data and implementation details within a class
What is an is-a relationship?
A subclass object has an is-a relationship with its superclass or interface
You want a method with behavior similar to a virtual method. It’s meant to be overridden, except that it does not have a method body. It just has a method signature. What kind of method should you use?
an abstract method
Which code creates a new object from the Employee class?
Employee current Employee = new Employee();
Which type of constructor cannot have a return type?
Trick question, constructors do not have a return type
When is a constructor executed?
When an object is created from a class using the new keyword
If a local class is defined in a function, what is true for an object of that class?
The object can be accessed, declared, and used locally in that function.
Which two blocks are used to handle and check errors
try and catch
Why would you implement composition using an id instead of a reference?
It makes it easier to save the entity
It can make the entity retrieval more efficient
It minimizes coupling
Which statement best describes the method of inheritance in OOP?
Inheritance describes the ability to create new classes based on an existing class
Which type of inheritance, when done continuously, is similar to a tree structure?
Hierarchical
Is a default parameter’s constructor equivalent to the default constructor?
No
What are some advantages of using getters and setters?
Getters and setters provide encapsulation of behavior
Getters and setters provide a debugging point for when a property changes at runtime
Getters and setters permit different access levels.
In context of OOP, what is association?
Association is a relationship where all objects have their own life cycle and there is no owner
How are user stories different from use cases?
User Stories are shorter and less detailed
Which type of inheritance must be used so that the resultant is hybrid?
Multiple and hierarchical
Hybrid inheritance is a composition of multiple and hierarchical inheritances
A language that does not support polymorphism but supports classes is considered what?
an object-based language
If two classes combine some private data members and provides public member functions to access and manipulate those data members. Where is abstraction used?
Abstraction is using public member functions to access and manipulate the data members
What are the five Creational Design patterns by the Gang of Four?
Abstract Factory Builder Factory Method Prototype Singleton
In multilevel inheritance, one class inherits how many classes?
one class only
if an object is passed by reference, the changes made in the function are reflected ____.
to the main object of the caller function, too
A mobile phone is made up of components such as a motherboard, camera, and sensors. The motherboard represents all the functions of a phone, the display shows the display only, and the phone is represented as a whole. Which of the following has the highest level of abstraction?
The mobile phone
Which class has the highest degree of abstraction in a multilevel inheritance relationship of five levels?
the class at the first level
Which is NOT one of the basic types of inheritance?
double inheritance
Why is code duplication so insidious?
One has to maintain all the duplicates
When and how often is a static constructor called?
It is called initially when an object is created and only one time
What does the code shown below demonstrate, and why?
static void Multiply(int num1, int num2) {};
static void Multiply(double num1, double num2, double num3) {};
static void Multiply(float num1, float num2) {};
Method overloading
It allows the creation of several methods with the same name, which differ by the type of input via parameter
What is the purpose of a static constructor?
To initialize all the members with static value
What are CRC Cards?
Class responsible collaboration cards are a brainstorming tool used in the design of oop software
How are contents of a composition different from those of aggregation?
if a composition dies, the contents die
Which statement about compositions and aggregations is true?
if a composition dies, the contents die
What is the result of using more abstraction?
it can limit code readability
What is an abstract class?
A class with no body, that is meant to be inherited (implemented?)
Do member functions need to be declared inside the class definition?
Yes
Why is inheritance used when creating a new class?
To avoid writing duplicate code
In addition to attributes and behaviors, what quality must a class possess?
a name
Which words in the following list are candidates for objects: trumpet, clean, enrage, leaf, tree, collapse, active, and lively?
leaf, tree, and trumpet
What best describes what object-oriented programming does?
It focuses on objects that interact cleanly with one another
Can abstract classes be used in multilevel inheritance?
Yes
What type of inheritance may lead to the diamond problem?
Multiple inheritance
What is the relationship between abstraction and encapsulation?
Abstraction is about making relevant information visible, while encapsulation enables a programmer to implement the desired level of abstraction
public and private are examples of?
Access Specifiers
What is a reference to an object?
It is the address where the variables and methods of an object are stored
Why is unit testing harder in OOP than functional programming?
Objects may maintain an internal state, which is not easily accessible by the tests
What is the function of a user diagram?
It links actors to roles played in all use cases
How do object behavior and attributes differ?
Attributes describe a state; behaviors describe a change
The open/closed principle states that classes should be open for __ but closed for __.
Open for extension but closed for modification
Why would you override a method of a base class?
to define a custom implementation of an inherited member
What is a copy constructor?
A unique constructor for creating a new object as a copy of an object that already exists. There will always be only one copy constructor that can be either defined by the user or the system
What defines the catch block most accurately?
The catch block that will be executed is the one that best matches the type of the exception thrown
There are five classes. Class E is derived from class D, D from C, C from B, and B from A. Which class constructor(s) will be called first if the object of E or D is created?
A
You have modules that are dependent on each other. If you change one module, you have to make changes in the dependent modules. What term is used to describe this problem, and what is a potential solution?
???
Cohesion. A solution is to show that each module has certain responsibilities and to use an anticohesive design pattern.
Encapsulation. A solution is to implement one of the SOLID principles to ensure the modules do not encapsulate with each other.
Coupling. A solution is to refactor the code to be loosely coupled by using inversion of control and dependency injection
.
Dependency. A solution is to implement polymorphism and abstraction to change and extract dependent elements of a module so that it functions on its own.
__________ describes an aggregation
???
A class of resources
A group of methods
A collection of objects
A list of children
And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.
x
An abstract class can have constructors—this is one major difference between an abstract class and an interface
x
An interface is basically a contract—it doesn’t have any implementation. An interface can contain only method declarations; it cannot contain method definitions.
x
Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. By contrast, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion – i.e. if a future expansion is likely in the class hierarchy. If you would like to provide support for future expansion when using interfaces, you’ll need to extend the interface and create a new one.
x
n interface differs from an abstract class because an interface is not a class. An interface is essentially a type that can be satisfied by any class that implements the interface.
x
But the real question is why did we declare the getArea method to be abstract in the Figure class? Well, what does the getArea method do? It returns the area of a specific shape. But, because the Figure class isn’t a specific shape (like a Circle or a Rectangle), there’s really no definition we can give the getArea method inside the Figure class. That’s why we declare the method and the Figure class to be abstract. Any classes that derive from the Figure class basically have 2 options:
- The derived class must provide a definition for the getArea method OR
- The derived class must be declared abstract itself.
x
An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.
a
An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public.
a
If you think you will need to add methods in the future, then an abstract class is a better choice. Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods. That can be quite a hassle.
c
Interfaces are a good choice when you think that the API will not change for a while.
c
Interfaces are also good when you want to have something similar to multiple inheritance, since you can implement multiple interfaces.
f
An abstract class can never be sealed or static.
An abstract class can have abstract as well as non abstract methods.
The abstract keyword can be used with class, methods, properties, indexers and events.
Abstract members can only be declared inside an abstract class.
An abstract member cannot be static or private.
An abstract method cannot be marked virtual.
A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible.
Without an abstract class, we cannot implement the Template Method Pattern.
fdh
However, there is the advantage of using an interface over an abstract class; that is “Multiple Inheritance Support”.
However, there is the advantage of using an interface over an abstract class; that is “Multiple Inheritance Support”.
derived class
derived class
By the definition of abstract class, any class that implements it must provide definitions to all its abstract members.
vs interface
What is the only way to do multiple inheritance in C#?
By using interfaces
no access modifier is allowed with interface methods
also cannot declare with Static
implemented vs inherited?
abstract is inherited, interfaces are implemented
In derived classes, the access modifier must always be _____?
derived classes, the access modifier must always be public
Can an interface be instantiated?
An interface has no implementation and cannot be instantiated. However, it can be referenced to the class object that implements it.
Tell the compiler which method is specific to which interface using Explicit Implementation. It can be done by prefixing the interface name with the method definitions in the derived class.
interface IDebitCard { void CardNumber(); }
//Second Interface ICreditCard interface ICreditCard { void CardNumber(); }
//Customer Class implements both the interfaces class Customer: IDebitCard, ICreditCard {
void IDebitCard.CardNumber()
Why might you choose an abstract class over an interface and vice versa?
If you have some kind of default functionality to share across classes in the hierarchy, you can use an abstract class. But if you don’t have any default implementation and just need to define contracts for derived classes to follow; interface is the most preferred way.
It is a standard rule when using an interface, be sure you have done it right the first time. Why?
Once the interface is implemented by derived classes, it is difficult to update or modify the interface since everyone else’s code breaks.
Now if the Apple class wants to implement TouchID features, it can easily be done by defining another interface IFeatures.
interface ISmartPhone { void OS(); void AppStore(); }
//New Interface meant only for Apple Class interface IFeatures { void TouchID(); }
class Apple: ISmartPhone, IFeatures {
What is an abstract class?
An abstract class is a half defined parent class
What is an interface?
An interface is a contract
What are events?
Events are encapsulation over delegates
What kind of model do events create?
Events create a publisher subscriber model
What is a delegate?
A Delegate is a pointer to a function
What is a delegate useful for?
Delegates are very useful as callbacks to communicate between threads