Programming Right From the Start with Visual Basic.NET CHAPTER 14 Advanced Object-Oriented Programming True-False Questions 1. 2. 3. 4. 5. Objects have properties. Answer: True Section: 14-1 Level: Easy Page: 390 Properties have methods. Answer: False Section: 14-1 Level: Easy Page: 390 All Controls are objects. Answer: True Section: 14-1 Level: Easy Page: 390 All Objects are controls. Answer: False Section: 14-1 Level: Easy Page: 390 Classes and objects are really the same thing. Answer: False Section: 14-1 Level: Moderate Page: 391 6. Actions and data that are grouped together are called objects. Answer: True Level: Easy Section: 14-1 Page: 391 7. The design of an object is called a class. Answer: True Section: 14-1 Level: Moderate Page: 391 A property is the action associated with an object. Answer: False Section: 14-1 Level: Moderate Page: 391 One class can create many objects. Answer: True Section: 14-1 Level: Moderate Page: 391 A class is an instance of an object. Answer: False Section: 14-1 Level: Hard Page: 391 8. 9. 10. 14 - 1 Chapter 14 – Advanced Object-Oriented Programming 11. Object oriented programming is considered just one way to deal with the inherent complexity of software development. Answer: True Level: Moderate Section: 14-2 Page: 391 12. The prefix for a class is T, which stands for text. Answer: False Section: 14-2 Level: Moderate Page: 392 13. One advantage of encapsulation is that it protects data in an object from external code. Answer: True Level: Moderate Section: 14-2 Page: 392 14. To ensure proper encapsulation internal variables are declared as public. Answer: False Level: Moderate Section: 14-2 Page: 392 15. Always use properties to encapsulate data. Answer: True Section: 14-2 Level: Moderate Page: 392 16. Only some properties and methods are inherited from a base class. Answer: False Level: Moderate Section: 14-2 Page: 392 17. Inheritance makes it easy to reuse existing code in new applications. Answer: True Level: Easy Section: 14-2 Page: 392 18. Making a change in a base class will always cause the derived class to change. Answer: True Level: Easy Section: 14-2 Page: 392 19. New properties can not be added to a derived class. Answer: False Section: 14-2 Level: Moderate Page: 392 20. Polymorphism means that one method can have many names. Answer: False Level: Hard Section: 14-2 Page: 393 21. The fact that multiplication can apply to a variety of data types is an example of polymorphism. Answer: True Level: Moderate Section: 14-2 Page: 393 22. The two parts of a class definition are properties and methods. Answer: False Level: Moderate Section: 14-3 Page: 393 14 - 2 Chapter 14 – Advanced Object-Oriented Programming 23. A property is a privately defined variable in a class. Answer: False Section: 14-3 Level: Moderate Page: 393 24. A class method can be either a function procedure or a sub procedure. Answer: True Level: Moderate Section: 14-3 Page: 393 25. When a constructor is called, it means a new object is created. Answer: True Level: Easy Section: 14-3 Page: 393 26. It is best to define a field as a private variable. Answer: True Section: 14-3 Level: Easy Page: 394 27. A field should have a prefix of P, which stands for private. Answer: False Level: Easy Section: 14-3 Page: 394 28. The standard way to interact with a private field is to use a property. Answer: True Level: Easy Section: 14-3 Page: 395 29. The get procedure of a property is used to assign a value to field. Answer: False Level: Moderate Section: 14-3 Page: 395 30. The best way to make a property read only is to not put code in the Set procedure. Answer: False Level: Moderate Section: 14-3 Page: 395 31. A method in a class can only be defined as Public. Answer: False Section: 14-3 Level: Moderate Page: 396 A method in a class can read any field in that class. Answer: True Section: 14-3 Level: Moderate Page: 396 32. 33. Having multiple constructors in a class is an example of polymorphism. Answer: True Level: Hard Section: 14-3 Page: 397 34. A constructor is a property that is used to create a new object. Answer: False Level: Moderate Section: 14-3 Page: 397 14 - 3 Chapter 14 – Advanced Object-Oriented Programming 35. The code to call the constructor of a base class is MyBaseClass.New(). Answer: False Level: Moderate Section: 14-3 Page: 397 36. Parameters are required in a call to a constructor. Answer: False Section: 14-3 Level: Moderate Page: 397 Multiple Choice Questions 37. Which control is an example of an object in VB.NET? a.) Button b.) Label c.) Textbox d.) Both a and b. e.) All of the above. Answer: e Section: 14-1 38. Level: Easy Page: 390 Which of the following is part of an object? a.) Methods b.) Properties c.) Instances d.) Both a and b. e.) All of the above. Answer: d Section: 14-1 39. Level: Moderate Page: 390 Which is true about objects? a.) Objects are used to create classes. b.) Objects are analogous to blueprints. c.) Objects combine actions and data. d.) Both a and b. e.) All of the above. Answer: c Section: 14-1 Level: Moderate Page: 391 14 - 4 Chapter 14 – Advanced Object-Oriented Programming 40. Properties are used to represent: a.) actions. b.) classes. c.) data. d.) events. e.) instances. Answer: c Section: 14-1 41. Level: Easy Page: 391 Methods are used to represent: a.) actions. b.) classes. c.) data. d.) events. e.) instances. Answer: a Section: 14-1 42. Level: Easy Page: 391 The term instantiation refers to the creation of: a.) a class from a blueprint. b.) an object from a class. c.) a method from an object. d.) a property from a method. e.) a blueprint from a property. Answer: b Section: 14-1 43. Level: Moderate Page: 391 Anything in VB.NET that has a property or method is: a.) a class. b.) a control. c.) an object. d.) Both a and b. e.) All of the above. Answer: c Section: 14-1 44. Level: Moderate Page: 391 Which feature is needed to make a programming language object oriented? a.) Encapsulation b.) Inheritance c.) Polymorphism d.) Both a and b. e.) All of the above. Answer: e Section: 14-2 Level: Easy Page: 391 14 - 5 Chapter 14 – Advanced Object-Oriented Programming 45. We should think of the practice of object-oriented programming as: a.) a simple answer to the complex problem of creating software. b.) an incremental improvement to the problem of creating software. c.) the way to get rid of a werewolf. d.) Both a and b. e.) All of the above. Answer: b Section: 14-2 46. Level: Moderate Page: 391 Encapsulation makes it easier to: a.) reuse and modify existing modules of code. b.) write and read code by sharing method names. c.) hide and protect data from external code. d.) Both a and b. e.) All of the above. Answer: c Section: 14-2 47. Level: Moderate Page: 392 Inheritance makes it easier to: a.) reuse and modify existing modules of code. b.) write and read code by sharing method names. c.) hide and protect data from external code. d.) Both a and b. e.) All of the above. Answer: a Section: 14-2 48. Level: Moderate Page: 392 Polymorphism makes it easier to: a.) reuse and modify existing modules of code. b.) write and read code by sharing method names. c.) hide and protect data from external code. d.) Both a and b. e.) All of the above. Answer: b Section: 14-2 49. Level: Moderate Page: 393 The standard prefix to signify a class is: a.) B. b.) C. c.) L. d.) S. e.) T. Answer: e Section: 14-2 Level: Moderate Page: 392 14 - 6 Chapter 14 – Advanced Object-Oriented Programming 50. When using encapsulation how should data be shared with external code? a.) Events b.) Methods c.) Properties d.) Private variables e.) Public variables Answer: c Section: 14-2 51. Level: Moderate Page: 392 Which statement is true? a.) A base class inherits some of the properties of a derived class. b.) A base class inherits all of the properties of a derived class. c.) A derived class inherits some of the properties of a base class. d.) A derived class inherits all of the properties of a base class. e.) None of the above. Answer: d Section: 14-2 52. Level: Moderate Page: 392 When a base class is changed: a.) there is no effect on the derived class. b.) the derived class changes when the key word Overridden is used. c.) only the methods of the derived class change. d.) only the properties of the derived class change. e.) the derived class automatically changes. Answer: e Section: 14-2 53. Level: Easy Page: 392 Polymorphism can apply to: a.) math operators. b.) method names. c.) object names. d.) Both a and b. e.) All of the above. Answer: d Section: 14-2 54. Level: Moderate Page: 393 With polymorphism: a.) one method can have multiple names. b.) one object can have multiple names. c.) many methods can share the same name. d.) many objects can share the same name. e.) None of the above statements are true. Answer: c Section: 14-2 Level: Hard Page: 393 14 - 7 Chapter 14 – Advanced Object-Oriented Programming 55. Which element of a class is optional? a.) Constructs b.) Fields c.) Methods d.) Properties e.) All of the above. Answer: e Section: 14-3 56. Level: Moderate Page: 394 What is the suggested order for the definition of class elements from first to last? a.) Constructs, fields, methods, properties b.) Properties, constructs, fields, methods c.) Fields, properties, constructs, methods d.) Constructs, properties, fields, methods e.) Methods, constructs, properties, fields Answer: c Section: 14-3 57. Level: Hard Page: 394 The standard for designing a field is that it be defined as a: a.) private method. b.) public method. c.) private variable. d.) public variable. e.) None of the above. Answer: c Section: 14-3 58. Level: Moderate Page: 393 What is the syntax for making a property read-only? a.) Property Read propertyname As datatype b.) Read Property propertyname As datatype c.) ReadOnly Property propertyname As datatype d.) Read-Only Property propertyname As datatype e.) RO Property propertyname As datatype Answer: c Section: 14-3 59. Level: Moderate Page: 395 The Get procedure of a property acts like: a.) an event. b.) a function. c.) a variable. d.) Both a and b. e.) All of the above. Answer: b Section: 14-3 Level: Hard Page: 395 14 - 8 Chapter 14 – Advanced Object-Oriented Programming 60. A method in a class is: a.) a sub procedure. b.) a function. c.) an event. d.) Both a and b. e.) All of the above. Answer: d Section: 14-3 61. Level: Moderate Page: 396 How many constructors can a class have? a.) 0 b.) 1 c.) 2 d.) 3 e.) All of the above. Answer: e Section: 14-3 62. Level: Easy Page: 397 A constructor is a special type of: a.) class. b.) field. c.) method. d.) property. e.) variable. Answer: c Section: 14-3 63. Level: Easy Page: 397 Which is true for constructors in a class? a.) All constructors must have the same number of parameters. b.) All constructors must be the same parameter data type. c.) Some constructors can have the same list of parameters. d.) Only two constructors in a class can have the same list of parameters. e.) No two constructors in a class can have the same list of parameters. Answer: e Section: 14-3 64. Level: Moderate Page: 397 Which statement will call a constructor of a base class? a.) Base.New( ) b.) BaseConstructor.New ( ) c.) CallBase.New( ) d.) Constructor.New ( ) e.) MyBase.New( ) Answer: e Section: 14-3 Level: Moderate Page: 397 14 - 9 Chapter 14 – Advanced Object-Oriented Programming Fill in the Blank Questions 65. The controls on a form and the form itself are examples of ___objects___. Level: Easy Section: 14-1 Page: 390 66. The name and text of a textbox control are ___properties___ of the control. Level: Moderate Section: 14-1 Page: 390 67. The ___class___ is similar to a blueprint for the object. Level: Moderate Section: 14-1 Page: 391 68. Creating an object from a class is called ___instantiation___. Level: Hard Section: 14-1 Page: 391 69. Elements in VB.NET that have methods and properties are considered ___objects___. Level: Moderate Section: 14-1 Page: 391 70. The process of hiding the internal data and actions of an object is called ___encapsulation___. Level: Easy Section: 14-2 Page: 392 71. Objects should always use ___properties___ to interact with external code. Level: Moderate Section: 14-2 Page: 392 72. Internal methods and variables of an object should be declared ___private____. Level: Moderate Section: 14-2 Page: 392 73. Creating modified and specialized versions of an existing class is called ___inheritance___. Level: Moderate Section: 14-2 Page: 392 74. A(n) ___base___ class is used to create a derived class. Level: Moderate Section: 14-2 Page: 392 75. When a method in a derived class is modified, it is called a(n) ___overridden___ method. Level: Moderate Section: 14-2 Page: 392 76. Changes in a ___base___ class will propagate to all derived classes. Level: Moderate Section: 14-2 Page: 392 14 - 10 Chapter 14 – Advanced Object-Oriented Programming 77. The capacity to have different methods all using the same name is called ___polymorphism___. Level: Moderate Section: 14-2 Page: 393 78. A single math operator can apply to different sets of data types is an example of ___polymorphism___. Level: Moderate Section: 14-2 Page: 393 79. A private variable is a class called a ___field___. Level: Easy Section: 14-3 Page: 393 80. Making a field in a class private helps to ensure ___data hiding___. Level: Hard Section: 14-3 Page: 394 81. The ___fields___ of a class should be defined first. Level: Moderate Section: 14-3 Page: 394 82. The ___Get___ procedure of a property is used to output data from an object. Level: Hard Section: 14-3 Page: 395 83. The ___Set___ procedure of a property is used for data input into an object. Level: Hard Section: 14-3 Page: 395 84. ___Properties___ are used to access private fields in an object. Level: Moderate Section: 14-3 Page: 395 85. You can prevent external code from changing the value of a property by using the ___ReadOnly___ keyword to define the property. Level: Moderate Section: 14-3 Page: 395 86. Procedures defined in a class are called ___methods___. Level: Easy Section: 14-3 Page: 396 87. A method of an object can be either a sub or ___function___. Level: Moderate Section: 14-3 Page: 396 88. Every constructor of an object is named ___New___. Level: Moderate 14 - 11 Chapter 14 – Advanced Object-Oriented Programming Section: 14-3 89. Page: 397 A derived class uses ___MyBase.New( )___ to call the constructor of its parent class. Level: Hard Section: 14-3 Page: 397 Essay Questions 90. Describe the three main features of an object-oriented programming language. An object-oriented programming language will have encapsulation, inheritance, and polymorphism. Encapsulation is the process of making an object into a secure module with hidden data. An encapsulated object will have public properties to allow limited access to information internal to the object. Only methods needed by external code will be declared as public, all other methods will be private. This decreases the ability of external code to change the objects internal variables, either by accident or design. A standard interface for interacting with an object also means that modifications can be made to the internal workings of the object without having to make modifications to the code that uses the object. This decreases the cost of maintaining software. Inheritance is the ability to take an existing object with all of its methods and properties and create newer, improved versions by adding or overriding properties and methods. This decrease the time it takes to create new objects because existing functions that work well can be reused. A programmer can reuse the button control that already exists instead of writing a new version of the button control. They can also make a derived button class that will have unique properties designed for their application. One advantage of inheritance is that modifications in the base class will automatically propagate to all the derived classes. Polymorphism allows objects to have multiple methods that share the same name. This allows a group of related methods to perform similar functions but with different types of input. Each version of the method must have a unique parameter signature (list). One example would be multiple versions of a Print method that can print to the screen any type of variable (string, integer, decimal, …). Without polymorphism each data type would need its own print method such as Print_integer, Print_string, Print_decimal. This greatly simplifies the job of a programmer and makes the code easier to read and write. 91. What is a constructor? How is it used in a class? A constructor is a method that is called every time a new instance of an object is created. It fills the properties of an object when it is first instantiated. 14 - 12 Chapter 14 – Advanced Object-Oriented Programming All constructors for a class are named New (this is an example of polymorphism). If no specific constructor is requested, the constructor with no parameters is called, New( ). However a class can have no constructors at all. Derived classes can call the constructors of the parent or base class by using the keyword MyBase, as with the code below. MyBase.New(“Joe”, “Smith”, “777-7777”) 14 - 13