OOP Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Class

A

Defines a type of object using OOP.

each object is an instance of the class

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

Attribute

A

a value within an OOP class

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

OOP Method

A

procedures or functions written within an OOP class

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

OOP Object

A

an independent entity
OOP defines the attributes of an object
OOP defines the methods that can be applied to the object
many instances of the object can be declared

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

Constructor in OOP

A

takes place immediatelly after defining the class .
-sets attributes
- gives them starting values
- provides parameters

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

write a constructor in python
(with two attributes called att1 and att2, set to the parameters p1 and p2)

A

def __init__(self, p1, p2):
self.att1=p1
self.att2=p2

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

advantages of OOP

A

easier to read code

less work in long run

better control over the process

fewer erros

good for team work

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

disadvantages of OOP

A

needs careful planning

more work at the start

objects must be similar

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

class Car():
def__init__(self, p1, p2):
self.colour = p1
self.make = p2
self.speed = 0

create an instance of the car class called mycar that is a pink miata

A

mycar = Car(“Pink”,”Miata”)

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

imagine this method is written within an OOP class:

def gofaster(self):
self.speed = self.speed + 10

in the main program, write a line of code to use this method on mycar

A

mycar.gofaster()

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

rewrite this constructor in psuedocode:

class Car():
def__init__(self, p1, p2):
self.colour = p1
self.make = p2
self.speed = 0

A

class Car
procedure new(p1, p2)
colour = p1
make = p2
speed = 0
end procedure
end class

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

inheritance

A

one class can “inherit” all the features of another class.

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

show the first line of a class definition of a class Y that inherits class X

A

Class Y inherits X

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

what do we call the class that inherits / the class it inherits from

A

super class and sub class

or

parent class and child class

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

encapsulation

A

using private methods and attribites in OOP

  • they cannot be altered directly, only accessed via “get” and “set” methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

get and set methods

A

if an attribute is private we cant show or change it in the main program, so get and set methods are used instead

17
Q

write a get method for the attribute colour in pseudocode

A

public procedure getcolour()
return colour
end procedure

18
Q

write a set method for the attribute colour in psuedocode, using p1 as the parameter

A

public procedure setcolour(p1)
colour = p1
end procedure

19
Q

What is inheritance in OOP

A

A sub class can inherit attributes and methods from an existing super - class. The subclass is then defined by making alterations or additions to the super- class.

20
Q

(psuedocode) here is a superclass.

class Employee
public procedure new(surname, jobtitle):
surname = surname
jobtitle = jobtitle
end procedure
endclass

now write a subclass for manager with their name and title, explaining which line inherits what.

A

-class manager inherits Employee
- public procedure new (thisname, thistitle)
- super.new( thisname, thistitle)

  • end procedure
    -endclass

the first line inherits all the methods of the superclass

the third line inherits all attributes from the super class

21
Q

here is the pseudocode for a sub- class .

class manager inherits Employee
public procedure new (thisname, thistitle)
super.new( thisname, thistitle)

end procedure
endclass

add a line to this constructor, that will add a new attribute called “manages” and set the variable to 0 .

A

class manager inherits Employee
public procedure new (thisname, thistitle)
super.new( thisname, thistitle)
manages = 0

end procedure
endclass

22
Q

(psuedocode) here is a class defenition. Now write it in python

class Employee
public procedure new(surname, jobtitle):
surname = surname
jobtitle = jobtitle
end procedure
endclass

A

Class Employee():
def __init__(self, surname, jobtitle)
self.surname = surname
self.jobtitle = jobtitile

23
Q

here is a superclass written in python.

Class Employee():
def __init__(self, surname, jobtitle)
self.surname = surname
self.jobtitle = jobtitile

now write a subclass for manager with their name and title

A

class Manager (Employee):
def __init__(self, thisname, thistitle)
super(). __init__ (thisname, thistitle)
end procedure
endclass

24
Q

in python, write the opening line for a class constructor called car.

then, write the opening line for a class constructor called car which inherits the class vehicle

A

class Car():

class Car(Vehicle):

25
Q

in psuedocode, write the opening line for a class constructor called car.

then, write the opening line for a class constructor called car which inherits the class vehicle

A

class Car

class Car inherits Vehicle

26
Q

list 4 advantages of inheritance

A
  • reduces the amount of work
  • fewer new things to learn
  • reduces risk of errors
  • tried and tested methods
27
Q

How could inheritance be used for logins

A

The admin login class could inherit attributes of the normal login class, with extra security checks.

This reduces the amount of work the programmer has to do and reduces the risk of errors. As methods are reused

28
Q

when using inheritance, what are the 3 different options for a constructor ( the part of the definition which defines and sets attributes)

A

No constructor - use attributes of superclass

Inherit constructor - Add new attributes to build on superclass

Make a new constructor

29
Q

what is the line to inherit a constructor in python. Imagine that the attributes being inherited are colour and size

A

super(). __init__ (colour,size)

30
Q

when attributes use encapsulation, what does this mean

A

they have been made private

they cannot be shown in the main program

they cannot be changed in the main program

31
Q

is encapsulation a feature in python

A

no

32
Q

what , other tha attributes, can have encapsulation used on them

A

methods

33
Q

how to tell if encapsulation has been used in psuedocode

A

write “public” or “private” before it.

eg.
public procedure new (…)

34
Q

in pseudocode, if i wanted to make one of the attributes in a class definition private, how would i do it?

write an example of a class called shape with the colour being private

A

the same way you call global variables at the start of a module

class Shape
private colour
…. (rest of constructor)

35
Q

in the main program, i want to set the boolean attribute “verified” on user01 to true. However, it is encapsulated and needs to be accessed using a set method.

Write the single line of code

A

instead of using
user01.verified = True

use
user01.setverified(True)

36
Q

what does polymorphism mean

A

the same command has different effects with different data types or classes

eg. py + symbol is different for numerical and string variables

37
Q

how does polymorphism apply to OOP

A

A method defined for one class of object cant be used with another

A method in a different class might have the same name but do a different thing

38
Q

imagine a geometry program with classes called “square”, “triangle” and “circle”.

All of them have a method “calculate_area”

explain why this is an example of polymorphism

A

the method will work in different ways for the different classes - it will be defined with the correct formula in each class.