Chapter 8 – Controlling Execution

chapter 8

Controlling Execution

True-False Questions

1.The statement clause follows ‘If’ and the condition clause follows ‘Then’.

Answer: FalseLevel: Easy

Section: 8-1Page: 176

2.Every ‘Else’ statement has a condition clause after it.

Answer: FalseLevel: Easy

Section: 8-1Page:176

3.A single line ‘If’ statement does not require the End If keywords.

Answer: TrueLevel: Easy

Section: 8-1Page: 177

4.An If…Then statement can be used to determine which button a user pressed in a message box.

Answer: TrueLevel: Easy

Section: 8-1Page: 178

5.There is a limit of two ElseIf statements in a multi-line If…Then construct.

Answer: FalseLevel: Moderate

Section: 8-1Page: 177

6.The Case Else is required in the Select Case statement.

Answer: FalseLevel: Easy

Section: 8-2Page: 180

7.Both the Case and Case Else are optional in the Select Case statement.

Answer: True Level: Moderate

Section: 8-2Page: 179

8.After a block of statements is executed, the Select Case statement ends.

Answer: TrueLevel: Easy

Section: 8-2Page: 180

9.A range of values can be placed in the test value clause.

Answer: TrueLevel: Easy

Section: 8-2Page: 180

10.A semi-colon (;) is used to place multiple statements on a single line.

Answer: FalseLevel: Moderate

Section: 8-2Page: 180

11.In a Do…Loop a block of statements will be repeated an indeterminate number of times.

Answer: True Level: Easy

Section: 8-3Page: 181

12.Every Do…Loop must have a condition expression.

Answer: TrueLevel: Easy

Section: 8-3Page: 181

13.If a While condition evaluates to true the loop will continue.

Answer: TrueLevel: Easy

Section: 8-3Page: 182

14.If an Until condition evaluates to true the loop will continue.

Answer: False Level: Easy

Section: 8-3Page: 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-3Page: 182

16.If you want the block of statements to run at least once, use either Do While or Loop While.

Answer: FalseLevel: Moderate

Section: 8-3Page: 182

17.The For…Next statement is used to select from two or more blocks of statements.

Answer: False Level: Easy

Section: 8-4Page: 184

18.Any For…Next statement can be re-written as a Do…Loop.

Answer: TrueLevel: Moderate

Section: 8-4Page: 185

19.The ‘To end’ part of a For…Next statement is optional.

Answer: FalseLevel: Easy

Section: 8-4Page: 184

20.Negative values are not allowed for the stepvalue.

Answer: FalseLevel: Moderate

Section: 8-4Page: 184

21.The For…Next loop exits when the loop variable exceeds the end value.

Answer: True Level: Moderate

Section: 8-4Page: 184

22.A For…Next loop can never result in an infinite loop.

Answer: False Level: Hard

Section: 8-4Page: 185

23.The Exit Sub can be used to stop processing when invalid data is entered.

Answer: True Level: Easy

Section: 8-5Page: 188

24.Do Exit is used to end processing inside a loop.

Answer: False Level: Easy

Section: 8-5Page: 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-5Page: 187

26.Sentinel values are used to validate numeric input.

Answer: FalseLevel: Moderate

Section: 8-5Page: 187

27.Exit If will stop execution in an If…Then block.

Answer: False Level: Hard

Section: 8-5 Page: 187

28.Visual Studio will automatically fix syntax errors.

Answer: FalseLevel: Easy

Section: 8-6Page: 192

29.If an exception is not handled the program will end immediately.

Answer: TrueLevel: Easy

Section: 8-6Page: 190

30.There are two types of errors: syntax and logic.

Answer: FalseLevel: Easy

Section: 8-6Page: 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: FalseLevel: Moderate

Section: 8-6 Page: 191

33.Logic errors are the hardest types to find.

Answer: True Level: Easy

Section: 8-6Page: 192

34.A CheckBox CheckedState property has two possible states: True and False.

Answer: False Level: Moderate

Section: 8-7Page: 193

35.The CheckBox can be changed to look like a button.

Answer: True Level: Easy

Section: 8-7Page: 193

36.There can be no more than three CheckBox controls on a form or in a group box.

Answer: FalseLevel: Moderate

Section: 8-7Page: 193

37.A CheckBox can have only two states.

Answer: False Level: Easy

Section: 8-7Page: 193

38.The state on one CheckBox control is independent of other CheckBox controls.

Answer: TrueLevel: Moderate

Section: 8-7Page: 193

39.The state on one RadioButton control is independent of other RadioButton controls.

Answer: FalseLevel: Moderate

Section: 8-8Page: 195

40.A form, group box, or panel control can each hold a unique group of RadioButtons.

