7. Objects & Classes - 16 Questions


7. Objects & Classes

7. Objects & Classes

16 Questions
Created by Y. Daniel Liang - http://www.cs.armstrong.edu/liang/index.html
Free
Python is called an "object-oriented programming language." This means there is a construct in Python called a class that lets you structure your software in a particular way. Using classes, you can add consistency to your programs so that they can be used in a cleaner way.
  1. An object is an instance of a __________.



    program
    class
    method
    data

    View Answer | Discuss in forum
  2. __________ represents an entity in the real world that can be distinctly identified.



    A class
    An object
    A method
    A data field

    View Answer | Discuss in forum
  3.  _______ is a template, blueprint, or contract that defines objects of the same type.



    A class
    An object
    A method
    A data field

    View Answer | Discuss in forum
  4. The keyword __________ is required to define a class. 



    def
    return
    class
    All of the above.

    View Answer | Discuss in forum
  5. ________ is used to create an object.



    A constructor
    A class
    A value-returning method
    A None method

    View Answer | Discuss in forum
  6. The ________ creates an object in the memory and invokes __________.



    the __init__ method
    the init method
    the initialize method
    the __str__ method

    View Answer | Discuss in forum
  7. Analyze the following code:

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


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

    View Answer | Discuss in forum
  8. Analyze the following code:

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

     a = A()
     a.print()



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

    View Answer | Discuss in forum
  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()



    The program has an error because class A does not have a constructor.
    The program has an error because s is not defined in print(s).
    The program runs fine and prints nothing.
    The program runs fine and prints Welcome.

    View Answer | Discuss in forum
  10. Given the declaration x = Circle(), which of the following statement is most accurate.



    x contains an int value.
    x contains an object of the Circle type.
    x contains a reference to a Circle object.
    You can assign an int value to x.

    View Answer | Discuss in forum
  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)



    The program has an error because x is private and cannot be access outside of the class.
    The program has an error because y is private and cannot be access outside of the class.
    The program has an error because you cannot name a variable using __y.
    The program runs fine and prints 1.

    View Answer | Discuss in forum
  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)



    The program has an error because x is private and cannot be access outside of the class.
    The program has an error because y is private and cannot be access outside of the class.
    The program has an error because you cannot name a variable using __y.
    The program runs fine and prints 1.

    View Answer | Discuss in forum
  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)



    The program has an error because x is private and cannot be access outside of the class.
    The program runs fine and prints 45.
    The program runs fine and prints 1.
    The program has an error because you cannot name a variable using __y.

    View Answer | Discuss in forum
  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
    __b
    __c__
    __d__

    View Answer | Discuss in forum
  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())



    The program has an error because x is private and cannot be access outside of the class.
    The program has an error because y is private and cannot be access outside of the class.
    The program has an error because you cannot name a variable using __y.
    The program runs fine and prints 1.

    View Answer | Discuss in forum
  16. 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()



    count is 101 times is 0
    count is 100 times is 0
    count is 100 times is 100
    count is 101 times is 101

    View Answer | Discuss in forum