OOP Flashcards

1
Q

What are objects?

A

Objects are Python’s abstraction for data. Formally, it is a collection of data and associated behaviors. All data in a Python program is represented by objects or by relations between objects.

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.

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

What does an object’s type determine?

A

determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The type() function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable.

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

The value of some objects can change. Objects whose value can change are said to be:

A

mutable

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

objects whose value is unchangeable once they are created are called:

A

immutable

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

The value of an immutable container object that contains a reference to a mutable object can change when:

A

the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed.

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

An object’s mutability is determined by its type; for instance, numbers, strings and tuples are _______ while dictionaries and lists are __________.

A

immutable

mutable

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

What does it mean to be object oriented?

A

To be functionally directed toward modeling objects.

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

What is Object oriented analysis?

A

the process of looking at a problem, system, or task that somebody wants to turn into an application and identifying the objects and interactions between those objects.

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

What is object oriented design?

A

the process of converting such requirements into an implementation specification. The designer must name the objects, define the behaviors, and formally specify what objects can activate specific behaviors on
other objects. The design stage is all about how things should be done.

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

Define an iterative development model.

A

In iterative development, a small part of the task is modeled, designed, and programmed, then the program is reviewed and expanded to improve each feature and include new features in a series of short cycles.

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

What’s the difference between an object and a class?

A

Classes describe objects. They are like blueprints for creating an object. Objects are instances of classes that can be associated with each other. An object instance is a specific object with its own set of data and behaviors.

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

What is UML?

A

The relationship between classes of objects in an inventory system aka (Unified Modeling Language).
It is used to specify, visualize, modify, construct and document the artifacts of an object-oriented software-intensive system under development.

It offers a standard way to visualize a system’s architectural blueprints, including elements such as activities, actors, business processes, database
schemas, components, programming language statements, and reusable software components.

It combines techniques from data modeling (entity relationship diagrams), business modeling (work flows),
object modeling, and component modeling. It can be used with all processes, throughout the software development life cycle, and across different implementation technologies.

The Unified Modeling Language is a standardized general-purpose modeling language and nowadays is managed as a de facto industry standard by the Object Management Group (OMG). UML includes a set of graphic notation techniques to create visual models of software-intensive systems.

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

What are objects?

A

Objects are instances of classes that can be associated with each other. An object instance is a specific object with its own set of data and behaviors; Objects are nouns.

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

What is a class?

A

A blueprint of an object. It defines the specific characteristics that are shared by all instances of an object for that class.

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

What is a class attribute?

A

It defines the properties of the class. eg. name, weight, height, color, etc. Attributes are adjectives.

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

What is a class method?

A
The behaviors that can be performed on a specific class of objects.  Methods are like functions in structured programming, but they have access to all the data
associated with that object. Like functions, methods can also accept parameters, and return values.  Methods are verbs.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are parameters to a method?

A

a list of objects that need to be passed into the method that is being called.

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

What does it mean to invoke a method?

A

Also known as method call, it is the process of telling the method to execute itself by passing it the required parameters as arguments.

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

Define encapsulation.

A

The process of hiding the implementation, or functional details, of an object via information hiding. Encapsulation is, literally, creating a capsule, so think of creating a time capsule. If you put a bunch of information into a time capsule, lock and bury it, it is both encapsulated and the information is hidden.

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

What is a model?

A

An abstraction of a real concept.

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

Define abstraction.

A

means dealing with the level of detail that is most appropriate to a given task. It is the process of extracting a public interface from the inner details. when abstracting interfaces, try to model exactly what needs to be modeled and nothing more.

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

What should you consider when designing an interface?

A

When designing the interface, try placing yourself in the object’s shoes and imagine that the object has a strong preference for privacy. Don’t let other objects have access to data about you unless you feel it is in your best interest for them to have it. Don’t give them an interface to force you to perform a specific task unless you are certain you want them to be able to do that to you.

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

Define composition.

A

The act of collecting together several objects to compose a new one. Composition is usually a good choice when one object is part of another object and that object cannot exist without the primary object.

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

Define object diagram.

A

also called an instance diagram. It describes the system at a specific state in time, and is describing specific instances of objects, not the interaction between classes.

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

Define aggregation.

A

Aggregation is almost exactly like composition. The difference is that aggregate objects can exist independently

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

Define inheritance.

A

one class can inherit attributes and methods from another class.

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

Define polymorphism

A

is the ability to treat a class differently depending on which subclass is implemented.

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

