Object-Oriented Programming Flashcards

1
Q

What are the major programming paradigms?

A

Imperative
–uses statements to change a program’s state

Declarative
–describes the desired result, system figures out how to achieve it

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

What are the tyes of imperative programming? (3)

A
  • Procedural
  • OOP
  • Recursive Programming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the tyes of declarative programming? (3)

A
  • Functional Programming
  • Logic Programming
  • Constraint Programming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What type of programming does Python use?
What about java?

A

-tends toward functional, but supports procedural and object-oriented
-primarily object-oriented, but added constructs to allow
functional programming

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

Describe the features of objects (3, one depends on the language)

A

An object:
–collects together related data (“fields”, “attributes”)
–collects together subroutines to work on that data (“methods”, “member functions”, etc.)
–may (depending on the language) allow hiding the data members and/or function members, restricting access to them to its subroutines only

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

What are the features of OOP languages? (5)

A

Encapsulation
– related data and procedures are kept together, and data or even
procedures may be hidden from outside access
Classes (definition of data/procedures)
– class variables (belong to the class as a whole, e.g. “static” in Java)
Objects (instances of a Class)
– instance variables (belong to a particular object)
Composition
– objects can contain other objects as members
Inheritance or Delegation

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

What is not a defining feature of OOP but almost always present?

A

Inheritance

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

What is inheritance?

A

means that a subclass can use subroutines or data members defined in an ancestor class

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

What is delegation?

A

means that a member of an object (“receiver”) is evaluated in the context of some other object (“sender”)
– can be done by passing the sender to the receiver as a parameter

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

Describe polymorphism? What allows it?

A

Polymorphism
– Inheritance allows Polymorphism
– a single function call in the source code can invoke different functions at runtime depending on the type of the object
– implemented by having a table of function pointers associated withevery object (one table per class)

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

What is SOLID?

A

Acronym for five principles of Object-Oriented Design
– SRP: Single Responsibility Principle
– OCP: Open/Closed Principle
– LSP: Liskov Substitution Principle
– ISP: Interface Segregation Principle
– DIP: Dependency Inversion Principle

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

What is a singleton?

A

A class which never has more than a single object instantiated

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

How do you implement a singleton? (2)

A
  1. hiding the class constructor –
  2. providing a static function that returns the sole instanc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s an adapter? (1)

A

Convert the interface of a class to look like another class’s interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  • What’s an iterator? (1)
  • How is it implemented?
A
  • allows traversal of a container object and accessing the container’s elements
  • typically implemented as a class with functions such as init(), next(), and getItem()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the benefits of an iterator? (2)

A
  • avoids exposing the container’s data structure
  • can define new traversal operations without changing container’s interface (algorithms and containers are decoupled by iterators)
17
Q

For python, what are two supported functions and one other entity related to iterating class objects?

A
  • it = obj.__iter__() return an iterator object
  • (often self) – it.__next__() get next item
  • raise StopIteration if no next item
18
Q

What is the “Strategy” design pattern? (3)

A
  • instead of “class Foo extends Bar” [Java] or class Foo(Bar) [Python], Foo has a member of type Bar which it invokes as needed
  • this allows runtime selection of behavior
  • The Strategy pattern decouples behaviors from the classes that use the behaviors

Example: Java’s “interface”/”implements” are a form of this design pattern

19
Q

Give an example of something that implements the strategy design pattern

A

– Java’s “interface”/”implements” are a form of this design pattern

20
Q

What’s a useful way to think of when to use inheritance?

A

think of inheritance as “is-substitutable-for”, not “is-a” (Liskov substitution principle)

21
Q

What’s an interface?

A

An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class) (e.g. methods)

22
Q

DIP – Dependency Inversion Principle (2) How is it achieved? (idea and term for it)

A

● High-level modules should not depend on low-level modules; both should depend on abstractions

● Abstractions should not depend on details; details should depend on abstractions

● Achieved by not directly instantiating dependencies (i.e. no “new MyDependency()” anywhere in the class)

– typically use Dependency Injection (in its basic form, an instance of the dependent class is passed in by the caller)

23
Q

What’s polymorphism?

A

objects of different types can be accessed through the same interface. Each type can provide its own, independent implementation of this interface

24
Q

What’s the test for a polymorphic object?

A

If the object successfully passes multiple is-a or instanceof tests, it’s polymorphic. As I’ve described in my post about inheritance, all Java classes extend the class Object. Due to this, all objects in Java are polymorphic because they pass at least two instanceof checks.

25
Q

Describe the concept of factories

A

Use a factory object and a factory method

  • a Factory Object is an object that can create other objects
  • a Factory Method is a method of an object (or class) which returns new instances of one or more classes
26
Q

Are constructors polymorphic? What about factories?

A
  • constructors are not polymorphic
  • factories can be polymorphic – a single function could return objects of multiple different types
27
Q

What’s an abstract factory?

A

A class does not create objects itself, but instead delegates the creation to another class which is specified at run-time