Object Oriented Programming Flashcards

(28 cards)

1
Q

What does OOP stand for?

A

Object Oriented Programming

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

True or False: In OOP, an object is an instance of a class.

A

True

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

Fill in the blank: The four main principles of OOP are encapsulation, inheritance, ______, and polymorphism.

A

abstraction

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

What is the purpose of encapsulation in OOP?

A

To restrict access to certain components of an object and protect its state.

All you need to know is the interface of a class to use it.

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

Define inheritance in the context of OOP.

A

Inheritance allows a new class to inherit properties and methods from an existing class.

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

True or False: Polymorphism allows methods to do different things based on the object that it is acting upon.

A

True

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

What is a class in OOP?

A

A blueprint for creating objects that defines properties and methods.

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

What does the term abstraction mean in OOP?

A

Abstraction is the concept of hiding complex implementation details and showing only the essential features of an object.

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

What is the name for the process of creating an object from a class?

A

Instantiation.

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

What is the purpose of a ‘getter’ method in a class?

A

They are used to access non-public attributes from a class.

See here for using the property decorator in Python instead of getter and setter methods.

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

What is the purpose of a ‘setter’ method in a class?

A

To set the value of non-public attributes in a class.

See here for using the property decorator in Python instead of getter and setter methods.

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

What are the advantages of using OOP to build a software program?

A
  • Organisation
  • Reusability - inheritance reduces the need to replicate code, reduces redundancy.
  • Collaboration - developers can work on different classes simultaneously.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the disadvantages of using OOP to build a software program?

A
  • It can create longer programs than using a procedural approach - can slow performance.
  • Greater complexity can make code more difficult to understand.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the relationship between a superclass and subclass?

A

A subclass is derived from a superclass (aka base class or parent class).

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

What is a helper class?

A

A class which is used to assist in providing functionality which is not the main objective of the application in which it is used.

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

What is the difference between a method and a function?

A

Methods are tied to objects/classes and need an instance of an object/class to be called.
Functions are independent and can be called anywhere.

17
Q

How do you make a protected variable or method in Python?

A

By prefixing the variable or method name with _.

18
Q

What is an abstract class?

A

A class which is not instantiated, but used to define other classes.

~ a base class.

19
Q

What is a static method?

A

A method defined inside a class which has no access to anything else in the class (e.g. self)

Created using the @staticmethod decorator in Python.

20
Q

What is a class method?

A

A method defined inside a class which only has access to class variables and other class methods, not specific instance attributes.

Created using the @classmethod decorator in Python.

21
Q

Which constructor function is run every time a new instance of a class is created?

A

\_\_init\_\_()

22
Q

When are try/except blocks appropriately used in Python?

A

When an error cannot be avoided and you are aware of it, plus you have a specific plan to handle the error.

Should probably be limited to cases where taking in external data.

23
Q

How do you create a child class in Python?

A

By passing the name of the parent class in parentheses after the child class name:

def parent_class():
...
def child_class(parent_class):
...
24
Q

Liskov substitution principle

How does it apply to inheriting classes in Python?

A

Every instance of an inheriting child class is a valid instance of the parent class.

25
How are subclasses of an abstract class guaranteed to contain the methods of the abstract class?
Because these methods must be explicitly defined inside the subclasses.
26
Explain the following command seen within a child class definition: `super().__init__`
Call the initialisation function from the super class (parent class).
27
How do you make a private variable in Python? | Private variable cannot be directly accessed outside the relevant class.
By prefixing the variable name with double underscore, `__`.
28
What is the purpose of creating a class in OOP?
To bring related data and functions together, creating a blueprint for individual instances with specific attributes and methods.