Define multiple inheritance.

A

allows a subclass to inherit functionality from multiple parent classes.

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

Define asynchronous message.

A

typically means the first object calls a method on the second object which returns immediately.

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

Define refactoring.

A

improve the design by moving code around, removing duplicate code or complex relationships in favor of simpler, more elegant designs.

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

When is inheritance most useful?

A

when the subclasses have some kind of specialization. Specialization is creating or changing attributes or behaviors on the subclass to make it somehow different from the parent class.

32
Q

What is a primitive data type?

A

A variable of a primitive type directly contains the value of that type (in other words, they are value types).

A variable of a non-primitive type doesn’t contain the value directly; instead, it is a reference (similar to a pointer) to an object. (It is not possible in Java to create user-defined value types).

33
Q

What is a use case model?

A

A representation of the systems we intend to model along with its intended functions and environment drawn as a diagram. It names the actors and gives the heading for each scene. Use case models have no timely order.

34
Q

What is a class diagram?

A

For object-oriented software engineers, the Class diagram is the most important one. A Class diagram consists of classes and lines between them. The classes
themselves are drawn as boxes, having two compartments, one for methods and one for
attributes.

35
Q

What are the 3 relationships between objects?

A

~ association
~ aggregation
~ inheritance

36
Q

Define association.

A
a static relationship, usually one class is attribute of another class, or one class uses
another class
37
Q

Define aggregation.

A

for instance an order consists of order details

38
Q

Define inheritance.

A

describes a hierarchy between classes

39
Q

What is a diagram?

A

a partial graphic representation of a system’s model.

40
Q

What are the two different views of a system model that UML diagrams represent?

A
Static (or structural) view: emphasizes the static structure of the system using objects, attributes, operations
and relationships. The structural view includes class diagrams and composite structure diagrams.

Dynamic (or behavioral) view: emphasizes the dynamic behavior of the system by showing collaborations
among objects and changes to the internal states of objects. This view includes sequence diagrams, activity
diagrams and state machine diagrams.

41
Q

In UML there are 14 types of diagrams divided into 2 categories. What are they?

A

Seven diagram types represent
structural information, and the other seven represent general types of behavior, including four that represent
different aspects of interactions. These diagrams can be categorized hierarchically as shown in the following
diagram:

42
Q

Define Class diagram:

A

describes the structure of a system by showing the system’s classes, their attributes, and the
relationships among the classes.

43
Q

Define Component diagram:

A

describes how a software system is split up into components and shows the dependencies
among these components.

44
Q

Define Composite structure diagram:

A

describes the internal structure of a class and the collaborations that this structure makes possible.

45
Q

Define Deployment diagram:

A

describes the hardware used in system implementations and the execution environments and artifacts deployed on the hardware.

46
Q

Define Object diagram:

A

shows a complete or partial view of the structure of a modeled system at a specific time.

47
Q

Define Package diagram:

A

describes how a system is split up into logical groupings by showing the dependencies among these groupings.

48
Q

Define Profile diagram:

A

operates at the metamodel level to show stereotypes as classes with the <> stereotype, and profiles as packages with the <> stereotype. The extension relation (solid line with closed, filled arrowhead) indicates what metamodel element a given stereotype is extending.

49
Q

Define Use case diagram:

A

describes the functionality provided by a system in terms of actors, their goals represented as use cases, and any dependencies among those use cases.

50
Q

Define Activity diagram:

A

describes the business and operational step-by-step workflows of components in a system. An activity diagram shows the overall flow of control.

51
Q

Define state machine diagram:

A

describes the states and state transitions of the system.

52
Q

Define Sequence diagram:

A

shows how objects communicate with each other in terms of a sequence of messages. Also indicates the lifespans of objects relative to those messages.

53
Q

Define Communication diagram:

A

shows the interactions between objects or parts in terms of sequenced messages. They represent a combination of information taken from Class, Sequence, and Use Case Diagrams describing both the static structure and dynamic behavior of a system.

54
Q

Define Interaction overview diagram:

A

provides an overview in which the nodes represent communication diagrams.

55
Q

Define Timing diagrams:

A

a specific type of interaction diagram where the focus is on timing constraints.

56
Q

How do you create a class in Python that doesn’t do anything?

A
class MyFirstClass:
         pass
57
Q

What is considered to be Python’s delimiter?

A

tab or 4 spaces

58
Q

Can you instantiate a class that does nothing?

A

yes

