Chapter 9 Procedures and Arrays

Chapter 9 Procedures and Arrays

<p> Programming Right From the Start with Visual Basic.NET</p><p>CHAPTER 9</p><p>Procedures and Arrays</p><p>True-False Questions</p><p>1. Some Visual Basic code is written inside procedures. Answer: False Level: Easy Section: 9-1 Page: 212 </p><p>2. Procedures make it possible to create modular programming. Answer: True Level: Easy Section: 9-1 Page: 212</p><p>3. Once a procedure is called during the lifetime of the program, it can not be called again. Answer: False Level: Easy Section: 9-1 Page: 212</p><p>4. The three types of procedures are episode, function, and sub. Answer: False Level: Easy Section: 9-1 Page: 212</p><p>5. A parameter is used to send information into a procedure, and an argument is used to carry information out of a procedure. Answer: False Level: Moderate Section: 9-1 Page: 212</p><p>6. The private keyword at the beginning of the procedure declaration will restrict access to the procedure. Answer: True Level: Moderate Section: 9-2 Page: 213</p><p>7. Event procedures cannot be declared with the Public keyword for accessibility. Answer: False Level: Hard Section: 9-2 Page: 213</p><p>8. The Handles keyword is optional in an event procedure. Answer: False Level: Moderate Section: 9-2 Page: 213</p><p>9. Every event procedure has two parameters. Answer: True Level: Easy Section: 9-2 Page: 213</p><p>10. The a parameter in an event procedure holds the arguments associated with that event. Answer: False Level: Moderate Section: 9-2 Page: 213</p><p>9 - 1 Chapter 9 – Procedures and Arrays</p><p>11. The sender parameter in an event procedure holds a reference to the object that raised the event. Answer: True Level: Moderate Section: 9-2 Page: 213</p><p>12. The default is for parameters to be sent as ByRef. Answer: False Level: Moderate Section: 9-2 Page: 213</p><p>13. To create an event procedure you must type the procedure declaration in the code editor. Answer: False Level: Easy Section: 9-2 Page: 213</p><p>14. Double clicking on a Button control in the Designer window will automatically create a click event procedure stub in the Code Editor window. Answer: True Level: Moderate Section: 9-2 Page: 213</p><p>15. Some sub procedures cannot be called multiple times. Answer: False Level: Easy Section: 9-3 Page: 213</p><p>16. One important purpose of a sub procedure is to make the code easier to understand. Answer: True Level: Easy Section: 9-3 Page: 213</p><p>17. When calling a sub procedure, parentheses are optional if there are no arguments. Answer: False Level: Easy Section: 9-3 Page: 215</p><p>18. The Handles keyword is optional in a sub procedure. Answer: False Level: Hard Section: 9-3 Page: 215</p><p>19. Parameters are optional in a sub procedure. Answer: True Level: Easy Section: 9-3 Page: 215</p><p>20. A function must have an assigned data type. Answer: True Level: Easy Section: 9-4 Page: 216</p><p>21. A function procedure must have one or more parameters. Answer: False Level: Easy Section: 9-4 Page: 216</p><p>9 - 2 Chapter 9 – Procedures and Arrays</p><p>22. A function procedure must have a Return statement. Answer: False Level: Hard Section: 9-4 Page: 216</p><p>23. Only one Return statement is allowed in a function procedure. Answer: False Level: Moderate Section: 9-4 Page: 216</p><p>24. A function can act like an expression in an assignment statement. Answer: True Level: Moderate Section: 9-4 Page: 216</p><p>25. Modular programming aims to decrease coupling and increase cohesion. Answer: True Level: Hard Section: 9-4 Page: 217</p><p>26. A function procedure is identical to a sub procedure. Answer: False Level: Easy Section: 9-4 Page: 217</p><p>27. Local variables are declared in the General Declaration Section of the program. Answer: False Level: Easy Section: 9-5 Page: 218</p><p>28. Module variables can be accessed from any procedure in a module. Answer: True Level: Easy Section: 9-5 Page: 218</p><p>29. The scope of a variable means how long the variable lasts in memory. Answer: False Level: Moderate Section: 9-5 Page: 218</p><p>30. It is best to declare all variables as module because then any procedure can access any variable. Answer: False Level: Moderate Section: 9-5 Page: 218</p><p>31. A local variable is recreated anew every time its procedure is activated. Answer: True Level: Moderate Section: 9-5 Page: 218</p><p>32. A module variable will not work unless its name starts with the letter m as a prefix. Answer: False Level: Moderate Section: 9-5 Page: 218</p><p>33. The index for the first element of an array is either 0 or 1. Answer: False Level: Easy Section: 9-6 Page: 219</p><p>9 - 3 Chapter 9 – Procedures and Arrays</p><p>34. An array consists of multiple elements. Answer: True Level: Easy Section: 9-6 Page: 219</p><p>35. The upper bound of an array equals the length of the array. Answer: False Level: Easy Section: 9-6 Page: 219</p><p>36. When declaring an array, the upper bound of an array must be included. Answer: False Level: Moderate Section: 9-6 Page: 220</p><p>37. When an array is made larger, the Preserve statement will prevent the values in the data elements from being lost. Answer: True Level: Easy Section: 9-6 Page: 222</p><p>38. A structure can hold multiple values of a single datatype. Answer: False Level: Easy Section: 9-7 Page: 226</p><p>39. A member of a structure is the same as a variable declared in a structure. Answer: True Level: Moderate Section: 9-7 Page: 226</p><p>40. A period is used to reference the name of a variable in a structure. Answer: True Level: Easy Section: 9-7 Page: 226</p><p>41. An array cannot hold a structure datatype. Answer: False Level: Easy Section: 9-7 Page: 226</p><p>42. An array can hold controls as their datatype. Answer: True Level: Easy Section: 9-8 Page: 228</p><p>43. A special DimControl statement is needed to create an array of controls. Answer: False Level: Easy Section: 9-8 Page: 228</p><p>44. The only way to create an array of controls is to assign existing controls to the array during the Form Load event. Answer: False Level: Moderate Section: 9-8 Page: 228</p><p>45. The Tag property is only associated with label and textbox controls. Answer: False Level: Moderate Section: 9-8 Page: 228</p><p>9 - 4 Chapter 9 – Procedures and Arrays</p><p>46. The Tag property is used to hold any kind of information needed in the program. Answer: True Level: Moderate Section: 9-8 Page: 228</p><p>47. Pressing the Shift, Control, or Alt key by itself will not generate a KeyPress event. Answer: True Level: Moderate Section: 9-9 Page: 233</p><p>48. Arrow keys do not generate a KeyPress event, but they do generate a KeyDown event. Answer: False Level: Moderate Section: 9-9 Page: 233</p><p>49. The KeyChar property in a KeyPress event can distinguish between an upper and lower case letter. Answer: True Level: Easy Section: 9-9 Page: 233</p><p>50. If the Handled Property in a KeyPress event is set to True, the letter typed will not appear in the textbox. Answer: True Level: Easy Section: 9-9 Page: 233</p><p>Multiple Choice Questions</p><p>51. Which is a type of procedure found in VB.Net? a.) Event b.) Function c.) Sub d.) Both a and b. e.) All of the above. </p><p>Answer: e Level: Easy Section: 9-1 Page: 212</p><p>52. The methodology where code is broken into small, logical procedures is called: a.) event-driven programming. b.) functional programming. c.) granular programming. d.) modular programming. e.) procedural programming.</p><p>Answer: d Level: Hard Section: 9-1 Page: 212</p><p>9 - 5 Chapter 9 – Procedures and Arrays</p><p>53. When using a procedure the calling code sends data via the: a.) actual argument to the formal parameter of the procedure. b.) formal argument to the actual parameter of the procedure. c.) actual parameter to the formal argument of the procedure. d.) formal parameter to the actual argument of the procedure. e.) All of the above.</p><p>Answer: a Level: Hard Section: 9-1 Page: 212</p><p>54. From how many places in the code can a procedure be called? a.) 0 b.) 1 c.) 2 d.) 3 e.) As many times as needed.</p><p>Answer: e Level: Easy Section: 9-1 Page: 212</p><p>55. Which parameter is found in an event procedure? a.) e b.) Sender c.) Receiver d.) Both a and b. e.) All of the above. </p><p>Answer: e Level: Moderate Section: 9-2 Page: 213</p><p>56. Which is an optional element of an event procedure? a.) End Sub b.) Handles c.) Object_Event d.) Statements e.) Sub</p><p>Answer: d Level: Moderate Section: 9-2 Page: 213</p><p>57. What happens when a parameter in a procedure is declared ByVal? a.) Only arguments of numeric data types are allowed. b.) A reference to the argument is sent to the procedure. c.) A copy of the argument is sent to the procedure. d.) Both a and b. e.) All of the above. </p><p>Answer: c Level: Easy Section: 9-2 Page: 213</p><p>9 - 6 Chapter 9 – Procedures and Arrays</p><p>58. Which is a valid way to write the procedure stub for an object’s default event? a.) Use the Class and Method combo boxes in the Code Editor window. b.) Double click on the object in the Form Designer window. c.) Type the procedure declaration in the Code Editor window. d.) Both a and b. e.) All of the above. </p><p>Answer: e Level: Moderate Section: 9-2 Page: 214</p><p>59. A sub procedure is valuable because it: a.) makes code easier to maintain. b.) splits the logic to solve a problem into small, manageable units. c.) limits the number of times the code can be accessed. d.) Both a and b. e.) All of the above. </p><p>Answer: d Level: Moderate Section: 9-3 Page: 214</p><p>60. Which is not an optional element of a sub procedure declaration? a.) Parameters b.) Public c.) Private d.) Statements e.) Sub</p><p>Answer: e Level: Moderate Section: 9-3 Page: 215</p><p>61. Which is a valid way to write a sub procedure declaration? a.) Use the Class and Method combo boxes in the Code Editor window. b.) Double click on the object in the Form Designer window. c.) Type the procedure declaration in the Code Editor window. d.) Both a and b. e.) All of the above. </p><p>Answer: c Level: Moderate Section: 9-3 Page: 215</p><p>9 - 7 Chapter 9 – Procedures and Arrays</p><p>62. Which statement will send the value generated by a function procedure, called CalculateTax, back to the calling code? a.) Return Sales*0.08 b.) CalculateTax = Sales*0.08 c.) Return CalculateTax (Sales*0.08) d.) Both a and b. e.) All of the above. </p><p>Answer: d Level: Moderate Section: 9-4 Page: 217</p><p>63. Which part of a function procedure declaration statement is optional? a.) Datatype b.) Function c.) Parameters d.) Private e.) ProcedureName</p><p>Answer: c Level: Hard Section: 9-4 Page: 217</p><p>64. How many return statements are allowed in a Function Procedure? a.) 0 b.) 1 c.) 2 d.) 3 e.) There is no limit.</p><p>Answer: e Level: Easy Section: 9-4 Page: 217</p><p>65. Why should a variable not be declared as a module variable? a.) It prevents a procedure from being self contained. b.) It makes it easier to document the code. c.) Local variable names can be reused in other procedures. d.) Both a and b. e.) All of the above. </p><p>Answer: e Level: Moderate Section: 9-5 Page: 219</p><p>66. Which variable name uses a standard naming convention for module variables? a.) mWeight b.) mdWeight c.) modWeight d.) moduleWeight e.) module_Weight</p><p>Answer: a Level: Easy Section: 9-5 Page: 218</p><p>9 - 8 Chapter 9 – Procedures and Arrays</p><p>67. The scope of a variable refers to: a.) the length of the variable. b.) the name of the variable. c.) the accessibility of the variable. d.) the datatype of the variable. e.) the lifetime of the variable.</p><p>Answer: c Level: Moderate Section: 9-5 Page: 218</p><p>68. What is the value of the index for the first element in a VB.NET array? a.) 0 b.) 1 c.) 2 d.) 3 e.) Depends on what the assigned value is.</p><p>Answer: a Level: Moderate Section: 9-6 Page: 219</p><p>69. Which method will return the number of elements in an array? a.) Dimension b.) Length c.) Number d.) Size e.) UpperBound</p><p>Answer: b Level: Moderate Section: 9-6 Page: 219</p><p>70. In the statement, Dim Days(7) as String, what part of the array does the number 7 refer to? a.) Array name b.) Datatype c.) Lowerbound d.) Upperbound e.) Size</p><p>Answer: d Level: Moderate Section: 9-6 Page: 220</p><p>71. What is required to reference an element in an array? a.) Array name b.) Index value of the element c.) Element value d.) Both a and b. e.) All of the above. </p><p>Answer: d Level: Moderate Section: 9-6 Page: 220</p><p>9 - 9 Chapter 9 – Procedures and Arrays</p><p>72. Which method will arrange the elements of an array in alphabetical order? a.) Arrange b.) Assemble c.) Order d.) Rank e.) Sort</p><p>Answer: e Level: Easy Section: 9-6 Page: 222</p><p>73. The number of variables allowed in a structured is: a.) 0 b.) 1 c.) 2 d.) 3 e.) Any number of variables can be declared in an array.</p><p>Answer: e Level: Easy Section: 9-7 Page: 226</p><p>74. The variable inside a structure is called a(n): a.) associate. b.) constituent. c.) element. d.) member. e.) part.</p><p>Answer: d Level: Moderate Section: 9-7 Page: 226</p><p>75. Which datatype can an array not hold? a.) TextBoxes b.) Labels c.) Structures d.) Controls e.) An array can hold all of the above. </p><p>Answer: e Level: Easy Section: 9-8 Page: 228</p><p>76. An array of controls can be populated by: a.) assigning existing controls to the array. b.) creating controls and assigning them to the array. c.) borrowing controls that will automatically assign them to the array. d.) Both a and b. e.) All of the above. </p><p>Answer: d Level: Moderate Section: 9-8 Page: 228</p><p>9 - 10 Chapter 9 – Procedures and Arrays</p><p>77. The Tag property can: a.) only hold string values. b.) only hold integer values. c.) only hold Boolean values. d.) only hold controls. e.) hold any data defined by the programmer.</p><p>Answer: e Level: Hard Section: 9-8 Page: 228</p><p>78. The KeyPress event will capture pressing the key: a.) A. b.) shift. c.) control. d.) Both a and b. e.) All of the above. </p><p>Answer: a Level: Easy Section: 9-9 Page: 233</p><p>79. Which argument in the KeyPress parameter list contains the Handled property? a.) Sender b.) e c.) Object d.) KeyPressEventArgs e.) None of the above.</p><p>Answer: b Level: Hard Section: 9-9 Page: 233</p><p>Fill in the Blank Questions</p><p>80. The advantage of ___modular___ programming is that it is easier to maintain. Level: Moderate Section: 9-1 Page: 212</p><p>81. The calling code uses a(n) ___argument___ to send information to a procedure. Level: Moderate Section: 9-1 Page: 212</p><p>82. A procedure has ___parameters___ that act like variables to hold data from the calling code. Level: Moderate Section: 9-1 Page: 212</p><p>83. The three types of procedures in VB.Net are ___events___, ___subs___, and ___functions___. Level: Easy Section: 9-1 Page: 212</p><p>9 - 11 Chapter 9 – Procedures and Arrays</p><p>84. Another term for an event procedure is ___event handler___. Level: Easy Section: 9-2 Page: 213</p><p>85. A procedure stub must be declared as having either ___public___ or ___private___ accessibility. Level: Moderate Section: 9-2 Page: 213</p><p>86. The Handles keyword is required in the declaration of a(n) ___event___ procedure. Level: Easy Section: 9-2 Page: 213</p><p>87. The ___sender___ parameter of an event procedure represents the object that generated the event. Level: Moderate Section: 9-2 Page: 213</p><p>88. The ___e___ parameter of an event procedure represents the parameters unique to that type of event. Level: Moderate Section: 9-2 Page: 213</p><p>89. The ___ByVal___ keyword will cause a copy of the argument to be sent to a procedure. Level: Easy Section: 9-2 Page: 213</p><p>90. The ___ByRef___ keyword will cause a pointer to the argument to be sent to a procedure. Level: Easy Section: 9-2 Page: 213</p><p>91. When a parameter is declared as ___ByRef ___ the argument sent to the procedure can be changed by the code inside the procedure. Level: Moderate Section: 9-2 Page: 213</p><p>92. The ___click___ event procedure stub is created when a button on a form in the Designer window is double clicked. Level: Moderate Section: 9-2 Page: 214</p><p>93. The ___End Sub___ statement will cause the control of the program to exit the sub procedure and return to the calling procedure. Level: Moderate Section: 9-3 Page: 215</p><p>94. One advantage of a sub procedure is that it can be called ___multiple___ times. Level: Moderate Section: 9-3 Page: 215</p><p>9 - 12 Chapter 9 – Procedures and Arrays</p><p>95. Another name for a sub procedure is ___subroutine___. Level: Easy Section: 9-3 Page: 214</p><p>96. A function procedure is also called a ___function___. Level: Easy Section: 9-4 Page: 216</p><p>97. The ___return___ statement sends the function’s value back to the code that called the function. Level: Moderate Section: 9-4 Page: 216</p><p>98. The amount of interdependency between procedures is called ___coupling___. Level: Hard Section: 9-4 Page: 217</p><p>99. The amount of uniformity within a procedure is called ___cohesion___. Level: Hard Section: 9-4 Page: 217</p><p>100. The difference between a sub and function procedure is that a function will ___return a value___. Level: Easy Section: 9-4 Page: 216</p><p>101. A ___local___ variable is declared in a procedure. Level: Easy Section: 9-5 Page: 218</p><p>102. A ___module___ variable is declared in the General Declaration Section. Level: Easy Section: 9-5 Page: 218</p><p>103. The ___scope___ of a variable indicates which parts of the code can reference the variable. Level: Moderate Section: 9-5 Page: 218</p><p>104. A ___local___ variable is erased from memory each time an End Sub statement is run. Level: Moderate Section: 9-5 Page: 218</p><p>105. The values of an array are stored in its ___elements___. Level: Easy Section: 9-6 Page: 219</p><p>106. Another name for an index is ___subscript___. Level: Moderate Section: 9-6 Page: 219</p><p>9 - 13 Chapter 9 – Procedures and Arrays</p><p>107. The index of the last element in an array is the ___upper bound___. Level: Moderate Section: 9-6 Page: 219</p><p>108. The statement that changes the size of an array is ___ReDim___. Level: Easy Section: 9-6 Page: 222</p><p>109. An array of ___structures___ can hold different data types. Level: Moderate Section: 9-7 Page: 226</p><p>110. Another name for the variables in a structure is a(n) ___member___. Level: Moderate Section: 9-7 Page: 226</p><p>111. An array that contains checkboxes, groupboxes, and labels is called a(n) ___control___ array. Level: Easy Section: 9-8 Page: 228</p><p>112. The ___Tag___ property of a control can contain integers, strings, or Boolean values. Level: Moderate Section: 9-8 Page: 228</p><p>113. The ___KeyDown___ Event can capture when the user presses the alt key in a textbox. Level: Moderate Section: 9-9 Page: 233</p><p>114. The ___KeyPress___ Event cannot capture when the user presses the alt key in a textbox. Level: Moderate Section: 9-9 Page: 233</p><p>115. The ____e____ argument in the KeyPress event contains the KeyChar property. Level: Moderate Section: 9-9 Page: 233</p><p>116. The ___Handled___ property, when set to True, will stop further processing of a key press. Level: Moderate Section: 9-9 Page: 233</p><p>9 - 14 Chapter 9 – Procedures and Arrays</p><p>Essay Questions</p><p>117. Describe three ways to create the declaration for an Event Procedure? Which is the best method and why?</p><p>In the Form Design window double-clicking on the object will cause the IDE to automatically write the event procedure stub for the default event of an object.</p><p>The programmer can type the event procedure stub into the Code Editor window.</p><p>In the Code Editor window, the programmer can first select the object from the Class combo box (at the top of the Code Editor window) and then select the Event from the Method combo box. This will cause the IDE to automatically write the event procedure stub for that object.</p><p>The easiest method is the double-click method, but this only works for one event in each object.</p><p>The combo box method can be used for any event and any object. Its advantage is that the procedure stub is created automatically.</p><p>The typing method should not be used because it is prone to operator error. The benefit of using the VB.Net IDE is that it can automatically and accurately create code. This is why VB.Net is a popular language for Rapid Application Development (RAD). Typing in code by hand defeats the benefit of having automatic code generation capabilities.</p><p>118. a) Create a structure named Employee that contains three variables: EmployeeID (Integer), LastName (String), FirstName (String), and Salary (Decimal). Then create a module level array that holds ten employees.</p><p>Structure Employee Dim EmployeeID As Integer Dim LastName As String Dim FirstName As String Dim Salary As Decimal End Structure</p><p>Dim mEmployees(9) As Employee </p><p> b) Create a Sub Procedure named InitializeID that uses a loop to put the values 1 to 10 in the EmployeeID for the employee's array.</p><p>Private Sub InitializeID() Dim I As Integer</p><p>For I = 0 To UBound(mEmployees) mEmployees(I).EmployeeID = I + 1 Next I</p><p>End Sub</p><p>9 - 15</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    15 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us