Object-Oriented Programming Flashcards

1
Q

What are variables and functions?

A

Variable is a label in the program pointing to a data object

Functions is a predefined set of instructions that can involve one or more input and return zero or more output

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

What are attributes and methods?

A

Attribute is a variable associated with an object.

Method is a function associated with an object.

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

In Object-Oriented Programming, what is an object?

A

A data type which can have associated variables and functions.
An object is instantiated from a class.

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

What is a static method?

A

A method that does not have access to its instance or class inside the method

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

What is a class method?

A

A method that has access to the class but does not have access to the instance inside the method

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

How do you make a method a static method in Python?

A

Add @staticmethod above the method definition (and remove the self argument)

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

How do you make a method a class method in Python?

A

Add @classmethod above the method definition (and change self to cls in the arguments)

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

How do you define class attributes?

A

on the lines after the class keyword, outside method definition, i.e.

class ClassName:
  classattribute1 = value
  classattribute2 = value
  def method(self, ...):
    ...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In a method, what is abstraction?

A

Abstraction is the concept that when using a function, the user does not need to know how the function is processing the data and returning the result.
The user only needs to know that the function has this specific purpose

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

Why is abstraction advantageous?

A

It reduces the complexity of viewing the things.
Avoids code duplication and increases reusability.
Helps to increase the security of an application or program as only important details are provided to the user.

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

In Object-Oriented Programming, what is encapsulation?

A

Encapsulation refers to the concept of bundling properties and methods together as a package.

OOP classes utilise encapsulation to hide the internal representation of an object from the outside in a process known as information hiding

Since the users need a way to access them, a set of
methods are made public

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

In Object-Oriented Programming, why is encapsulation advantageous?

A

Keeping the properties of objects private protects them from being accidentally or intentionally modified by unauthorised parties.

Makes the program logic easier to reason about / hides implementation detail / organises code for easier understanding

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

In OOP, what is inheritance?

A
Inheritance refers to the concept of properties and methods in one
class being shared with its subclass.

In OOP, the subclass inherits all the properties and methods of the superclass, but the subclass may have additional functionality from the superclass with the addition of certain properties or method

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

Why is inheritance advantageous?

A

Inheritance promotes software reuse/code reuse as it allows developers to create subclasses that reuse code already declared in a superclass, reducing code duplication and saving time and resources.

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

What is polymorphism?

A

Polymorphism refers to the concept of an object being able to take on multiple forms, where inherited subclass methods can be used in different ways

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

Why is polymorphism advantageous?

A

Polymorphism promotes extensibility as it is implemented through method overriding, which refers to the redefining of the implementation of a method provided by the superclass

Polymorphism enables code generalisation as a methods with the same name can behave slightly differently depending on the subclass that the method is acting on, leading to more concise code, less duplication and allowing for easier maintainability.

17
Q

What are 4 advantages of writing a program as a function instead of as a procedure?

A
  1. The program can be used elsewhere without repeating the code, encouraging code reusablity and reducing code duplication.
  2. Variables locally defined in the function are separated from the global space, which prevents accidental modification.
  3. Changes to the code will only require a change in one location (by definition of a function).
  4. Inputs and outputs are easier to identify, making debugging easier/making the code easier to understand/making collaboration between contributors easier.
18
Q

What is a superclass?

A

superclass encapsulates attributes/properties and methods common to all subclasses