Chapter 5  Decisions

Section 5.1  Relational and Logical Operators

1.         Which of the following is true?

(A)  "Cat" = "cat"

(B)  "Cat" < "cat"

(C)  "Cat" > "cat"

(D)  Relational operators are only valid for numeric values.

2.         Which of the following is a valid VB.NET conditional statement?

(A)  2 < n < 5

(B)  2 < n Or < 5

(C)  2 < n Or 5

(D)  (2 < n) Or (n < 5)

3.         Which statement is true?

(A)  "Ford" < "Ford"

(B)  Chevy > Chevy

(C)  GMC <= GMC

(D)  Oldsmobile < Oldsmobile

4.         Which statement is false?

(A)  "Ford" >= "Ford"

(B)  "Chevy" <= "chevy"

(C)  GMC <= GMC

(D)  Oldsmobile < Oldsmobile

5.         The three main logical operators are ________, _________, and ________.

(A)  And, Or, Not

(B)  And, Not, If

(C)  Or, Not, If

(D)  False, And, True

6.         When using the logical operator “And”, what part of the expression must be true?

(A)  only the left part.

(B)  only the right part.

(C)  both parts.

(D)  either the left or right part, but not both.

7.         When using the logical operator “Or”, part of the expression must be true?

(A)  only the left part.

(B)  only the right part.

(C)  either the left or right part, but not both.

(D)  either the left or right part.

8.         When is the expression “Not cond1” true?

(A)  when cond1 is false.

(B)  when cond1 is true.

(C)  when cond1 is either true or false.

(D)  none of the above.

9.         Which value for x would make the following condition true:  x >= 5

(A)  x is equal to 7

(B)  x is equal to 5

(C)  x is equal to 5.001

(D)  all of the above

10.     Which value for x would make the following condition true: Not (x >= 5)

(A)  x is equal to 7

(B)  x is equal to 4

(C)  x is equal to 5.001

(D)  all of the above

11.     Which value for x would make the following condition true: (x >= 5) And (x <= 6)

(A)  x is equal to 7

(B)  x is equal to 5

(C)  x is equal to 5.001

(D)  B and C

12.     "1st Place" < "2nd Place" (T/F)

13.     The following two statements are equivalent. (T/F)

Not (a < b)

a > b

14.     In VB.NET, the letter j is considered to be greater than the letter y. (T/F)

15.     The And operator requires that both conditions be true for the compound condition to be true. (T/F)

16.     The following condition evaluates to true. (T/F)

("DOG" > "CAT") And ("CAT" > "BIRD") And ("BIRD" > "aardvark")

17.     Assume that A, B, and C are conditions, with A being true, B being true, and C being false. Give the truth value of each of the following conditions.

(a)   A And (Not B)

(b)   A And Not (B Or C)

(c)   Not (A And B)

18.     Is the following condition True or False (assume a = 2, b = 3, and c = 3)?

(a = c) Or (b = c)

19.     < and = are examples of relational operators. (T/F)

20.     Conditions can involve variables, operators and functions. (T/F)

21.     Two stings are compared working from left to right, character by character, to determine which one should precede the other. (T/F)

22.     Write a condition to express each of the following:

(a)   X is strictly between –10 and 10.

(b)   Exactly one of X and Y is greater than 6.

(c)   Both X and Y are positive or both X and Y are negative.

Section 5.2  If Blocks

1.         Consider the following two sets of code.

(a) If (a = 1) And (b = 1) Then        (b)   If a = 1 Then

      txtBox.Text = "Hi"                       If b = 1 Then

    End If                                       txtBox.Text = "Hi"

                                              End If

                                            End If

Which one of the following statements is true?

(A)  (a) and (b) will produce different outputs.

(B)  (b) is not a valid set of VB.NET instructions.

(C)  The two sets of code are equivalent, but (a) is preferred to (b) because (a) is clearer.

(D)  The condition statement in (a) is not valid.

2.         What will be displayed by the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim a, b, c, x As Double

  a = 5

  b = 3

  c = 6

  If a > c Then

    x = 1

  Else

    If b > c Then

      x = 2

    Else

      x = 3

      txtBox.Text = CStr(x)

    End If

  End If

End Sub

(A)  1

(B)  2

(C)  3

(D)  None of the above.

3.         Consider the following program.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim x, y, z As Double

  Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

  x = CDbl(sr.ReadLine)

  y = CDbl(sr.ReadLine)

  z = CDbl(sr.ReadLine)

  If ((x > y) Or (x > z)) And (z >= 100) Then

    txtBox.Text = "Hello, "

  Else

    txtBox.Text = "Good-bye, "

  End If

  sr.Close()

  txtBox.Text &= "Larry."

