u08_slides-classes-flashcards

1
Q

What is Object-Oriented Programming (OOP)?

A

A programming paradigm based on objects that combines variables, functions, and data structures to increase code reusability and modularity

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

What are the two main components of objects in OOP?

A
  1. State (data/attributes/fields), 2. Behavior (methods/functions)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a class and an object?

A

A class is like a blueprint that defines attributes and methods, while an object is a specific instance of a class that exists uniquely

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

What is the naming convention for Python classes?

A

Class names should use CapWords convention (e.g.

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

What is the purpose of the __init__ method in a class?

A

It’s a special method for object initialization, called when creating a new instance of the class

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

Why must all object methods have ‘self’ as their first parameter?

A

‘self’ references the instance the method was called on, allowing access to instance attributes and methods

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

What’s the difference between class attributes and instance attributes?

A

Class attributes exist once and are shared by all instances, while instance attributes exist separately for each object instance

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

How do you create a new instance of a class?

A

Using MyClass(…) syntax, where … are arguments passed to __init__ (e.g., dog = Dog(‘Bello’))

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

How do you access class or object attributes?

A

Using dot notation: MyClass.attribute for class attributes, my_obj.attribute for instance attributes

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

What is inheritance in Python classes?

A

A mechanism where a class (subclass/child) can inherit attributes and methods from another class (superclass/parent)

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

How do you create a subclass in Python?

A

Using the syntax: class ChildClass(ParentClass), e.g., class GuardDog(Dog)

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

What is the purpose of the super() function?

A

It allows access to methods from the parent class, particularly useful in method overriding

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

What is Method Resolution Order (MRO)?

A

The order in which Python searches for methods through the class hierarchy, typically bottom-to-top in single inheritance

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

What is the purpose of isinstance(x

A

y)?

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

What is the purpose of issubclass(x

A

y)?

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

What is the base class for all Python classes?

A

The ‘object’ class - every class in Python is indirectly derived from it

17
Q

How do you call a method on an object?

A

Using dot notation: my_obj.method(), where self is automatically passed as the first argument

18
Q

What is method overriding?

A

When a subclass provides a different implementation of a method that exists in its parent class

19
Q

What is multiple inheritance?

A

When a class can inherit from multiple parent classes (supported in Python)

20
Q

What does type(x) return?

A

Returns the class/type of x