Answer: TrueLevel: Moderate

Section: 8-8Page: 195

41.Both the RadioButton and CheckBox controls have a CheckChanged event.

Answer: TrueLevel: Moderate

Section: 8-8Page: 195

42.Only the CheckChanged Event for the RadioButton being selected is called, not the CheckChanged Event for the RadioButton automatically de-selected.

Answer: FalseLevel: Moderate

Section: 8-8Page: 195

43.Only one RadioButton in a group can be selected at a time.

Answer: TrueLevel: Easy

Section: 8-8Page: 195

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: cLevel: Moderate

Section: 8-1Page: 176

45.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: bLevel: Moderate

Section: 8-1Page: 177

46.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: cLevel: Easy

Section: 8-1Page: 177

47.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: bLevel: Hard

Section: 8-2Page: 179

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: bLevel: Moderate

Section: 8-2Page: 180

49.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: bLevel: Hard

Section: 8-2Page: 180

50.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: aLevel: Moderate

Section: 8-2Page: 180

51.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: bLevel: Hard

Section: 8-3Page: 181

52.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: eLevel: Easy

Section: 8-3Page: 181

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: bLevel: Hard

Section: 8-3Page: 182

54.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: cLevel: Easy

Section: 8-4Page: 184

55.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: cLevel: Easy

Section: 8-4Page: 184

56.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: eLevel: Moderate

Section: 8-4Page: 185

57.Which is not a valid Exit statement?

a.) Exit Do

b.) Exit For

c.) Exit Form

d.) Exit Select

e.) Exit Sub

Answer: cLevel: Moderate

Section: 8-5Page: 187

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: cLevel: Easy

Section: 8-5Page: 187

59.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: cLevel: Moderate

Section: 8-5Page: 189

60.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: cLevel: Easy

Section: 8-6 Page: 190

61.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: eLevel: Easy

Section: 8-6Page: 190

62.An Exception is another name for a:

a.) compile error.

b.) logic error.

c.) runtime error.

d.) superficial error.

e.) syntax error.

Answer: cLevel: Moderate

Section: 8-6Page: 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-7Page: 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 Level: Easy

Section: 8-7Page: 193

65.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: aLevel: Easy

Section: 8-7Page: 195

66.What is the standard prefix for the name of a RadioButton?

a.) rad

b.) rab

c.) rdo

d.) rdb

e.) rbt

Answer: a Level: Easy

Section: 8-8Page: 195

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: bLevel: Easy

Section: 8-8Page: 195

68.Which event is activated when a RadioButton is selected?

a.) Checked

b.) CheckedChanged

c.) Selected

d.) SelectedChanged

e.) SelectionChanged

Answer: bLevel: Moderate

Section: 8-8Page: 196

Fill in the Blank Questions

69. Else statements are run when the previous ___condition___ is false.

Level: Moderate

Section: 8-1Page: 177

70.A(n) ___nested if ___ statement has an If statement embedded inside another If statement.

Level: Moderate

Section: 8-1Page: 177

71.The ElseIf statement is followed by a condition, the ___Else___ statement is not.

Level: Moderate

Section: 8-1Page: 177

72.The ___Is___ keyword is used with a relational test clause of a Select Case statement.

Level: Moderate

Section: 8-2Page: 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-2Page: 180

74.Every Case in the Select Case statement is followed by a ___test value clause___

Level: Hard

Section: 8-2Page: 179

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-3Page: 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-4Page: 184

81.It is optional to put the loopvariable after the ___Next___ statement.

Level: Moderate

Section: 8-4Page: 184

82.In the For…Next statement the step value default is ___1___.

Level: Easy

Section: 8-4Page: 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

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-7Page: 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-7Page: 193

93.The three letter prefix for a RadioButton control is ___rad___.

Level: Easy

Section: 8-8Page: 195

94.The RadioButton is best used when a user’s group of choices is ___mutually exclusive___.

Level: Hard

Section: 8-8Page: 195

95.To work together a group of RadioButtons must be in a single ___container___.

Level: Moderate

Section: 8-8Page: 195

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.

DimSumAsDouble = 0D

DimCount, IndexAsInteger

DimInputAsString

ForIndex = 1 To 10

Input = InputBox("Enter a Number")

IfIsNumeric(Input) Then

Sum = Sum + CDbl(Input)

Count = Count + 1

EndIf

Next

MsgBox("The Average is: " & (Sum / Count))

(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.

DimSumAsDouble = 0D

DimCountAsInteger = 0I

DimInputAsString

Do

Input = InputBox("Enter a Number")

IfIsNumeric(Input) Then

Sum = Sum + CDbl(Input)

Count = Count + 1

EndIf

LoopWhileCount < 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 - 1