You are on page 1of 14

Programming Right From the Start with Visual Basic.

NET

CHAPTER 7 Variables and Expressions


True-False Questions
1. Every variable has a name, data value, and data type. Answer: True Section: 7-1 Level: Easy Page: 142

2.

There are two types of integer data types: numeric and floating point. Answer: False Level: Easy Section: 7-1 Page: 143 It is better to use integer data types because they are processed faster. Answer: True Level: Easy Section: 7-1 Page: 143 The string data type holds characters found in the Unicode character set, which is a subset of the larger ASCII character set. Answer: False Level: Moderate Section: 7-1 Page: 144 String and Char data types are actually stored as numeric values in the computer. Answer: True Level: Easy Section: 7-1 Page: 144 Boolean data types have three possible values: true, false, or null. Answer: False Level: Easy Section: 7-1 Page: 144 The advantage of the object data type is that it uses a 4-byte pointer in addition to the data itself. Answer: False Level: Moderate Section: 7-1 Page: 145 The Date data type holds the day, month, and year while the Time data type hold seconds, minutes, and hours. Answer: False Level: Moderate Section: 7-1 Page: 145 VB.Net is flexible enough that you do not have to declare a variable before using it. Answer: False Level: Easy Section: 7-2 Page: 146 You can declare more than one variable with a single Dim statement. Answer: True Level: Easy Section: 7-2 Page: 146

3.

4.

5.

6.

7.

8.

9.

10.

7-1

Chapter 7 Variables and Expressions

11.

Multiple variables declared in the same Dim statement can be of different data types. Answer: False Level: Moderate Section: 7-2 Page: 146 A statement separate from the Dim statement is needed to initialize a variable. Answer: False Level: Easy Section: 7-2 Page: 146 A Constant must be initialized when it is declared. Answer: True Section: 7-2 Constants, but not variables, must start with a letter. Answer: False Section: 7-2 Level: Easy Page: 148 Level: Moderate Page: 147

12.

13.

14.

15.

Constants are used to make the program easier to read and maintain. Answer: True Level: Easy Section: 7-2 Page: 148 Variables, constants, and operators are combined to form expressions. Answer: True Level: Easy Section: 7-3 Page: 149 Using arithmetic operators will always result in a logical output. Answer: False Level: Easy Section: 7-3 Page: 149 Using comparison operators will always result in a logical output. Answer: True Level: Easy Section: 7-3 Page: 150 If X = 3 and Y = 2, then X ^ 2 * Y = 18. Answer: True Section: 7-3 If X = 2 and Y = 6, then 4 + Y / X = 5. Answer: False Section: 7-3 Level: Moderate Page: 151 Level: Moderate Page: 151

16.

17.

18.

19.

20.

21.

The left side of an assignment statement always has an expression. Answer: False Level: Easy Section: 7-4 Page: 152 Assignment statements are used to store data in a variable or property. Answer: True Level: Easy Section: 7-4 Page: 152

22.

7-2

Chapter 7 Variables and Expressions

23.

The equal sign (=) is used both as a comparison operator and an assignment operator. Answer: True Level: Easy Section: 7-4 Page: 152 If an equal sign can be replaced by a greater than sign, it is being used as an assignment operator. Answer: False Level: Moderate Section: 7-4 Page: 152 The CDbl intrinsic function will take a double data type and convert it into another data type. Answer: False Level: Moderate Section: 7-5 Page: 153 The IsNumeric intrinsic function will return a value of true if the string can be changed into a numeric value. Answer: True Level: Easy Section: 7-5 Page: 153 The Val intrinsic function will return a numeric value for the string one. Answer: True Level: Hard Section: 7-5 Page: 154 One limitation of the VB.NET intrinsic functions is that there are no financial functions. Answer: False Level: Easy Section: 7-5 Page: 154 All intrinsic functions take zero or one arguments. Answer: False Section: 7-5 Level: Easy Page: 154

24.

25.

26.

27.

28.

29.

30.

The FormatCurrency function takes a number and returns a string formatted in dollars and cents. Answer: True Level: Easy Section: 7-5 Page: 153 A dialog box is like a pop-up window that gets input from and provides output to the end user. Answer: True Level: Easy Section: 7-6 Page: 156 The text typed into the InputBox must be assigned to a string variable. Answer: False Level: Moderate Section: 7-6 Page: 156 The prompt argument of the InputBox is optional. Answer: False Section: 7-6 Level: Easy Page: 156

31.

32.

33.

34.

The MsgBox function will display a dialog box if the vbDisplay argument is used. Answer: False Level: Easy Section: 7-6 Page: 156

7-3

Chapter 7 Variables and Expressions

35.