End Sub

Which of the following sets of data in the three lines of the file DATA.TXT will produce the output "Hello, Larry."?

(A)  2, 3, 101

(B)  3, 2, 100

(C)  2, 3, 100

(D)  3, 2, 4

4.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim x, y, z As Double

  x = 3

  y = 3

  If x > y Then

    z = x + y

  Else

    z = y - x

  End If

  txtBox.Text = CStr(z)

End Sub

(A)  6

(B)  3

(C)  0

(D)  No output

5.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim word1, word2, newword As String

  word1 = "shower"

  word2 = "about"

  newword = word1.Substring(0, 4) & word2.Substring(0, 3)

  If newWord.IndexOf("how") = -1 Then 

    txtBox.Text = "The new word was not found."

  Else

    txtBox.Text = "Found it."

  End If

End Sub

(A)  The new word was not found.

(B)  Found it.

(C)  Syntax error

(D)  No output

6.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim num As Integer = 10

  DisplayMult(num)

  num = 5

  DisplayMult(num)

  num = 2

  DisplayMult(num)

End Sub

Sub DisplayMult(num As Integer)

  If num <= 3 Then

    txtOutput.Text &= CStr(3 * num)

  Else

    If num > 7 Then

      txtOutput.Text &= CStr(7 * num)

    End If

  End If

End Sub

(A)  7014

(B)  30614

(C)  706

(D)  No output

7.         What will be the output of the following lines of code?

Dim phrase As String = "A penny saved is worth nothing."

If phrase.IndexOf("pen") <> -1 Then

  txtBox.Text = CStr(phrase.IndexOf("pen"))

Else

  txtBox.Text = CStr(-1)

End If

(A)  pen

(B)  2

(C)  0

(D)  A penny saved is worth nothing.

8.         Which of the following is not a logical operator in VB.NET?

(A)  Not

(B)  And

(C)  Or

(D)  Then

9.         What will be displayed when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim num As Double = 9

  txtBox.Text = CStr(CalculateSquareRoot(num))

End Sub

Function CalculateSquareRoot(ByVal NumberArg As Double) As Double

  If NumberArg < 0 Then

    MsgBox("Error - cannot find square root - result set to zero")

    Return 0

  Else

    Return Math.Sqrt(NumberArg)

  End If

End Function

(A)  0

(B)  3

(C)  6

(D)  An error will occur.

10.     A(n) _____________ allows a program to decide on a course of action based on whether a certain condition is true or false.

(A)  Or block

(B)  If block

(C)  And block

(D)  Else block

11.     Constructs in which an If block is contained inside another If block are called:

(A)  multi-If blocks

(B)  nested If blocks

(C)  sequential If blocks

(D)  none of the above

12.     What word(s) will appear in the list box when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim num As Integer = 5

  If num = 2 Then

    lstBox.Items.Add("Two")

  ElseIf num > 3 Then

    lstBox.Items.Add("Great")

  ElseIf num = 5 Then

    lstBox.Items.Add("Equal")

  End If

End Sub

(A)  Two, Great, and Equal

(B)  Great and Equal

(C)  Great

(D)  Equal

13.     When an If block has completed execution, the program instruction immediately following the If block is executed. (T/F)

14.     The following lines of code are correct. (T/F)

If (txtAge.Text >= 62) Then

  txtOutput.Text = "You are elegible to collect Social Security."

End If

15.     The following lines of code are correct. (T/F)

If age >= 13 And < 20 Then

  txtOutput.Text = "You are a teenager."

End If

16.     The Else part of an If block may be omitted if no action is associated with it. (T/F)

17.     Given that x = 7, y = 2, and z = 4, the following If block will display "TRUE". (T/F)

If (x > y) Or (y > z) Then

  txtBox.Text = "TRUE"

End If

18.     Given that x = 7, y = 2, and z = 4, the following If block will display "TRUE". (T/F)

If (x > y) And (y > z) Then

  txtBox.Text = "TRUE"

End If

19.     Every If block must have a Then and an Else. (T/F)

20.     No more than one ElseIf statement may occur in any one If block. (T/F)

21.     No more than one Else statement may occur in any one If block. (T/F)

22.     The following line of code is valid. (T/F)

If letter = ("The quality of mercy is not strained").Substring(7, 1) Then

Section 5.3  Select Case Blocks

1.         Why is the following Select Case block invalid in VB.NET?

