Introduction to Programming Using Python, Y. Daniel Liang

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 7 Objects and Classes


Section 7.2 Defining Classes for Objects
7.1  __________ represents an entity in the real world that can be distinctly identified.
A. A class
B. An object
C. A method
D. A data field

7.2  _______ is a template, blueprint, or contract that defines objects of the same type.
A. A class
B. An object
C. A method
D. A data field

7.3  An object is an instance of a __________.
A. program
B. class
C. method
D. data

7.4  The keyword __________ is required to define a class.
A. def
B. return
C. class
D. All of the above.

7.5  ________ is used to create an object.
A. A constructor
B. A class
C. A value-returning method
D. A None method

7.6  The ________ creates an object in the memory and invokes __________.
A. the __init__ method
B. the init method
C. the initialize method
D. the __str__ method

7.7  Analyze the following code:

 class A:
     def __init__(self, s):
         self.s = s
 
     def print(self):
         print(s)

 a = A("Welcome")
 a.print()
A. The program has an error because class A does not have a constructor.
B. The program has an error because class A should have a print method with signature print(self, s).
C. The program has an error because class A should have a print method with signature print(s).
D. The program would run if you change print(s) to print(self.s).

7.8  Analyze the following code:

 class A:
     def __init__(self, s):
         self.s = s
 
     def print(self):
         print(self.s)

 a = A()
 a.print()
A. The program has an error because class A does not have a constructor.
B. The program has an error because s is not defined in print(s).
C. The program runs fine and prints nothing.
D. The program has an error because the constructor is invoked without an argument.

7.9  Analyze the following code:

 class A:
     def __init__(self, s = "Welcome"):
         self.s = s
 
     def print(self):
         print(self.s)

 a = A()
 a.print()
A. The program has an error because class A does not have a constructor.
B. The program has an error because s is not defined in print(s).
C. The program runs fine and prints nothing.
D. The program has an error because the constructor is invoked without an argument.
E. The program runs fine and prints Welcome.

7.10  Given the declaration x = Circle(), which of the following statement is most accurate.
A. x contains an int value.
B. x contains an object of the Circle type.
C. x contains a reference to a Circle object.
D. You can assign an int value to x.

Section 7.4 Hiding Data Fields
7.11  Analyze the following code:

class A:
    def __init__(self):
        self.x = 1
        self.__y = 1
 
    def getY(self):
        return self.__y

a = A()
print(a.x)
A. The program has an error because x is private and cannot be access outside of the class.
B. The program has an error because y is private and cannot be access outside of the class.
C. The program has an error because you cannot name a variable using __y.
D. The program runs fine and prints 1.
E. The program runs fine and prints 0.

7.12  Analyze the following code:

class A:
    def __init__(self):
        self.x = 1
        self.__y = 1
 
    def getY(self):
        return self.__y

a = A()
print(a.__y)
A. The program has an error because x is private and cannot be access outside of the class.
B. The program has an error because y is private and cannot be access outside of the class.
C. The program has an error because you cannot name a variable using __y.
D. The program runs fine and prints 1.
E. The program runs fine and prints 0.

7.13  Analyze the following code:

 class A:
     def __init__(self):
         self.x = 1
         self.__y = 1
 
     def getY(self):
         return self.__y

 a = A()
 a.x = 45
 print(a.x)
A. The program has an error because x is private and cannot be access outside of the class.
B. The program has an error because y is private and cannot be access outside of the class.
C. The program has an error because you cannot name a variable using __y.
D. The program runs fine and prints 1.
E. The program runs fine and prints 45.

7.14  In the following code,

 def A:
 def __init__(self):
     __a = 1
     self.__b = 1
     self.__c__ = 1
     __d__ = 1

 # Other methods omitted

Which of the following is a private data field?
A. __a
B. __b
C. __c__
D. __d__

7.15  Analyze the following code:

 class A:
     def __init__(self):
         self.x = 1
         self.__y = 1
 
     def getY(self):
         return self.__y

 a = A()
 a.__y = 45
 print(a.getX())
A. The program has an error because x is private and cannot be access outside of the class.
B. The program has an error because y is private and cannot be access outside of the class.
C. The program has an error because you cannot name a variable using __y.
D. The program runs fine and prints 1.
E. The program runs fine and prints 45.

7.16  Which of the following statement is most accurate?

A. A reference variable is an object.
B. A reference variable refers to an object.
C. An object may contain other objects.
D. An object may contain the references of other objects.

7.17  What is the value of times displayed?

def main():
    myCount = Count()
    times = 0

    for i in range(0, 100):
        increment(myCount, times)

    print("myCount.count =", myCount.count, "times =", times)

def increment(c, times):
    c.count += 1
    times += 1

class Count:
    def __init__(self):
        self.count = 0
    
main()
A. count is 101 times is 0
B. count is 100 times is 0
C. count is 100 times is 100
D. count is 101 times is 101