B3: Object Oriented Programming

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/57

encourage image

There's no tags or description

Looks like no tags are added yet.

58 Terms

1
New cards

What is object-oriented programming (OOP)?

A form of programming that creates code for classes of objects, allowing many objects to be created from a single code base, making development more modular and extensible.

2
New cards

Fill in the gap: OOP lets many objects be created from a single ______ , making development more modular and extensible.

code base

3
New cards

Name the five advantages of OOP.

Increased modularity, code reusability, encapsulation, scale and collaboration.

4
New cards

How does increased modularity benefit a project?

Objects can be created and modified independently of each other, which makes large code bases easier to manage.

5
New cards

How does OOP achieve code reusability?

A class, once written, can be imported into other projects and reused many times.

6
New cards

How does OOP allow projects to scale?

Modularity and reusability allow a project to grow in size while remaining maintainable.

7
New cards

How does OOP improve collaboration?

Modularity makes it easy to delegate different parts of a project to different team members, so more people can work on the same project.

8
New cards

Name the six disadvantages of OOP.

Learning curve, increased complexity, overuse, overhead, lack of optimisation and object-centric design limitations.

9
New cards

Why is the learning curve a disadvantage of OOP?

Concepts such as classes, objects and inheritance can be difficult for beginner programmers to understand.

10
New cards

Why can OOP be a disadvantage for small problems?

A procedural approach would suffice, so a purely OOP approach is unnecessarily complex and harder to debug and maintain.

11
New cards

What can happen if OOP is overused or forced onto a problem?

Deep inheritance hierarchies, leading to code that is difficult to understand and maintain.

12
New cards

Fill in the gap: OOP introduces additional ______ in time and space requirements compared with paradigms that have less abstraction.

overhead

13
New cards

Why does OOP show a lack of optimisation?

It focuses on abstractions that improve modularity, at the cost of organising code in ways that are efficient for the CPU.

14
New cards

Which type of application may suit lower-level paradigms better than OOP, and why?

Performance-critical applications, because OOP's abstraction costs efficiency.

15
New cards

What are the object-centric design limitations of OOP?

Some problem domains are better modelled with other paradigms, such as functional or procedural programming.

16
New cards

Term: Name the three rows of a UML class diagram and what each contains.

Row 1 is the class name, row 2 is the variables (with data types) and row 3 is the methods (with parameter signatures and return data type).

17
New cards

In a UML class diagram, what does a + sign mean and what does a - sign mean?

  • means public and - means private.
18
New cards

Where in a UML class diagram are the instance variables and their data types listed?

In the second row.

19
New cards

Where in a UML class diagram are the methods with their parameters and return type listed?

In the third row.

20
New cards

Define instantiation.

The line of code that declares a new object variable based on the template provided by a class, which then executes the constructor to initialise the object.

21
New cards

Define constructor.

A special method in a class that is automatically executed during instantiation, whose main task is to initialise the instance variables before the object can be used by other code.

22
New cards

What is the name of the constructor method in Python?

init()

23
New cards

What does the keyword self do in a class?

It refers to the current object instance of the class and must prefix every instance variable or method.

24
New cards

Fill in the gap: ______ must be listed as the first parameter of every method belonging to a class.

self

25
New cards

Define encapsulation.

Bundling data and the methods that manipulate that data together into a single object, hiding the implementation details from outside code.

26
New cards

Fill in the gap: Encapsulation hides the ______ details of an object from outside code.

implementation

27
New cards

Define access modifiers.

Mechanisms provided by a programming language to control the visibility of methods and variables within an object.

28
New cards

In Python, what is the default access level of variables and methods?

Public.

29
New cards

What does a single underscore prefix (e.g. _variable) indicate?

That it is for internal use only, within the class.

30
New cards

What does a double underscore prefix (e.g. __variable) do in Python?

It is name mangled: the interpreter renames the variable or method at runtime, making it harder to access from outside the class.

31
New cards

Why was name mangling created, and does it enforce strict access control?

It was created to avoid naming conflicts, not to enforce strict access control.

32
New cards

Define accessor and give its other name.

A public method that lets external code access the value of a private instance variable, also known as a getter method.

33
New cards

Define mutator and give its other name.

A public method that lets external code update the value of a private instance variable, also known as a setter method.

34
New cards

Accessor or mutator: a method that sets the value of a private instance variable.

Mutator (setter).

35
New cards

Define static variables and methods.

Variables and methods that belong to the class rather than individual objects; only one copy is created and it is shared by all instances.

36
New cards

Define non-static variables and methods.

The normal instance variables and methods, where each object creates its own unique copy.

37
New cards

When are static variables and methods created?

At runtime by the interpreter or compiler when the class is defined, so no instance is needed for them to exist.

38
New cards

What happens when one object changes a non-static variable?

Nothing happens to other objects, because each object has its own copy of the variable.

39
New cards

Give a common use of a static variable.

Tracking ID values so that each object has its own unique ID number.

40
New cards

Fill in the gap: ______ variables belong to the class, while ______ variables belong to each individual object.

static, non-static

41
New cards

Define inheritance.

Where a class takes a copy of an existing class as the starting point for its methods and variables, which can then be overridden and extended to add functionality.

42
New cards

What is the main aim of inheritance?

To ease code reuse in complex projects, so only the custom modifications need to be written.

43
New cards

What are the two other names for a derived class?

Child class or subclass.

44
New cards

What are the two other names for the origin class?

Parent class or superclass.

45
New cards

How is an inheritance relationship invoked in Python?

By putting the parent class in parentheses in the class definition, e.g. class Customer(Person).

46
New cards

What must be done with the superclass constructor in a subclass, and where?

It must still be called, as the first line of the subclass constructor, using super().init()

47
New cards

Define polymorphism.

Meaning 'many forms': objects exhibit different behaviours based on their specific class implementation while still adhering to a shared interface or contract.

48
New cards

Define overriding.

When a child class creates a property or method with the same name as one in the parent class, thereby replacing it.

49
New cards

Give a GUI example of inheritance combined with polymorphic overriding.

Core text input box functionality is written once, then inherited and overridden to create more specialised input types.

50
New cards

Give a game design example of inheritance combined with polymorphic overriding.

A generic bot with a random move strategy is inherited by specialised bots, which override the strategy or abilities function.

51
New cards

What does overriding eq(self, other) allow?

Custom logic for the == operator when comparing two objects.

52
New cards

What does hash(self) do and what must be true of equal objects?

It returns an integer hash value for the object, and objects considered equal must have the same hash value.

53
New cards

Which methods are overridden for the comparison operators

lt, le, gt and ge.

54
New cards

What do getitem, setitem and delitem do?

They are called to retrieve, set or delete an item using the indexing syntax obj[key].

55
New cards

Which two methods make an object iterable?

iter(self) and next(self).

56
New cards

Define abstract class.

A generic template with no executable code of its own that cannot be instantiated and is designed to be extended by other classes.

57
New cards

When should you use an abstract class?

When all subclasses should have certain methods or fields but the implementation is specific to each subclass, so a default implementation makes no logical sense.

58
New cards

What is the benefit of an abstract class?

It provides standardisation and consistency, so users know certain methods are always available and new derived classes must adhere to the defined contract.