No more than two string expressions can be combined in the MsgBox prompt argument. Answer: False Level: Moderate Section: 7-6 Page: 157 The VB constant vbCrLf is used to create a MsgBox message with multiple lines. Answer: True Level: Easy Section: 7-6 Page: 157 The TextBox can both get input from the user and give output to the user. Answer: True Level: Easy Section: 7-7 Page: 158 When the DataType property of the TextBox is set to Number, whatever the user types into the TextBox is stored in the Text property as a numeric data type. Answer: False Level: Moderate Section: 7-7 Page: 158 Only the Copy method places selected TextBox text in the clipboard. Answer: False Level: Easy Section: 7-7 Page: 159 The TextBox property MaxLength can be used to make sure the user does not enter more than nine characters for a Social Security Number. Answer: True Level: Easy Section: 7-7 Page: 158 Scroll bars will automatically appear if too much data is typed into a Textbox. Answer: False Level: Moderate Section: 7-7 Page: 158 Scroll bars will automatically appear if too many items are entered into a Listbox. Answer: True Level: Moderate Section: 7-8 Page: 161 The Alphabetical Property of a ListBox will sort the items in alphabetical order. Answer: False Level: Easy Section: 7-8 Page: 160 The Index for the Items in a ListBox is zero-based. Answer: True Section: 7-8 Level: Moderate Page: 160

36.

37.

38.

39.

40.

41.

42.

43.

44.

45.

The Items.Clear method of a ListBox will clear the selected item. Answer: False Level: Easy Section: 7-8 Page: 160

7-4

Chapter 7 Variables and Expressions

46.

Items are added to the ListBox during run time with the Properties Window. Answer: False Level: Moderate Section: 7-8 Page: 161 The ComboBox has a Text Property and an Items Property. Answer: True Level: Easy Section: 7-9 Page: 162 The Items property of the ComboBox contains a collection of individual Items. Answer: True Level: Moderate Section: 7-9 Page: 162 The ComboBox combines features of the ListBox and InputBox. Answer: False Level: Easy Section: 7-9 Page: 162 In a ComboBox the user can always type in a new value to the list of items. Answer: False Level: Easy Section: 7-9 Page: 162

47.

48.

49.

50.

Multiple Choice Questions


51. Which is not an integer data type? a.) Single b.) Byte c.) Short d.) Integer e.) Long Answer: a Section: 7-1 52. Which is a numeric data type? a.) Floating point b.) Integer c.) Boolean d.) Both a and b. e.) All of the above. Answer: d Section: 7-1 Level: Moderate Page: 143 Level: Moderate Page: 143

7-5

Chapter 7 Variables and Expressions

53.

Which sequence of char data types is listed from lowest to highest? a.) a, A, z, Z b.) a, z, A, Z c.) A, a, Z, z d.) A, Z, a, z e.) z, a, Z, A Answer: d Section: 7-1 Level: Moderate Page: 145

54.

The Date data type does not hold which type of information. a.) Seconds b.) Hours c.) Days d.) Months e.) Quarters Answer: e Section: 7-1 Level: Easy Page: 145

55.

The Boolean data type: a.) is unsigned. b.) has two states. c.) is displayed by the program as yes or no. d.) Both a and b. e.) All of the above. Answer: d Section: 7-1 Level: Moderate Page: 144

56.

Which is a valid statement for declaring a variable? a.) Const Form As Integer b.) Const myForm As Integer c.) Dim Form As Integer d.) Dim myForm As Integer e.) All of the above. Answer: d Section: 7-2 Level: Moderate Page: 147

57.

VB.Net identifiers: a.) are case sensitive. b.) can begin with an underscore. c.) can begin with a number. d.) Both a and b. e.) All of the above. Answer: b Section: 7-2 Level: Moderate Page: 147

7-6

Chapter 7 Variables and Expressions

58.

The name of a constant: a.) must both begin with a letter and be all upper case. b.) does not have to begin with a letter but must be all upper case. c.) must begin with a letter but can be upper or lower case. d.) does not have to begin with a letter and be either upper or lower case. e.) None of the above. Answer: d Section: 7-2 Level: Moderate Page: 148

59.

The proper operator precedence, from first to last, is: a.) logical, comparison, and arithmetic. b.) arithmetic, comparison, and logical. c.) arithmetic, logical, and comparison. d.) comparison, arithmetic, and logical. e.) logical, arithmetic, comparison. Answer: b Section: 7-3 Level: Moderate Page: 151

60.

With A = False and B = True, which statement evaluates as True? a.) A AND A b.) A AND B c.) B AND A d.) B AND B e.) None are true. Answer: d Section: 7-3 Level: Easy Page: 151

61.

With A = False and B = True, which statement evaluates as False? a.) A OR A b.) A OR B c.) B OR A d.) B OR B e.) None are true. Answer: a Section: 7-3 Level: Easy Page: 151

62.

