Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON Homework 4 Could you please help me answer these questions? 1.) What is t

ID: 3681861 • Letter: P

Question

PYTHON Homework 4 Could you please help me answer these questions?

1.) What is the name of the method in the following code segment?

class Fruit :

    def getColor(self) :

        return self._color

a.) _color

b.) Fruit

c.) getColor

d.) self

2.) Consider the following code segment.. ____ is a class variable.

class Fruit :

    _type = "Fruit"

    def __init__(self, color) :

        self._color = color

a.) color

b.) _color

c.) self

d.) _type     

3.) Which of the following is NOT a difference between methods and functions?

a.) A method is defined as part of a class definition

b.) A function is defined as part of a class definition

c.) The first parameter variable of a method is called self

d.) A method can access the instance variables of an object

4.) Python's ____ statement allows an exception to be caught and the program to recover.

a.) try-catch

b.) try-except

c.) assert

d.) except

5.) Naming conventions for Python dictate that instance variables should start with an underscore to represent ____.

a.) public visibility

b.) private visibility

c.) encapsulation

d.) public interface

6.) Python includes a module that allows the programmer to save and load objects using a process called ____.

a.) serial-construcdt

b.) streamize

c.) pickling

d.) converting

7.) Which of the following method headers represent a constructor for an Employee class, assuming we add a colon to each?

a.) def Employee(self)

b.) def __init()__

c.) def __Employee__(self)

d.) def __init__(self)

8.) What is the purpose of an object's instance variables?

a.) Store the data temporarily until a method completes.

b.) Store the data for each individual instance created by a class

c.) To provide access to the data of an object

d.) To create variables with public visibility

9.) use objectName = pickle.load(file_variable) to read an object from the file.

a.) objectName = pickle.load(file_variable)

b.) pickle.dump(objectName, file_Variable)

10.) Given the following class definition, what is printed after executing this code snippet?

class PhoneNumber :

    def __init__(self, lName, phone = "215-555-1212") :

        self._name = lName

            self._phone = phone  

Jones = PhoneNumber("Jones")

print(Jones._name, Jones._phone)

a.) Jones 215-555-1212

b.) IOError

c.) 'phoneNumber' object has no attribute

d.) Jones

e.) Jones 000-000-0000

11.) Most classes include a special method named ____, which is the class's constructor.

a.) _self

b.) __self__

c.) _init

d.) __init__

12.) Methods that allow a user to modify an object's state are called ____.

a.) mutators

b.) modifiers

c.) writers

d.) output methods                    

13.) Which of the following is NOT true about instance methods for a class?

a.) The object on which a method is applied is automatically passed to the self parameter variable of the method

b.) In a method, you access instance variables through the self parameter variable

c.) A class variable belongs to the class, not to any instance of the class

d.) The accessor and mutator methods are automatically called when an object is created

14.) Consider the following class, Which method is an accessor?

class Counter :

    def getValue(self) :

        return self._value

    def click(self) :

        self._value = self._value + 1

    def unClick(self) :

        self._value = self._value - 1

    def reset(self) :

        self._value = 0

a.) getValue

b.) click

c.) unClick

d.) reset                

15.) How do you access instance variables in a method?

a.) Using the constructor

b.) Using a setter variable

c.) Using a default value

d.) Using the self reference

16.) use ____ to store an object to a file.

a.) objectName = pickle.load(file_variable)

b.) pickle.dump(objectName, file_Variable)

17.) What is the name of the instance variable in the following code segment?

class Fruit :

    def getColor(self) :

        return self._color

a.) _color

b.) Fruit

c.) getColor

d.) self

18.) How many constructors can be defined for each class?

a.) Constructors are not used in Python

b.) Only one may be defined

c.) At least one, but as many as needed

d.) At least one, but no more than five

19.) What is the purpose of the self parameter?

a.) Enables changes in the implementation without affecting users of a class

b.) To create variables with public visibility

c.) Store the data for all objects created by the same class

d.) Refers to the object on which the method was invoked

