Object Orientated Programming Flashcards

1
Q

Write code to define a ‘Track’ Class which contains the title, artist and musical of the song
It also has a method of display the details for the track

A
class Track():
def \_\_init\_\_(self, title="",artist="",musical=""):
self.title = title
self.artist = artist
self.musical = musical
def showTrack(self):
print(self.title, self.artist, self.musical)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Make an instance of the ‘Track’ Class which has a title, artist and musical instance variables

A

track1 = Track(“You’ll See”, “Adam Pascal”, 2.2)

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

How do you access methods?

A

Dot notation

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

Write the init subroutine in a Playlist class that contains

  • An array of tracks
  • A max length
  • A next track value
A
Class Playlist():
def \_\_init\_\_(self,maxLength):
self.maxLength = maxLength
self.tracks = [""]*maxLength
self.nextTrack = 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write an add track function on a Playlist class where newTrack is passed in and the instance variables in the class are self.maxLength, self.tracks, self.nextTrack

A
def addTrack(self,newTrack):
if self.nextTrack == self.maxLength:
print("Playlist is full")
else:
self.tracks[self.nextTrack] = newTrack
self.nextTrack = self.nextTrack + 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a delete track function on a Playlist where index in passed in and the instance variables in the class are self.maxLength, self.tracks, self.nextTrack

A
def deleteTrack(self,index):
if index > self.maxLength or index < 0:
print("Invalid index")
else:
for item in range(index, self.nextTrack-1):
self.tracks[item] = self.tracks[item+1]
end for
self.nextTrack = self.nextTrack - 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write the generic code for Inheritance

A
class SubClass(SuperClass):
def \_\_init\_\_(self, attribute1, attribute2, attribute3):
SuperClass.\_\_init\_\_(self,attribute1, attribute2)
self.attribute3 = attribute3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write code to define a generic class with two attributes

A
class Name:
def \_\_init\_\_(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
Create an instance of this class called Pupil 1 with an age of 17 and a name of Heather
class Pupil:
def \_\_init\_\_(self, age, name):
self.age = age
self.name = name
A

pupil1 = Pupil(17,’Heather’)

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

What is a class?

A

Blueprint of an object, definitions for the data format and avaliable procedures for a given type of object

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

What is an object?

A

An instance of a class

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

What is Instantiation?

A

The creation of an object from a class

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

What is Inheritance?

A

Sharing of characteristics between a super class and a newly created subclass

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

What are the benefits of inheritance?(1)

A

Saves development time, testing already complete, saves lines of code

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

Describe Polymorphism (2)

A
A method in a sub class can be written to overwrite the method inherited by the superclass
The method called is the one in the subclass instead of the superclass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe Encapsulation and give a benefit of encapsulation (2)

A

Restricting how an object’s data can be accessed

Safer, prevents unintended changing of data