Select Case myName.Substring(0, 1)

  Case myName < "D"

    txtBox.Text = "Your name starts with A, B, or C."

End Select

(A)  There should not be a standard conditional statement in a value list.

(B)  There are not enough Case statements.

(C)  There is no selector.

(D)  myName.Substring(0, 1) is not valid where it is.

2.         Suppose that the selector in a Select Case block is the string variable myvar. Which of the following is NOT a valid Case clause?

(A)  Case "Adams"

(B)  Case "739"

(C)  Case (myVar.Substring(0, 1)

(D)  Case myVar.Length

3.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim name As String = "Washington"

  Select Case name

    Case "George"

      txtBox.Text = "George"

    Case "Wash"

      txtBox.Text = "Wash"

    Case "WASHINGTON"

      txtBox.Text = "WASHINGTON"

    Case Else

      txtBox.Text = "Washington"

  End Select

End Sub

(A)  WashWashington

(B)  Washington

(C)  WASHINGTONWashington

(D)  No output

4.         What is the problem (if any) with the following Select Case block which is intended to determine the price of a movie depending on the patron's age?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim age as Integer, price As Double

  age = CInt(InputBox("Enter your age:"))

  Select Case age

    Case Is >= 65       'Senior citizen

      price = 4.50

    Case Is >= 5        'Regular price

      price = 6.00     

    Case Is >= 0        'Child (no charge with parents)

      price = 0

    Case Else

      txtBox.Text = "Entry error"

  End Select

End Sub

(A)  Everyone will get in free at the child rate.

(B)  The output will always be "Entry error."

(C)  The Case Is statements have bad syntax.

(D)  There is nothing wrong.

5.         What will be the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim a, b, c, acronym As String

  a = "federal"

  b = "aviation"

  c = "administration"

  acronym = a.Substring(0, 1) & b.Substring(0, 1) & c.Substring(0, 1)

  Select Case acronym

    Case "FAA"

      txtBox.Text = "Federal Aviation Administration"

    Case "DEA"

      txtBox.Text = "Drug Enforcement Agency"

    Case Else

      txtBox.Text = "Unknown acronym. Sorry."

  End Select

End Sub

(A)  Federal Aviation Administration

(B)  Drug Enforcement Agency

(C)  Syntax error

(D)  Unknown acronym. Sorry.

6.         In a Select Case block, if more than one Case clause matches the selector,

(A)  only the statements associated with the first matching Case clause will be executed.

(B)  all of the statements associated with each matching Case clause will be executed.

(C)  only the statements associated with the last matching Case clause will be executed.

(D)  the program will malfunction and stop.

7.         What will be displayed in the list box when the following code runs? (Choose two.)

Select Case num

  Case 6, 7, 11

    lstBox.Items.Add("W")

  Case Is < 7

    lstBox.Items.Add("X")

  Case Is > 5

    lstBox.Items.Add("Y")

  Case Else

    lstBox.Items.Add("Z")

End Select

(A)  Z can never be displayed.

(B)  W, X and Y will be displayed if the value of num is 6

(C)  W and Y will be displayed if the value of num is 7

(D)  Y will be displayed if the value of num is 12

8.         Different items appearing in the same value list of a Select Sase block must be separated by a(n) ____________.

(A)  semi colon

(B)  comma

(C)  colon

(D)  pair of quotation marks

9.         Which Case clause will be true whenever the value of the selector in a Select Case block is between 1 and 5 or is 8?

(A)  Case 1 To 8

(B)  Case 1 To 5, 8

(C)  Case 1 To 8, 5

(D)  Case 1 To 5; 8

10.     Which Case clause will be true whenever the value of the selector in a Select Case block is greater than or equal to 7?

(A)  Case Is >7

(B)  Case Is = 8

(C)  Case Is >= 7

(D)  Case Is <= 8

11.     What type of items are valid for use in the value list of a Case clause?

(A)  literals

(B)  variables

(C)  expressions

(D)  all of the above

12.     The Case Else part of a Select Case block is optional. (T/F)

13.     One may use an If block within a Select Case block. (T/F)

14.     One may use a Select Case block within an If block. (T/F)

15.     Every Select Case block can be replaced by If blocks. (T/F)

16.     One may use a Select Case block within another Select Case block. (T/F)

17.     Select Case choices are determined by the value of an expression called a selector.(T/F)

18.     Items in the value list must evaluate to a literal of the same type as the selector.(T/F)

19.     A single Case statement can contain multiple values.(T/F)

20.     You can specify a range of values in a Case clause by using the To keyword. (T/F)