20.) What is the definition of a class?

a.) A class is exactly the same thing as a function

b.) A class decomposes tasks into functions

c.) A class describes a set of objects with the different behaviors

d.) A class describes a set of objects with the same behavior

21.) The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.

a.) true

b.) false

22.) A(n) ____ class includes data and methods common to its subclasses, but is never instantiated.

a.) polymorphic

b.) abstract

c.) encapsulated

d.) anonymous

23.) Python's ____ function is used to determine whether or not two objects are of the same type.

a.) is

b.) type

c.) in

d.) eq

24.) What method name is used for a constructor?

a.) __begin__

b.) __construct__

c.) __create__

d.) __init__               

25.) When pickling several different objects in the same file, when you retrieve the items you need to load them in the same order they were dumped in.

a.) true

b.) false

26.) Consider the following class. Which method(s) are mutators?

class Counter :

    def getValue(self) :

        return self._value

    def click(self) :

        self._value = self._value + 1

    def unClick(self) :

        self._value = self._value - 1

    def reset(self) :

        self._value = 0

a.) Only reset

b.) Only click and unClick

c.) Only click, unClick and reset

d.) All four methods are mutators

27.) Which of the following is NOT true about constructors?

a.) A constructor initializes the instance variables of an object

b.) The constructor is automatically called when an object is created

c.) The constructor is defined using the special method name __default__

d.) Default arguments can be used with a constructor to provide different ways of creating an object

28.) When is a constructor invoked?

a.) When an object is created

b.) When instance data is modified

c.) When a method is invoked

d.) When multiple arguments are required to create an object

29.) Before invoking a method on an object, the object must have first been instantiated.

a.) true

b.) false

30.) Assume a class exists named Fruit. Which of the follow statements constructs an object of the Fruit class?

a.) def Fruit()

b.) class Fruit()

c.) x = Fruit()

d.) x = Fruit.create()

Explanation / Answer

1.) What is the name of the method in the following code segment?

class Fruit :

    def getColor(self) :

        return self._color

a.) _color

b.) Fruit

c.) getColor

d.) self

2.) Consider the following code segment.. ____ is a class variable.

class Fruit :

    _type = "Fruit"

    def __init__(self, color) :

        self._color = color

a.) color

b.) _color

c.) self

d.) _type     

3.) Which of the following is NOT a difference between methods and functions?

a.) A method is defined as part of a class definition

b.) A function is defined as part of a class definition

c.) The first parameter variable of a method is called self

d.) A method can access the instance variables of an object

4.) Python's ____ statement allows an exception to be caught and the program to recover.

a.) try-catch

b.) try-except

c.) assert

d.) except

5.) Naming conventions for Python dictate that instance variables should start with an underscore to represent ____.

a.) public visibility

b.) private visibility

c.) encapsulation

d.) public interface

6.) Python includes a module that allows the programmer to save and load objects using a process called ____.

a.) serial-construcdt

b.) streamize

c.) pickling

d.) converting

7.) Which of the following method headers represent a constructor for an Employee class, assuming we add a colon to each?

a.) def Employee(self)

b.) def __init()__

c.) def __Employee__(self)

d.) def __init__(self)

8.) What is the purpose of an object's instance variables?

a.) Store the data temporarily until a method completes.

b.) Store the data for each individual instance created by a class

c.) To provide access to the data of an object

d.) To create variables with public visibility

9.) use objectName = pickle.load(file_variable) to read an object from the file.

a.) objectName = pickle.load(file_variable)

b.) pickle.dump(objectName, file_Variable)

10.) Given the following class definition, what is printed after executing this code snippet?

class PhoneNumber :

    def __init__(self, lName, phone = "215-555-1212") :

        self._name = lName

            self._phone = phone  

Jones = PhoneNumber("Jones")

print(Jones._name, Jones._phone)

a.) Jones 215-555-1212

b.) IOError

c.) 'phoneNumber' object has no attribute

d.) Jones

e.) Jones 000-000-0000