Programming Right From the Start with Visual Basic.NET CHAPTER 8 Controlling Execution True-False Questions 1. The statement clause follows ‘If’ and the condition clause follows ‘Then’. Answer: False Level: Easy Section: 8-1 Page: 176 2. Every ‘Else’ statement has a condition clause after it. Answer: False Section: 8-1 Level: Easy Page: 176 3. A single line ‘If’ statement does not require the End If keywords. Answer: True Level: Easy Section: 8-1 Page: 177 4. An If…Then statement can be used to determine which button a user pressed in a message box. Answer: True Level: Easy Section: 8-1 Page: 178 5. There is a limit of two ElseIf statements in a multi-line If…Then construct. Answer: False Level: Moderate Section: 8-1 Page: 177 6. The Case Else is required in the Select Case statement. Answer: False Level: Easy Section: 8-2 Page: 180 7. Both the Case and Case Else are optional in the Select Case statement. Answer: True Level: Moderate Section: 8-2 Page: 179 8. After a block of statements is executed, the Select Case statement ends. Answer: True Level: Easy Section: 8-2 Page: 180 9. A range of values can be placed in the test value clause. Answer: True Level: Easy Section: 8-2 Page: 180 10. A semi-colon (;) is used to place multiple statements on a single line. Answer: False Level: Moderate Section: 8-2 Page: 180 8-1 Chapter 8 – Controlling Execution 11. In a Do…Loop a block of statements will be repeated an indeterminate number of times. Answer: True Level: Easy Section: 8-3 Page: 181 12. Every Do…Loop must have a condition expression. Answer: True Section: 8-3 Level: Easy Page: 181 13. If a While condition evaluates to true the loop will continue. Answer: True Level: Easy Section: 8-3 Page: 182 14. If an Until condition evaluates to true the loop will continue. Answer: False Level: Easy Section: 8-3 Page: 182 15. There are some situations where a While condition will work and the Until condition can’t be used. Answer: False Level: Moderate Section: 8-3 Page: 182 16. If you want the block of statements to run at least once, use either Do While or Loop While. Answer: False Level: Moderate Section: 8-3 Page: 182 17. The For…Next statement is used to select from two or more blocks of statements. Answer: False Level: Easy Section: 8-4 Page: 184 18. Any For…Next statement can be re-written as a Do…Loop. Answer: True Level: Moderate Section: 8-4 Page: 185 19. The ‘To end’ part of a For…Next statement is optional. Answer: False Level: Easy Section: 8-4 Page: 184 20. Negative values are not allowed for the stepvalue. Answer: False Section: 8-4 Level: Moderate Page: 184 21. The For…Next loop exits when the loop variable exceeds the end value. Answer: True Level: Moderate Section: 8-4 Page: 184 22. A For…Next loop can never result in an infinite loop. Answer: False Section: 8-4 8-2 Level: Hard Page: 185 Chapter 8 – Controlling Execution 23. The Exit Sub can be used to stop processing when invalid data is entered. Answer: True Level: Easy Section: 8-5 Page: 188 24. Do Exit is used to end processing inside a loop. Answer: False Section: 8-5 Level: Easy Page: 187 25. An Exit statement inside a nested control statement will cause execution to continue immediately after the nested control statement. Answer: False Level: Moderate Section: 8-5 Page: 187 26. Sentinel values are used to validate numeric input. Answer: False Section: 8-5 Level: Moderate Page: 187 Exit If will stop execution in an If…Then block. Answer: False Section: 8-5 Level: Hard Page: 187 Visual Studio will automatically fix syntax errors. Answer: False Section: 8-6 Level: Easy Page: 192 27. 28. 29. If an exception is not handled the program will end immediately. Answer: True Level: Easy Section: 8-6 Page: 190 30. There are two types of errors: syntax and logic. Answer: False Section: 8-6 Level: Easy Page: 190 31. The type of exception being caught must be defined for every Catch statement. Answer: False Level: Hard Section: 8-6 Page: 191 32. The Try…Catch statement is good for catching logic errors. Answer: False Level: Moderate Section: 8-6 Page: 191 33. Logic errors are the hardest types to find. Answer: True Section: 8-6 34. Level: Easy Page: 192 A CheckBox CheckedState property has two possible states: True and False. Answer: False Level: Moderate Section: 8-7 Page: 193 8-3 Chapter 8 – Controlling Execution 35. The CheckBox can be changed to look like a button. Answer: True Section: 8-7 Level: Easy Page: 193 36. There can be no more than three CheckBox controls on a form or in a group box. Answer: False Level: Moderate Section: 8-7 Page: 193 37. A CheckBox can have only two states. Answer: False Section: 8-7 Level: Easy Page: 193 38. The state on one CheckBox control is independent of other CheckBox controls. Answer: True Level: Moderate Section: 8-7 Page: 193 39. The state on one RadioButton control is independent of other RadioButton controls. Answer: False Level: Moderate Section: 8-8 Page: 195 40. A form, group box, or panel control can each hold a unique group of RadioButtons. Answer: True Level: Moderate Section: 8-8 Page: 195 41. Both the RadioButton and CheckBox controls have a CheckChanged event. Answer: True Level: Moderate Section: 8-8 Page: 195 42. Only the CheckChanged Event for the RadioButton being selected is called, not the CheckChanged Event for the RadioButton automatically de-selected. Answer: False Level: Moderate Section: 8-8 Page: 195 43. Only one RadioButton in a group can be selected at a time. Answer: True Level: Easy Section: 8-8 Page: 195 8-4 Chapter 8 – Controlling Execution Multiple Choice Questions 44. When a condition in an If…Then statements tests true: a.) the next Else statement is activated. b.) the next If statement is activated. c.) the next Then statement is activated. d.) the End If statement is activated. e.) a condition can never test true. Answer: c Section: 8-1 45. Level: Moderate Page: 176 The End If statement is required: a.) in all If…Then statements. b.) in all Multi-line statements with Else. c.) in Single Line statements. d.) Both a and b. e.) All of the above. Answer: b Section: 8-1 46. Level: Moderate Page: 177 Which statements are optional in an If…Then statement? a.) If b.) Then c.) Else d.) Both a and b. e.) All of the above. Answer: c Section: 8-1 47. Level: Easy Page: 177 Which selection process is an example of multiple branches from a single expression? a.) If…Then b.) Select Case c.) Do…Loop d.) For…Next e.) All of the above. Answer: b Section: 8-2 Level: Hard Page: 179 8-5 Chapter 8 – Controlling Execution 48. How many times is the test expression of a Select Case evaluated? a.) 0 b.) 1 c.) 2 d.) Once for each Case. e.) It depends on the value of the test expression. Answer: b Section: 8-2 49. Level: Moderate Page: 180 Which is not a type of Select Case test construct? a.) simple value b.) complex value c.) relational value with Is d.) range of values with To e.) All of the above are types of test constructs. Answer: b Section: 8-2 50. Level: Hard Page: 180 What happens in a Select Case construct when a test value matches the test expression? a.) The corresponding block of statements is run. b.) The next Case test value is checked. c.) The Case Else statement is run. d.) The Select Case construct is exited. e.) An error is generated. Answer: a Section: 8-2 51. Level: Moderate Page: 180 Do...Loop is an iterative statement because it: a.) selects a block of statements to run. b.) runs the same block of statements repeatedly. c.) selects a block of statements and runs it repeatedly. d.) selects a block of statements and runs it a specified number of times. e.) All of the above. Answer: b Section: 8-3 52. Level: Hard Page: 181 Which is true of a Do…Loop? a.) The While condition goes after the Do keyword. b.) The Until condition goes after the Do keyword. c.) The While condition goes after the Loop keyword. d.) The Until condition goes after the Loop keyword. e.) All of the above. Answer: e Section: 8-3 Level: Easy Page: 181 8-6 Chapter 8 – Controlling Execution 53. Which Do…Loop statement should be used to process test scores where a test score over 100 is a signal to stop the processing? a.) Do While Score > 100 b.) Do Until Score > 100 c.) Loop While Score > 100 d.) Loop Until Score > 100 e.) All of the above are valid for this situation. Answer: b Section: 8-3 54. Level: Hard Page: 182 In the For…Next statement the default value for the Step is: a.) -1 b.) 0 c.) 1 d.) 2 e.) There is no default for the step value. Answer: c Section: 8-4 55. Level: Easy Page: 184 The For…Next Loop is used when: a.) a choice is made based on a Boolean condition. b.) a block of statements is executed an unknown number of times. c.) a block of statements is executed a known number of times. d.) Both a and b. e.) All of the above. Answer: c Section: 8-4 56. Level: Easy Page: 184 The advantage of For…Next loops over Do…Loops is that they are: a.) easier to read and maintain. b.) less prone to being infinite loops. c.) good for working with arrays. d.) Both a and b. e.) All of the above. Answer: e Section: 8-4 57. Level: Moderate Page: 185 Which is not a valid Exit statement? a.) Exit Do b.) Exit For c.) Exit Form d.) Exit Select e.) Exit Sub Answer: c Section: 8-5 Level: Moderate Page: 187 8-7 Chapter 8 – Controlling Execution 58. A sentinel value: a.) is used to prevent infinite loops. b.) must be a negative value. c.) signals the end of a list of data. d.) Both a and b. e.) All of the above. Answer: c Section: 8-5 59. Level: Easy Page: 187 Which function should be used to validate that input is not a string before performing arithmetic operations? a.) IsArithmetic b.) IsNotString c.) IsNumeric d.) IsString e.) IsValue Answer: c Section: 8-5 60. Level: Moderate Page: 189 Which is not a type of error programmers look for? a.) Logic b.) Runtime c.) Superficial d.) Syntax e.) All are errors programmers look for. Answer: c Section: 8-6 61. Level: Easy Page: 190 Which action will raise an exception? a.) Dividing by zero. b.) Assigning the string “Hi” to an integer variable. c.) Accessing an empty CD drive. d.) Both a and b. e.) All of the above. Answer: e Section: 8-6 Level: Easy Page: 190 8-8 Chapter 8 – Controlling Execution 62. An Exception is another name for a: a.) compile error. b.) logic error. c.) runtime error. d.) superficial error. e.) syntax error. Answer: c Section: 8-6 Level: Moderate Page: 190 63. What is the most number of states a CheckBox can have? a.) 0 b.) 1 c.) 2 d.) 3 e.) 4 Answer: d Level: Moderate Section: 8-7 Page: 193 64. What is the standard prefix for the name of a CheckBox? a.) chb b.) chk c.) ckb d.) ckx e.) cbx Answer: b Section: 8-7 65. Level: Easy Page: 193 A CheckBox can also appear as a(n): a.) button. b.) RadioButton. c.) ScrollBar. d.) Both a and b. e.) All of the above. Answer: a Section: 8-7 66. Level: Easy Page: 195 What is the standard prefix for the name of a RadioButton? a.) rad b.) rab c.) rdo d.) rdb e.) rbt Answer: a Section: 8-8 Level: Easy Page: 195 8-9 Chapter 8 – Controlling Execution 67. How many RadioButtons in a Group Box can be selected at the same time? a.) 0 b.) 1 c.) 2 d.) 3 e.) 4 Answer: b Section: 8-8 68. Level: Easy Page: 195 Which event is activated when a RadioButton is selected? a.) Checked b.) CheckedChanged c.) Selected d.) SelectedChanged e.) SelectionChanged Answer: b Section: 8-8 Level: Moderate Page: 196 Fill in the Blank Questions 69. Else statements are run when the previous ___condition___ is false. Level: Moderate Section: 8-1 Page: 177 70. A(n) ___nested if ___ statement has an If statement embedded inside another If statement. Level: Moderate Section: 8-1 Page: 177 71. The ElseIf statement is followed by a condition, the ___Else___ statement is not. Level: Moderate Section: 8-1 Page: 177 72. The ___Is___ keyword is used with a relational test clause of a Select Case statement. Level: Moderate Section: 8-2 Page: 180 73. When no test value of a Select Case statement matches the text expression the ___Case Else___ block of statements are run. Level: Moderate Section: 8-2 Page: 180 74. Every Case in the Select Case statement is followed by a ___test value clause___ Level: Hard Section: 8-2 Page: 179 8 - 10 Chapter 8 – Controlling Execution 75. By using a(n) ___comma___ multiple test constructs can be placed after a single Case. Level: Moderate Section: 8-2 Page: 180 76. For a Do…Loop the condition should follow the ___Do___ keyword if the block of statements should not be executed in some situations. Level: Moderate Section: 8-3 Page: 182 77. The ___While___ condition continues processing the loop if it evaluates true. Level: Easy Section: 8-3 Page: 182 78. The ___Until___ condition continues processing the loop if it evaluates false. Level: Easy Section: 8-3 Page: 182 79. In a For…Next Statement the ___loop variable___ is automatically changed each time the loop is executed. Level: Moderate Section: 8-4 Page: 184 80. The ___For…Next___ statement is used when the loop is executed a fixed number of times. Level: Easy Section: 8-4 Page: 184 81. It is optional to put the loopvariable after the ___Next___ statement. Level: Moderate Section: 8-4 Page: 184 82. In the For…Next statement the step value default is ___1___. Level: Easy Section: 8-4 Page: 184 83. The ___Exit Select__ statement is used to immediately stop execution in a Select Case statement. Level: Easy Section: 8-5 Page: 187 84. The Exit Sub statement can be used to stop execution before arithmetic operations are applied to ___invalid (or string)___ data. Level: Moderate Section: 8-5 Page: 188 85. The ___sentinel___ value is used to signal that there is no more data to process. Level: Easy Section: 8-5 Page: 187 8 - 11 Chapter 8 – Controlling Execution 86. The ___Try…Catch___ statement is used to prevent exceptional situations from crashing the program. Level: Moderate Section: 8-6 Page: 191 87. Exceptions are examples of ___Runtime___ errors. Level: Easy Section: 8-6 Page: 190 88. Misspelling Dim as Din will cause a(n) ___Syntax (or compile)___ error. Level: Easy Section: 8-6 Page: 190 89. The hardest type of error to find is typically a(n) ___Logic___ error. Level: Easy Section: 8-6 Page: 191 90. The three letter prefix for a CheckBox control is ___chk___. Level: Easy Section: 8-7 Page: 193 91. The ___ThreeState___ property of a CheckBox will make it have a third type of Checked property. Level: Hard Section: 8-7 Page: 193 92. The ___Appearance___ property of a CheckBox will make it look like a button. Level: Easy Section: 8-7 Page: 193 93. The three letter prefix for a RadioButton control is ___rad___. Level: Easy Section: 8-8 Page: 195 94. The RadioButton is best used when a user’s group of choices is ___mutually exclusive___. Level: Hard Section: 8-8 Page: 195 95. To work together a group of RadioButtons must be in a single ___container___. Level: Moderate Section: 8-8 Page: 195 8 - 12 Chapter 8 – Controlling Execution Essay Questions 96. What are the three types of errors that can occur in a computer program? Describe each. A syntax (or compiler) error occurs when the syntax rules of the language are violated. This can include misspelled keywords, misplaced keywords, violations data type conversions rules, variable declaration rules, etc. The program cannot be run until all syntax errors are fixed, but the compiler identifies all of these errors. Visual Studio puts wavy lines under each instance of a syntax error and puts the error message in a ToolTip, making it easier to find and fix. These are the easiest errors to find. A runtime error occurs when the processing of data or external elements violates rules of the programming language. Examples include dividing by zero, applying arithmetic operations to strings, performing an operation on the wrong data type, accessing a floppy disk drive when no disk is in the drive, or trying to access a database on another computer when the network is down. These errors occur because of factors outside of the program (the data input, drives, and networks) the errors can not be detected as it is being written. The developer must anticipate that these errors will occur and use the Try…Catch statement to trap and handle these errors. Without the Try…Catch statement these errors will cause the program to crash, and this must not be allowed to happen. A logic error occurs when there are no syntax or runtime errors but the program does not work as specified because of inaccurate implementation of the code. One example of this is if the programmer intends to add two variables together but uses a multiplication symbol (Sum = VarA * VarB) instead of the addition symbol (Sum = VarA + VarB). Syntactically both statements are accurate, but the multiplication version is the wrong logic needed to solve the problem. Logic errors are only detected with accurate, thorough testing or extensive use of the software. The tendency to trust the output of machines can lead users to accept inaccurate output. The users must realize what the answer should be and compare this result with the actual output to detect an error. 97. (a) Write a For loop that will get 10 numbers and then display the average in a message box. Make sure the input is valid and include variable declarations. Dim Sum As Double = 0D Dim Count, Index As Integer Dim Input As String For Index = 1 To 10 Input = InputBox("Enter a Number") If IsNumeric(Input) Then Sum = Sum + CDbl(Input) Count = Count + 1 End If Next MsgBox("The Average is: " & (Sum / Count)) 8 - 13 Chapter 8 – Controlling Execution (b) Write a Do loop that will get 10 numbers and then display the average in a message box. Make sure the input is valid and include variable declarations. Dim Sum As Double = 0D Dim Count As Integer = 0I Dim Input As String Do Input = InputBox("Enter a Number") If IsNumeric(Input) Then Sum = Sum + CDbl(Input) Count = Count + 1 End If Loop While Count < 10 MsgBox("The Average is: " & (Sum / Count)) (c) What is the advantage of the Do Loop solution? The Do Loop is better because the For loop will not have 10 values if invalid data is entered. The Do Loop will run until the correct data is entered, the For loop will only run 10 times, whether the data is good or bad. 8 - 14