Classes and objects review ( week 13 object oriented programming)

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/34

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

35 Terms

1
New cards

What is a class in python?

  • A template (blueprint) for creating objects it defines attributes (data) and methods (functions

  • one class can have many objects

2
New cards

What is an object?

an object is an instance of a class

3
New cards

How are classes and objects related?

  • an object is an instance of a class

  • one class can be used to create multiple objects (i

4
New cards

What othwr built in classe sin python have been covered?

  • int string list etc

  • are all classes

5
New cards

What does x = 12 what class is x an instance of?

x is part of the int class

6
New cards

if name = Helen, what class is name an instance of?

  • the string class (str)

7
New cards

What is a class attribute (class variable)

A variable defined inside a class that is shared by all instances of that class.

8
New cards

In the following class, what are x, y, and z?

x = 10

y = 6

z = 9

  • Class attributes

9
New cards

What does pos1 = MyPosition() do?

  • it adds another object instance of the class (My position)

10
New cards

Do class attributes have the same value for every object?

  • Yes unless directly specified for a specific instance

11
New cards

What prints when you run this?

pos1 = MyPosition()

print(pos1.x)

  • prints the value of the class attribute in this case x

12
New cards

Why use Object oriented prgramming (OOP)?

  • instead of one big main file the data is seperated into different classes 

  • each class defines its own variables and methods (functions)

  • you work with objects and data rather than functions or logic

13
New cards

What things define object oriented programming?

  • CLasses the building block

  • Objects instance of a class

  • methods functions that objects preform

  • attributes ( the state of an object aka the data)

14
New cards

What are the 4 benefits of OOP?

  • code reusability

  • improved moduality and organization

  • easier to maintain and update

  • better flexibility and scalability for adding new features

15
New cards

What is abstraction?

The inner workings of the class (and all its details) is hidden from outside the class

  • the process of hiding complex implementation details and only showing the essential features of an object

  • focus on what the object does rather than how it does it

  • boundary between internal implementation and external interface allowing the internal details to be changed without affecting how the object is used

16
New cards

What is encapsulation?

  • Variable sinside the class should be protected from the outside manipulation

  • the outsider should not be allowed to see or change the details inside the class such as its variables

  • python does NOT force encapuslation

  • should always call a method to access or change a variable

17
New cards

What is the main difference between abstraction and encapsulaion?

  • abstraction hides the implementation details (car is a car, not all its components)

  • encapsulation protects and has control access to the data (like a shield) to prevent unauthorized access

18
New cards

What is a contructor in python?

  • a special method called (__init__)?that runs automatically when an object is created

19
New cards

What is the main purpose of the constructor (__init__)?

  • to initialize and set the instances attributes so each object starts at a consistent state

20
New cards

What does self refer to inside __init__?

The specific instance (object) being created.

21
New cards

What is the difference between a class attribute and an instance attribute?

  • Class attribute → shared by all objects

  • Instance attribute → unique to each object, defined inside __init__

22
New cards

What happens if a class has no __init__ method?

Python uses a default constructor that doesn’t initialize attributes.

23
New cards

What does "instantiating a class" mean?

Creating an object by calling the class like a function → e.g., MyPosition(10, 15, 20).

24
New cards

What is a module in Python?

A file containing Python code (variables, functions, classes) that can be imported into other programs.

25
New cards

What does the shapes module contain?

Three classes: Circle, Triangle, and Square.

26
New cards

What is the constructor for the Circle class?

def init(self, r):

self.radius = r

A method that initializes the radius of the circle.

27
New cards

What is the instance variable in the Circle class?

self.radius

28
New cards

What is the constructor for the Triangle class?

def init(self, b, h):

self.base = b

self.height = h

29
New cards

What does the Square class currently contain

class Square:

pass

none

30
New cards

What keyword is used to create an empty class?

pass

31
New cards

How do you import all classes from the shapes module?

from shapes import Circle, Triangle, Square

32
New cards

Why does c.calc_area() work?

Because c is an object with a method defined inside its class.

33
New cards
34
New cards
35
New cards