Back to all Computer Science topics
Topic 7.1Computer Science SL25 flashcards

OOP: a single class

Practice Flashcards

Flip cards to reveal answers
Card 1 of 257.1.1
7.1.1
Question

What is object-oriented programming?

Click to reveal answer

Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.

All Flashcards in Topic 7.1

Below are all 25 flashcards for this topic. Sign up free to track your progress and get personalized review schedules.

7.1.15 cards

Card 1definition
Question

What is object-oriented programming?

Answer

An approach that keeps a thing's data and the operations on it in one place. A book's title and its borrow() method live together, rather than the data in one structure and the code elsewhere.

Card 2comparison
Question

What is the difference between a class and an object?

Answer

A class is the template, saying what every one of this kind will have and can do; it holds no values itself. An object is one actual thing made from that class, with its own values.

Card 3comparison
Question

What is the difference between inheritance and polymorphism?

Answer

Inheritance is reusing what a more general class already has — Ebook takes Book's title and author. Polymorphism is the same method call answered appropriately by each kind. Inheritance often makes polymorphism possible, but they are different ideas.

Card 4concept
Question

Give two advantages and two disadvantages of OOP.

Answer

Advantages: it models real things with data and behaviour, and inheritance gives reuse with no copied code. Disadvantages: the class structure must be designed before coding, and it is more code than a small task needs.

Card 5concept
Question

When is OOP not worth using?

Answer

For a short script with no real entities to model — reading a file and printing a total gains nothing from classes while still paying the design and code overhead.

7.1.25 cards

Card 6process
Question

How do you find the classes and methods in a description?

Answer

Underline the nouns as candidates for classes and attributes, and the verbs as candidates for methods. It is crude but gets a first draft right surprisingly often.

Card 7definition
Question

What are the three parts of a UML class box?

Answer

The name at the top, the attributes in the middle with their types, and the methods at the bottom with their return types — always in that order.

Card 8definition
Question

What do -, + and underlining mean in UML?

Answer

A minus sign means private, reachable only inside the class. A plus sign means public. An underlined entry is static, belonging to the class itself rather than to each object.

Card 9concept
Question

Where should a method be placed?

Answer

In the class that holds the data it uses. borrow() belongs on Book because Book holds onLoan; putting it elsewhere would force that attribute to be made public, losing encapsulation.

Card 10concept
Question

When is inheritance the wrong choice?

Answer

When the relationship is really 'has a' rather than 'is a'. A Library has Books but is not a Book, so it should hold a collection rather than inherit.

7.1.35 cards

Card 11comparison
Question

What is the difference between a static and a non-static variable?

Answer

A non-static (instance) variable has one copy per object, holding what makes that object different. A static (class) variable has exactly one copy belonging to the class itself, shared by every object.

Card 12concept
Question

Why must a counter of objects created be static?

Answer

Because it is a fact about the class, not about any one object. As an instance variable, every object would hold its own count of 1 rather than a shared running total.

Card 13comparison
Question

What is the difference between a static and an instance method?

Answer

An instance method uses the object's own data and cannot be called without an object. A static method belongs to the class, uses no object's data, and can be called as Book.count() with no object at all.

Card 14concept
Question

Why can a static method not use instance variables?

Answer

It can be called with no object in existence, so there is nothing for self to refer to — no way to know which object's data was meant.

Card 15process
Question

What test decides between static and instance?

Answer

Would every object have the same value, always? Yes means static; no means it must be an instance variable. In doubt, choose instance — sharing one value is a decision, not a default.

7.1.45 cards

Card 16comparison
Question

What is the difference between defining a class and instantiating an object?

Answer

Defining writes the template once, saying what every one will have and can do. Instantiating creates an actual object from that template, with its own values, as often as needed.

Card 17definition
Question

What does a constructor do?

Answer

It runs automatically when an object is created and gives every attribute a value, so no object ever exists in an unfinished state. It can also refuse invalid values before the object exists.

Card 18definition
Question

What does self refer to?

Answer

The object the method was called on. self.title is this particular book's title, which is what lets one method definition serve every object of the class.

Card 19concept
Question

Why is not every attribute a constructor parameter?

Answer

Some have a single correct starting value. A new book is never on loan, so onLoan is set to False inside the constructor rather than asked for — which removes a chance to get it wrong.

Card 20concept
Question

What happens if a constructor misses an attribute?

Answer

The object exists with a missing value, and the failure appears much later, somewhere that has nothing to do with the cause — which makes it hard to trace.

7.1.55 cards

Card 21definition
Question

What is encapsulation?

Answer

Keeping an object's data private and providing public methods as the only way to reach it, so every change goes through code that can check it first rather than any part of the program writing whatever it likes.

Card 22comparison
Question

What is the difference between encapsulation and information hiding?

Answer

Encapsulation is the mechanism — private attributes, public methods. Information hiding is the principle behind it: the outside should know what a class does, not how, so the inside can be rewritten without breaking callers.

Card 23definition
Question

What are getters and setters?

Answer

A getter returns a private value; a setter changes one after checking it. Providing a getter but no setter makes a value readable from outside but not changeable.

Card 24concept
Question

Why is a setter with no checks not really encapsulation?

Answer

The data is then effectively public with extra typing. The value of encapsulation lies in the checking that happens on the way in, not in the wrapping itself.

Card 25concept
Question

How does a private attribute help when a value is wrong?

Answer

The fault must be inside that class, since nothing outside could have written it. With a public attribute, any line of the whole program could be responsible — the same argument as preferring local variables to global ones.

Want smart review reminders?

Sign up free to track your progress. Our spaced repetition algorithm will tell you exactly which cards to review and when.

Start Free