Chapter 10 - Classes and Object-Oriented Programming Starting Out with Python, 3e

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What two questions should you ask to determine a class's responsibilities?

1) What must the class know? 2) What must the class do?

What is an accessor method?

A method that returns a value from a class's attribute but does not change it

What is a mutator method?

A method that stores a value in a data attribute or changes the value of a data attribute in some other way

What is a problem domain?

A written description of the real-world objects, parties, and major events related to the problem

When designing an object-oriented application, who should write a description of the problem domain?

If you adequately understand the nature of the problem you are trying to solve, you can write a description of the problem domain yourself. If you do nor thoroughly understand the nature of the problem, you should have an expert write the description for you.

The typical UML diagram for a class has three sections. What appears in these three sections?

The top section is where you write the name of the class. The middle section holds a list of the class's fields. The bottom section holds a list of the class's methods.

The _______ method is automatically called when an object is created. a. init b. _ _init_ _ c. _ _str_ _ d. _ _object_ _

init

An object is a(n) _______. a. blueprint b. cookie cutter c. variable d. instance

instance

A(n) _______ method stores a value in a data attribute or changes its value in some other way. a. modifier b. constructor c. mutator d. accessor

mutator

In one approach to identifying the classes in a problem, the programmer identifies the _______ in a description of the problem domain. a. verbs b. adjectives c. adverbs d. nouns

nouns

The programming practice is centered on creating objects. a. object-centric b. objective c. procedural d. object-oriented

object-oriented

The programming practice is centered on creating functions that are separate from the data that they work on. a. modular b. procedural c. functional d. object-oriented

procedural

In one approach to identifying a class's data attributes and methods, the programmer identifies the class's ______. a. responsibilities b. name c. synonyms d. nouns

responsibilities

A set of standard diagrams for graphically depicting object-oriented systems is provided by _____. a. the Unified Modeling Language b. flowcharts c. pseudocode d. the Object Hierarchy System

the Unified Modeling Language

When the _ _init_ _ method executes, what does the self parameter reference?

the selfparameter is automatically set to the object that was just created

What is an instance attribute?

An attribute that belongs to a specifjc instance of a class

What is an object?

An object is a software entity that contains both data and procedures.

How do you call the _ _str_ _ method?

By passing the object to the built-in str method

In a Python class, how do you hide an attribute from code outside the class?

By starting the attribute's name with two underscores

What is encapsulation?

Encapsulation is the combining of data and code into a single object.

A class method does not have to have a self parameter. T or F

False

It is a common practice in object-oriented programming to make all of a class's data attributes accessible to statements outside the class. T or F

False

One way to find the classes needed for an object-oriented program is to identify all of the verbs in a description of the problem domain. T or F

False

How do you identify the potential classes in a problem domain description?

First, identify the nouns, pronouns, and pronoun phrases in the problem domain description. Then, refine the list to eliminate duplicates, items that you do not need to be concerned with in the problem, items that represent objects instead of classes, and items that represent simple values that can be stored in variables.

What is the purpose of the _ _str_ _ method?

It returns a string representation of the object.

What is the purpose of the _ _init_ _ method? When does it execute?

Its purpose is to initialize an object's data attributes. It executes immediately after the object is created.

Will all of a class's actions always be directly mentioned in the problem domain description?

No, not always

In this chapter, we use the metaphor of a cookie cutter and cookies that are made from the cookie cuter to describe classes and objects. In this metaphor, are objects the cookie cutter, or the cookies?

Objects are the cookies.

What are private methods?

Private methods cannot be accessed by entities outside the object. They are designed to be accessed internally.

What are public methods?

Public methods can be accessed by entities outside the object.

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

The main difference between a class and an instance of a class is that a class provides a blueprint to the instances for working but is not a real entity. an instance of the class is a real entity which actually uses its definitions and works

You hear someone make the following comment: "A blueprint is a design for a house. A carpenter can use the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the same blueprint." Think of this as a metaphor for classes and objects. Does the blueprint represent a class, or does it represent an object?

The metaphor of a blueprint represents a class.

What are a class's responsibilities?

The things that the class is responsible for knowing and the actions that the class is responsible for doing

A(n) _______ is a component of a class that references data. a. method b. instance c. data attribute d. module

data attribute

Why should an object's data attributes be hidden from code outside the class?

To secure the data from outside access and unwanted modifications.

Object reusability has been a factor in the increased use of object-oriented programming. T or F

True

Starting an attribute name with two underscores will hide the attribute from code outside the class. T or F

True

The practice of procedural programming is centered on the creation of objects. T or F

True

You cannot directly call the _ _str_ _ method. T or F

True

What is the purpose of the self parameter in a method?

When a method executes, it must have a way of knowing which object's data attributes it is supposed to operate on. That's where the self parameter comes in. When a method is called, Python automatically makes its self parameter reference the specific object that the method is supposed to operate on.

Why is an object's internal data usually hidden from outside code?

When an object's internal data is hidden from outside code and access to that data is restricted to the object's methods, the data is protected from accidental corruption. In addition, the programming code outside the object does not need to know about the format or internal structure of the object's data.

A(n) method gets the value of a data attribute but does not change it. a. retriever b. constructor c. mutator d. accessor

accessor

By doing this you can hide a class's attribute from code outside the class. a. avoid using the self parameter to create the attribute b. begin the attribute's name with two underscores c. begin the name of the attribute with private _ _ d. begin the name of the attribute with the @ symbol

begin the attribute's name with two underscores

If a class has a method named _ _str_ _, which of these is a way to call the method? a. you call it like any other method: object.._ _str_ _( ) b. by passing an instance of the class to the built in str function c. the method is automatically called when the object is created d. by passing an instance of the class to the built-in state function

by passing an instance of the class to the built in str function


Kaugnay na mga set ng pag-aaral

Mental Health Chpt. 17 Mood Disorders and Suicide 1-4

View Set

Rat Dissection - arteries functions

View Set

Musculoskeletal Exam 1 (ROM, DTR testing)

View Set

Module 1b - Principles and Concepts

View Set

Chapter 7. Variable Costing and Segment Reporting: Tools for Management

View Set

EAQ Schizophrenia spectrum disorders

View Set

direct democracy vs indirect democracy

View Set