Which operator is evaluated first? a.) NOT b.) AND c.) XOR d.) OR e.) They are always evaluated left-to-right. Answer: a Section: 7-3 Level: Moderate Page: 151

7-7

Chapter 7 Variables and Expressions

63.

The left side of an assignment statement will hold: a.) a variable. b.) an object property. c.) an expression. d.) Both a and b. e.) All of the above. Answer: d Section: 7-4 Level: Easy Page: 152

64.

The right side of an assignment statement will hold: a.) a variable. b.) an object property. c.) an expression. d.) Both a and b. e.) All of the above. Answer: c Section: 7-4 Level: Easy Page: 152

65.

Which function will return the monthly payments of a loan? a.) Pay (Rate, PV, Nper) b.) Pmt (Rate, Nper, PV) c.) FV (Rate, Nper, Pmt) d.) FV (Rate, Nper, PV) e.) None of the above. Answer: b Section: 7-5 Level: Easy Page: 154

66.

Which function returns the numbers represented in the string $56.7? a.) Abs b.) CDbl c.) Int d.) Rnd e.) Val Answer: b Section: 7-5 Level: Moderate Page: 153

67.

What will the function Val ($165.30) return? a.) 0 b.) 165 c.) 165.30 d.) $165.30 e.) An error Answer: a Section: 7-5 Level: Easy Page: 153

7-8

Chapter 7 Variables and Expressions

68.

Which function displays a pop-up window? a.) MsgBox b.) InputBox c.) TextBox d.) Both a and b. e.) All of the above. Answer: d Section: 7-6 Level: Easy Page: 156

69.

Which is true about the prompt argument? a.) It can be made of multiple values concatenated into one string. b.) It can include the vbCrLf constant. c.) It can include the ampersand symbol to concatenate strings. d.) Both a and b. e.) All of the above. Answer: e Section: 7-6 Level: Easy Page: 157

70.

In order to process a number typed in a TextBox the programmer must: a.) use the Val function to convert the Text value. b.) use the CDbl function to convert the Text value. c.) use the IsNumeric function to convert the Text value. d.) Both a and b. e.) All of the above. Answer: d Section: 7-7 Level: Moderate Page: 158

71.

Which TextBox method does not use the clipboard? a.) Clear b.) Copy c.) Cut d.) Paste e.) All of these methods use the clipboard. Answer: a Section: 7-7 Level: Easy Page: 159

72.

Which TextBox property should always be changed first? a.) AcceptsReturn b.) BorderStyle c.) Font d.) Name e.) Text Answer: d Section: 7-7 Level: Moderate Page: 158

7-9

Chapter 7 Variables and Expressions

73.

Which is not a valid value for the ListBox SectionMode Property? a.) None b.) One c.) MultiSimple d.) MultiExtended e.) All of the above. Answer: e Section: 7-8 Level: Moderate Page: 160

74.

Setting the SelectedIndex property of a ListBox to -1 will: a.) cause an error. b.) cannot be done. c.) de-select any selected item. d.) Both a and b. e.) All of the above. Answer: c Section: 7-8 Level: Hard Page: 160

75.

Which method of a ListBox will remove just one item at a time? a.) Items.RemoveAt b.) Item.RemoveAt c.) Items.ClearAt d.) Item.ClearAt e.) Items.Clear Answer: a Section: 7-8 Level: Moderate Page: 160

76.

The Items property of a ComboBox: a.) is a collection of items. b.) is the same as the Items property of a ListBox. c.) contains methods and properties. d.) Both a and b. e.) All of the above. Answer: e Section: 7-9 Level: Moderate Page: 162

77.

Which value for the ComboBox DropDownStyle property allows a user to type in data? a.) DropDown b.) DropDownSimple c.) DropDownList d.) Both a and b. e.) All of the above. Answer: a Section: 7-9 Level: Moderate Page: 164

7 - 10

Chapter 7 Variables and Expressions

78.

Which two controls combined to form the ComboBox control? a.) ListBox and TextBox b.) ListBox and InputBox c.) ListBox and MsgBox d.) Label and TextBox e.) Label and InputBox Answer: a Section: 7-9 Level: Easy Page: 162

Fill in the Blank Questions


79. The best integer data type to use with a 32-bit processor is the ___Integer___ type. Level: Moderate Section: 7-1 Page: 143 Any numeric value that has a decimal must be stored as a(n) ___floating-point___ data type. Level: Easy Section: 7-1 Page: 143 The ___Char___ data type is a single character while the ___String___ data type is a sequence of characters. Level: Easy Section: 7-1 Page: 144 When no data type is included in the Dim statement of a variable, VB.NET uses the ___Object___ data type as the default. Level: Moderate Section: 7-1 Page: 145 A variable is declared with the ___Dim___ statement. Level: Easy Section: 7-2

80.

81.

82.