59
Q

Define dot notation

A

When you assign a value to an attribute on an object and use the syntax . = . The value can be anything: a Python primitive, a
built-in data type, another object. It can even be a function or another class!

60
Q

How do you give a class that does nothing attributes?

A
We can set arbitrary attributes on an instantiated object using the dot notation:
class Point:
        pass
p1 = Point()
p2 = Point()
p1.x = 5
p1.y = 4
p2.x = 3
p2.y = 6
print(p1.x, p1.y)
print(p2.x, p2.y)
61
Q

A method in python is identical to?

A

defining a function. essentially, the method really is just a function that happens to be on a class.

62
Q

How do you start a method?

A

It starts with the keyword def followed by a space and the name of the method. This is followed by a set of parentheses containing the parameter list, and terminated with a colon. The next line is indented to contain the statements inside the method. These statements can be arbitrary Python code operating on the object itself and any parameters passed in as the method sees fit.

63
Q

What’s the difference between methods and normal functions?

A

all methods have one required argument. This argument is conventionally named self;

64
Q

What is the self argument?

A

a reference to the object that the method is being invoked on.

65
Q

How do you pass multiple arguments to a method?

A
class Point:
        def move(self, x, y):
               self.x = x
               self.y = y
        def reset(self):
               self.move(0, 0)
        def calculate_distance(self, other_point):
               return math.sqrt(
                         (self.x - other_point.x)**2 +
                         (self.y - other_point.y)**2)
66
Q

How do you call the methods

A

how to use it:

point1 = Point()
point2 = Point()

point1. reset()
point2. move(5,0)

print(point2.calculate_distance(point1))
assert (point2.calculate_distance(point1) ==
point1.calculate_distance(point2))
point1.move(3,4)
print(point1.calculate_distance(point2))
print(point1.calculate_distance(point1))
67
Q

Most object-oriented programming languages have the concept of a constructor. Define constructor.
How is python different?

A

a special method that creates and initializes the object when it is created.
Python is a little different; it has a constructor and an initializer.

68
Q

What is python’s initialization method?

A

__init__
this is a special method that the Python interpreter will treat as a special case. The __init__ method requires the user to supply arguments when an object is instantiated.

69
Q

What are python modules?

What happens when we have more than 1 python file in a folder?

A
modules are simply python files and nothing more.
The single file in our small program is a module. Two Python files are two modules. If we have two files in the same folder, we can load a class from one module for use in the other module.
70
Q

What is the import statement used for?

A

importing modules or specific classes or functions from modules.

71
Q

What are several ways to import modules?

A

import database
db = database.Database()
This version imports the database module into the products namespace (the list of names currently accessible in a module or function), so any class or function in the database module can be accessed using database. notation.
________________________
or
Alternatively, we can import just the one class we need using the from…import syntax:

from database import Database
db = Database()
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
or
If, for some reason, products already has a class called Database, and we don't want the two names to be confused, we can rename the class when used inside the products module:
from database import Database as DB
db = DB()
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
or
We can also import multiple items in one statement. If our database module also contains a Query class, we can import both classes using:

from database import Database, Query
or all classes using:

from database import *
****Bear in mind that using the import * syntax can bring unexpected objects into our local namespace. Sure, it will import all the classes and functions defined in the module being imported from, but it will also import any classes or modules that were themselves imported into that file!****

There should be no magic variables that seem to come out of thin air. We should always be able to immediately identify where the names in our current namespace originated.

72
Q

Define a package.

A

a collection of modules in a folder. The name of the package is the name of the folder. All we need to do to tell Python that a folder is a package and place a (normally empty) file in the folder named __init__.py. If we forget this file, we won’t be able to import modules from that folder.

73
Q

Define absolute imports.

A
the complete path to the module, function, or path we want to import. If we need access to the Product class inside the products module, we could use any of these syntaxes to do an absolute import:
import ecommerce.products
product = ecommerce.products.Product()
or
from ecommerce.products import Product
product = Product()
or
from ecommerce import products
product = products.Product()
74
Q

Define relative imports.

A
Relative imports are basically a way of saying "find a class, function, or module as it is positioned relative to the current module". For example, if we are working in the products module and we want to import the Database class from the database module "next" to it, we could use a relative import:
from .database import Database

The period in front of database says, “Use the database module inside the current package”.

75
Q

How do we access a parent package?

A

That is easily done with two periods:
from ..database import Database

We can use more periods to go further up the hierarchy.