90 likes | 105 Views
This chapter covers exercises on objects and classes, data encapsulation, and inheritance in Python. Learn about creating objects, constructors, class attributes, and more.
E N D
CSCI/CMPE 4341 Topic: Programming in PythonChapter 7: Introduction to Object-Oriented Programming in Python – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu
Objectives • In this chapter, you will do some exercises about: • Objects and classes • Data encapsulation • Inheritance
Multiple Choice • Object-oriented programming encapsulates data and functions into _______. • A. functions B. attributes C. classes D. files • Method ____ is called the constructor in Python. • A. __del__() B. className() C. __init__ D. __self__ • A class can have __________ object(s). • A. only 1 B. only 2 C. between 1 and 3 D. 1 or more than 1 • Every class declaration contains keyword ____ followed immediately by the class’s name. • A. keyword B. public C. void D. class • Which of the following statements is true to create a new object of class MyClass in Python? • A. MyClass obj = new MyClass () • B. obj = new MyClass ; • C. MyClass *obj = new MyClass (); • D. obj = new MyClass ();
Multiple Choice (cont'd) • A(n) _______ is a variable shared by all objects of a class. • A. object attribute B. class attribute C. object function D. dynamic attribute • With _____, a class is derived from several base classes. • A. single inheritance B. multiple inheritance C. parent class D. data encapsulation • _____ in the first line of a class definition are used to indicate the inheritance. • A. colons B. parentheses C. classes D. square brackets
True or False Statements • Object-oriented programming languages do not use functions to perform actions. • The parameter self must be the first item in a method's argument list. • The class constructor returns an object of the class. • Constructors may specify default arguments. • An attribute that begins with a single underscore is a private attribute. • The destructor is called when the keyword del is used on an object.
True or False Statements (cont'd) • A shared class attribute should be initialized in the constructor. • When invoking an object’s method, a program does not need to pass a value that corresponds to the object reference parameter. • Every class should have a __del__ method to reclaim an object's memory. • The derived class inherits all the attributes and methods of its base class in Python.