83.

Page: 146

84.

The unique name of a variable is known as a(n) ___identifier___. Level: Easy Section: 7-2 Page: 147 The VB.NET compiler sees the variable names BookCost and bookcost as the same. This is an example of how VB.NET identifiers are ___case insensitive___. Level: Moderate Section: 7-2 Page: 147 ___Constants___ are easier to identify when they are typed in all upper case. Level: Easy Section: 7-2 Page: 148

85.

86.

7 - 11

Chapter 7 Variables and Expressions

87.

Addition, subtraction, multiplication, and exponents are examples of ___arithmetic___ operators. Level: Easy Section: 7-3 Page: 149 Greater Than, Less Than, Not Equal, and Equal are examples of ___comparison___ operators. Level: Easy Section: 7-3 Page: 150 NOT, OR, AND, and XOR are examples of ___logical___ operators. Level: Easy Section: 7-3 Page: 151 When multiple operators are present in a complex expression, using ___parenthesis___ will make the expression easier to read. Level: Moderate Section: 7-3 Page: 151 We use ___assignment___ statements to store data in variables or properties. Level: Easy Section: 7-4 Page: 152 A(n) ___expression___ is found on the right side of an assignment statement. Level: Easy Section: 7-4 Page: 152 To make sure a string can be converted into a number use the ___IsNumeric___ function. Level: Easy Section: 7-5 Page: 153 The function that will return the future value of an annuity is the ___FV___ function. Level: Easy Section: 7-5 Page: 155 The ___Rnd___ function will return a random number between 0 and 1. Level: Easy Section: 7-5 Page: 153 When the user presses the ___Cancel___ button in the InputBox an empty string is returned. Level: Moderate Section: 7-6 Page: 156 The ___title___ argument in the InputBox function is optional. Level: Moderate Section: 7-6 Page: 156 The ___InputBox___ function displays a text box in its dialog box, the ___MsgBox___ function does not. Level: Easy Section: 7-6 Page: 157

88.

89.

90.

91.

92.

93.

94.

95.

96.

97.

98.

7 - 12

Chapter 7 Variables and Expressions

99.

The VB constant ___vbCrLf___ causes a carriage return and line feed to be inserted into the prompt argument. Level: Moderate Section: 7-6 Page: 157 The ___AppendText___ TextBox Method will add text to the end of the existing text. Level: Easy Section: 7-7 Page: 159 The VB constant, vbCrLf, will act like pressing the enter key when using a ___multiline__ TextBox. Level: Hard Section: 7-7 Page: 158 The ___Text___ property of a TextBox holds the data in the TextBox. Level: Easy Section: 7-7 Page: 158 The ___Undo___ method of a TextBox will reverse the last edit operation in the TextBox. Level: Moderate Section: 7-7 Page: 159 The ___lst___ prefix is used to identify the name of a ListBox. Level: Easy Section: 7-8 Page: 160 The ___SelectedIndex___ property of a ListBox will return a -1 when no item is selected. Level: Moderate Section: 7-8 Page: 160 The ___Items.Add___ method of the ListBox will append text to the end of the list. Level: Moderate Section: 7-8 Page: 160 The ___Items___ property of the ListBox is an object with a set of properties and methods. Level: Moderate Section: 7-8 Page: 160 The ___cbo___ prefix is used to identify the name of a ComboBox. Level: Easy Section: 7-9 Page: 162 If the DropDownStyle property of a ComboBox is set to ___DropDownList___ the user cannot type text into the ComboBox. Level: Moderate Section: 7-9 Page: 162

100.

101.

102.

103.

104.

105.

106.

107.

108.

109.

7 - 13

Chapter 7 Variables and Expressions

110.

The ComboBox combines properties of the ___ListBox___ and ___TextBox___. Level: Moderate Section: 7-9 Page: 162

Essay Questions
111. Write code to load a ListBox with the cities Detroit, Denver, and Dublin. Make sure the cities are listed in alphabetical order and that the second item in the list is selected. lstCity.Items.Add("Detroit") lstCity.Items.Add("Denver ") lstCity.Items.Add("Dublin") lstCity.Sorted = True lstCity.SelectedIndex = 2 112. What are the required rules for naming variables and constants? What are the suggested rules? Both variables and constants are considered Identifiers and as such must (1) begin with either a letter character or the underscore character, (2) only contain letters, numbers, or the underscore, and (3) not be the same as any reserved words (i.e. Dim, Integer, String, etc.). For both variables and constants it is suggested that they start with a letter and that they should be easy to understand. To be easily understandable the names should be composed of complete words that clearly describe the variable. However the names should not be so long that they are difficult to manage. To distinguish variables and constants, it is suggested that constants be all CAPITAL LETTERS while variables are in a mixed case where the First Letter of each word is capitalized.

7 - 14