Chapter 1 An Introduction to Computers and Problem Solving Section 1.1 An Introduction to Computers 1.
Visual Basic is considered to be a (A) first-generation language. (B) package. (C) higher-level language. (D) machine language. C
2.
The person who actually runs a computer program is called a (A) user. (B) customer. (C) client. (D) tester. (E) runner. A
3.
A collection of lines of instruction is called a(n) (A) program. (B) algorithm. (C) system. (D) programmer. A
4.
Which of the following is NOT considered to be one of the three basic features of a program? (A) input (B) processing (C) output (D) store D
5.
Software refers to the people who work with computer hardware. (T/F) F
6.
Visual Basic uses a graphical user interface. (T/F) T
Section 1.2 Program Development Cycle 1.
An algorithm is defined as: (A) a mathematical formula that solves a problem. (B) a tempo for classical music played in a coda. (C) a logical sequence of steps that solve a problem. (D) a tool that designs computer programs and draws the user interface. C
2.
Which of the following is the proper order of procedures used in the problem-solving process? (A) design, analysis, coding, testing (B) analysis, testing, design, coding (C) analysis, design, coding, testing (D) analysis, design, testing, coding (E) design, testing, analysis, coding C
3.
The process of finding and correcting errors in a program is called (A) pseudocoding. (B) debugging. (C) algorithms. (D) development cycles. B
4.
In most cases, a well-written program need not be tested. (T/F) F
5.
The analysis and design steps of program planning are largely independent of the particular computer language the programmer is using. (T/F) T
6.
When starting a new program, it is best to start writing code as soon as possible to avoid wasting time thinking about it. (T/F) F
7.
Often a problem is too difficult to understand until one writes the program. (T/F) F
8.
Although the documentation step is usually listed last in the problem-solving process, it should actually begin when the problem is first defined and continue through the problemsolving process. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
Order the steps in the program development cycle starting with 1 for the first step and so on. ____ Program design (devise an algorithm). ____ Test the program until it is error free. ____ Test the design. ____ Problem analysis (define the problem). ____ Determine the input and output. ____ Review the code. ____ Add new features. ____ Code the program. ____ Enter the program. 384127956
Section 1.3 Programming Tools 1.
What does the parallelogram flowchart symbol represent? (A) input/output (B) terminal (C) decision (D) connector (E) process A
2.
Pseudocode is (A) data that have been encoded for security. (B) the incorrect results of a computer program. (C) a program that doesn't work. (D) the obscure language computer personnel use when speaking. (E) a description of an algorithm similar to a computer language. E
3.
Which one of the following is NOT one of the three basic types of statement structures? (A) sequence (B) loop (C) decision (D) input/output D
4.
Which of the following types of charts shows only the relationships between general program tasks without showing specific modules or directions for data flow? (A) top-down chart (B) flowchart (C) both A and B (D) neither A nor B A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
5.
Suppose in the very early stages of the problem-solving process, your supervisor wants you to show her the relationships between the various processes that will be needed to solve the problem. The best way to do this would be to use (A) a top-down chart. (B) a flowchart. (C) a sample program. (D) pseudocode. B
6.
If you are writing a program that needs to repeat a series of calculations, what programming structure should you use to repeat the calculations? (A) sequence structure (B) decision structure (C) looping structure (D) unconditional branching C
7.
"Desk-checking" refers to (A) the continual problem programmers have with losing things. (B) timing a program to see how fast it completes its tasks. (C) testing an algorithm at the flowchart stage using several different kinds of data. (D) having a fellow programmer read your pseudocode to be sure it is understandable. C
8.
What does the rectangle flowchart symbol represent? (A) input/output (B) terminal (C) decision (D) connector (E) process E
9.
What does the diamond flowchart symbol represent? (A) input/output (B) terminal (C) decision (D) connector (E) process C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. What does the circle flowchart symbol represent? (A) input/output (B) terminal (C) decision (D) connector (E) process D 11. What does the “rectangle with rounded sides” flowchart symbol represent? (A) input/output (B) annotation (C) decision (D) start/stop (E) process D 12. A graphical depiction of the logical steps to carry out a task and show how the steps relate to each other is called a(n) (A) flowchart. (B) pseudocode. (C) algorithms. (D) hierarchy chart. A 13. A flowchart is an arrangement of geometric shapes connected by arrows. (T/F) T 14. A top-down chart is the same thing as a flowchart. (T/F) F 15. Pseudocode and flowcharts are two different tools or methods used in planning a solution to a problem. (T/F) T 16. Suppose your supervisor at work wants you to write a program to accomplish a task that he has specified. However, he has not let you know which computer language to use. It is still possible to design the program in pseudocode. (T/F) T 17. During the design phase, all errors will be caught by Visual Basic's smart editor. (T/F) F 18. Although a flowchart (as its name suggests) depicts data flow very well, it is not easily modified once written. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
19. If a problem is very complex, extensive design prior to coding will actually delay the completion of the project. (T/F) F 20. The divide-and-conquer-method of problem solving breaks a problem into large, general pieces first, then refines each piece until the problem is manageable. (T/F) T 21. The purpose of a test or decision in a looping structure is to tell when the loop should end. (T/F) T 22. Flowcharts, pseudocode, and top-down charts can be used to solve problems only in Visual Basic. (T/F) F 23. What are the three basic methods of control used in designing structured algorithms? sequence, decision, loop 24. Define the following: (A) algorithm (B) pseudocode (C) flowchart
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 2 Visual Basic, Controls, and Events Section 2.1 An Introduction to Visual Basic 2012 1.
Programming in VB 2012 is different from traditional programming environments because first you should (A) write the code. (B) input the data. (C) name the buttons. (D) draw the user interface. D
2.
GUI stands for (A) graphical user interface. (B) graphing user introduction. (C) graphical unit interface. (D) graphical user input. A
3.
Visual Basic first appeared about ten years ago. (T/F) F
4.
A mouse click is an example of an event. (T/F) T
Section 2.2 Visual Basic Controls 1.
Press F4 to (A) run a program. (B) display the Properties window. (C) display the Solution Explorer window. (D) terminate a program. B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
2.
Which of the properties in a control's list of properties is used to give the control a meaningful name? (A) Text (B) ContextMenu (C) ControlName (D) Name D
3.
Visual Basic access keys are created by using which symbol in a button's Text property? (A) @ (B) & (C) % (D) # B
4.
Which of the following steps specifies P as the access key for a button? (A) Set the Text property to Com_pute. (B) Set the Text property to Com&pute. (C) Set the Text property to &Com_pute. (D) Set the Text property to comPute. B
5.
When a Visual Basic program is running, the user can move from one control to another using the keyboard by pressing the (A) Tab key. (B) Space bar. (C) Enter key. (D) Backspace key. A
6.
Which of the following properties determines the words appearing in a form's title bar? (A) Text (B) Caption (C) Name (D) Title A
7.
What effect will the following statement have? lblOne.Visible = False
(A) Enable lblOne (B) Delete lblOne (C) Make lblOne invisible (D) It is not a valid Visual Basic statement. C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
8.
When creating a new program in Visual Basic, you are asked to supply a name for the program. If you do not specify a name, a default name is used. What is this default name? (A) Wapplication followed by a number. (B) Application followed by a number. (C) WindowsApplication. (D) WindowsApplication followed by a number. D
9.
In Visual Basic, tooltips assist by showing a small caption about the purpose of each icon on the Toolbar. How do you make a tooltip appear? (A) Right click the Toolbar icon and select purpose from the available options. (B) Position the mouse pointer over the icon for a few seconds. (C) Hold down a shift key, then click the appropriate Toolbar icon to display its purpose. (D) Hold down the Alt key, then click the appropriate Toolbar icon to display its purpose. B
10. The Properties window plays an important role in the development of Visual Basic programs. It is mainly used (A) to change how objects look and feel. (B) when opening programs stored on a hard drive. (C) to allow the developer to graphically design program components. (D) to set program related options like Program Name, Program Location, etc. A 11. Sizing Handles make it very easy to resize virtually any control when developing programs with Visual Basic. When working in the Form Designer, how are these sizing handles displayed? (A) A rectangle with 4 arrows, one in each corner, around your control. (B) A 3-D outline around your control. (C) A rectangle with small squares around your control. (D) None of the above C 12. The Font dialog box allows you to select different Fonts, their style, their size, and some other special effects. How do you bring up this Font dialog box? (A) In the Properties window, click the ellipsis (…) on the right side of the settings box for the Font property. (B) Double-click the control, and select Font Property. (C) Right-click the control, and select Font Property. (D) None of the above A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
13. What keyboard shortcut is used to run the current program? (A) F4 (B) F5 (C) F6 (D) F7 B 14. What would be a good name for a text box to hold a person’s first name? (A) txtFirstName (B) FirstName (C) txt First Name (D) First Name A 15. What feature in Visual Basic allows you to make more room on your screen by temporarily minimizing certain windows? (A) Help (B) Auto Hide (C) Auto Minimize (D) Expert-View B 16. Which property is available for most controls that allows you to hide/unhide them either manually by setting the property or by setting it during run time via code? (A) Clear (B) Refresh (C) Visible (D) View-Control C 17. What property of controls tells the order they receive the focus when the tab key is pressed during run time? (A) Focus order (B) Focus number (C) Tab index (D) Control order C 18. What is the default tab index of the first object placed on a form? (A) 0 (B) 1 (C) First (D) None of the above A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
19. What property of a control determines whether the control can receive the focus during run time? (A) TabOrder (B) Focus (C) TabIndex (D) TabStop D 20. Which of the following is not a submenu of the Format menu? (A) Align (B) Make Same Size (C) Center in Form (D) Widen D 21. Changing a control's Text property also changes how you refer to the control in code. (T/F) F 22. You can display the Properties window by pressing F4. (T/F) T 23. The Properties window is used to change how objects look and react. (T/F) T 24. When working with text boxes, the sizing handles allow you to resize the object by dragging to make it wider or narrower. (T/F) T 25. Once a text box control is placed on a form, it cannot be resized or moved. (T/F) F 26. F5 is the keyboard shortcut used to activate the Properties window. (T/F) F 27. Once a control is placed on your form, you can rename it by editing the Name property in the Properties window. (T/F) T 28. The Description pane, located below the Properties windows, shows a brief explanation of the highlighted property. (T/F) T 29. The ForeColor property of a text box changes the color of the form containing the text box. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
30. Shortcut keys like F5 (Run), allow you to perform certain tasks without the use of the mouse. (T/F) T 31. The recommended prefix for the name of a button control is btn. (T/F) T 32. The Auto Hide feature of the Toolbox is active when the pushpin is horizontal. (T/F) T 33. A group of several controls can be resized or moved simultaneously. (T/F) T 34. The control with tab index 1 is always the first control to receive the focus when the program is run. (T/F) F 35. Snap lines can be used to align the bottoms, middles, and tops of controls. (T/F) T 36. The key combination Shift + arrow key can be used to resize a control or a group of controls. (T/F) T Section 2.3 Visual Basic Events 1.
When the user clicks a button, _________ is raised. (A) an event (B) a method (C) a setting (D) a property A
2.
Which of the following is not one of the three steps used to create a Visual Basic program? (A) create the interface (B) set the properties of the objects (C) specify the methods (D) write the code C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
3.
IntelliSense is a technology built into Visual Basic that assists the programmer by (A) automatically inserting text and words that have similar meaning to those currently being entered by the programmer. (B) allowing the programmer to edit object properties from inside the code window. (C) replacing misspelled words with the correct spelling as soon as the programmer presses the Enter key. (D) automatically displaying the methods and properties available to a control. D
4.
What of the following is a correct statement for specifying the words to appear in the title bar of Form1? (A) Form1.Text = "My Text" (B) Form1.TitleBar = "My Text" (C) Me.Text = "My Text" (D) Me.Caption = "My Text" C
5.
When will the following event procedure be executed? Private Sub txtBox_TextChanged(...) Handles txtBox.TextChanged
(A) when text is added to the text box (B) when text is deleted from the text box (C) when text is modified inside the text box (D) All of the above D 6.
Which of the following statements specifies that the color of the text in txtBox be red? (A) txtBox.Text = Color.Red (B) txtBox.ForeColor = Color.Red (C) txtBox.ForeColor = Red (D) txtBox = Color.Red B
7.
The statement btnButton.Focus()
(A) has no effect. (B) moves the focus to the button btnButton. (C) has the same effect as clicking on the button btnButton. (D) is invalid in Visual Basic. B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
8.
Which of the following statements sets the words on a button to "Push Me"? (A) btnButton.Name = "Push Me" (B) btnButton.Text = Push Me (C) btnButton.Text = "Push Me" (D) btnButton = "Push Me" C
9.
Which of the following is a valid statement in Visual Basic? (A) Form1.Text = "Revenue" (B) Form1.Caption = "Revenue" (C) btnButton.Text = Push Me (D) Me.Text = "Revenue" D
10. A user action such as clicking a button is called (A) an accident (B) an event (C) a procedure (D) a property B 11. Visual Basic responds to events using which of the following? (A) a code procedure (B) an event procedure (C) a form procedure (D) a property B 12. Which of the following statements will place "Greetings" in the title bar of a form? (A) Me.Title = "Greetings" (B) Me.Text = "Greetings" (C) Me.Name = "Greetings" (D) Me.Heading = "Greetings" B 13. The following lines of code are valid. (T/F) Private Sub Click(...) Handles Click txtBox.Text = "" End Sub
F 14. Keywords are also referred to as reserved words. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
15. Complete Word is a helpful feature produced by the Microsoft Technology called IntelliSense. (T/F) T 16. The Visual Basic Code Editor will automatically detect certain types of errors as you are entering code. (T/F) T 17. The statement txtBox.Font.Bold = True
will produce an error message when Visual Basic tries to execute it. (T/F) T 18. The Visual Basic Code Editor automatically capitalizes the first letters of reserved words. (T/F) T 19. The first line of an event procedure must contain both the keyword Sub and Handles. (T/F) T 20. The statement btnButton = "Press" produces an error message. (T/F) T 21. The Properties window cannot be used to display the events associated with a control. (T/F) F 22. Write a statement that gives the focus to btnCompute. btnCompute.Focus()
23. Write a statement that disables btnCompute. btnCompute.Enabled = False
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 3 Variables, Input, and Output Section 3.1 Numbers 1.
What is the correct statement when declaring and assigning the value of 100 to an Integer variable called numPeople? (A) Dim numPeople = 100 (B) Dim numPeople = Int(100) (C) numPeople = 100 (D) Dim numPeople As Integer = 100 D
2.
What is the value of Int(8.9)? (A) 8 (B) 8.9 (C) 9 (D) 1 A
3.
The type of error that is normally spotted by the Code Editor is: (A) runtime (B) logic (C) syntax (D) user C
4.
Which of the following arithmetic operations has the highest level of precedence? (A) +(B) */ (C) ^ (D) ( ) D
5.
Which of the following statements removes all text from lstBox? (A) lstBox.Clear() (B) lstBox.Items.Clear() (C) lstBox.Text.Clear() (D) lstBox.Items.Add("") B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
What will be the contents of the variable x after the following statement is executed? x = Math.Sqrt(((9 + 7) / (4 * 2)) + 2)
(A) 1 (B) 2 (C) 3 (D) 4 (E) None of the above B 7.
Suppose the Double variable num has the value 123.4567. What value will the following statement assign to num? num = Math.Round(num, 2)
(A) 123.4567 (B) 123.457 (C) 123.45 (D) 123.46 D 8.
What value will be assigned to the numeric variable x when the following statement is executed? x = 2 + 3 * 4
(A) 20 (B) 14 (C) 92 (D) 234 B 9.
Which of the following statements declare the variables a and b as type Integer? (A) a = 0: b = 0 (B) Dim a, b (C) Dim a & b As Integer (D) Dim a, b As Integer D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. Assume that x, y, and temp are Integer variables. Which of the following lines of code swaps the values of x and y? (A) x = y y = x
(B) x = temp x = y y = temp
(C) temp = x x = y y = temp
(D) x = y temp = x y = temp
C 11. Which of the following is a valid name for a variable? (A) Two_One (B) 2One (C) Two One (D) Two.One A 12. Given x = 3 and y = 1, what value will be assigned to the Double variable w when the following statement is executed? w = (x + y) / (x – y)
(A) 1 (B) 2 (C) 3 (D) None of the above B 13. What will be displayed when the following lines are executed? Dim x As Double = 3, y As Double = 1 Dim z As Double z = x + (y * x) x = y z = x + z lstBox.Items.Add(x + y + z)
(A) 4 (B) 9 (C) 10 (D) None of the above B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
14. Which of the following is the same as 2 ^ 3? (A) 2 * 2 * 2 (B) 2 * 3 (C) 2 + 2 + 2 (D) 3 * 3 A 15. Which of the numbers below is equivalent to 0.000017489? (A) 1.7489E+05 (B) 17489E+06 (C) 1.7489E-06 (D) 1.7489E-05 D 16. Keywords in Visual Basic are words that (A) should be used when naming variables. (B) are used to name controls, such as TextBox1, Command2, etc. (C) have special meaning and should not be used when naming variables. (D) are used as prefixes for control names (such as txt, btn, lbl, and lst). C 17. When declaring a variable that will refer to a submarine, a good name for the variable is sub. (T/F) F 18. The function Math.Int will always round mixed numbers down to the next lowest integer. (T/F) T 19. When using the equal sign to assign values, the variable on the left hand side of the equal sign will always receive the value. (T/F) T 20. The variables firstName and firstNAME are identical. (T/F) T 21. The following statement assigns 6 times the value of y to x. (T/F) x = 6y
F 22. The following statement is valid. (T/F) y = y + 72
T 23. A numeric variable that has not been assigned a value has the default value zero. (T/F) T © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
24. The following statement is valid. (T/F) Dim x As Double = 3,542.36
F 25. The statement a + b = c assigns to c the sum of the values of a and b. (T/F) F 26. The exponential notation used in Visual Basic is exactly the same as standard mathematical notation. (T/F) F 27. The lstBox.Items.Clear() statement is used to empty the contents of a list box. (T/F) T 28. Visual Basic always displays numbers in decimal format. (T/F) T 29. An assignment statement is used to assign a value to a variable or property. (T/F) T 30. You can use a variable name in almost any place you could use a literal value. (T/F) T 31. Numeric variables can be initialized to zero or any other number, but once they are initialized, they cannot be changed. (T/F) F 32. The following two statements are equivalent. (T/F) var1 = var2 var2 = var1
F 33. The value of (11 Mod 2) is 1. (T/F) T 34. The value of (11 \ 2) is 1. (T/F) F 35. The following two statements are equivalent, where numVar is a numeric variable. (T/F) numVar = numVar + 1 numVar += 1
T 36. What is the value of numVar after the following three statements are executed? Dim numVar As Integer = 5 numVar = numVar + 2 numVar += 3
10 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Section 3.2 Strings 1.
Which of the following statements is a valid assignment statement? (A) txtBox = "Hello World" (B) "Hello World" = txtBox.Text (C) txtBox.Text = "Hello World" (D) Text.txtBox = "Hello World" C
2.
What is the correct syntax for displaying the value of the String variable myString in a text box? (A) txtBox.Text = "myString" (B) txtBox.Text = myString (C) txtBox.Text.myString (D) txtBox.Text = Str(myString) B
3.
Which of the following code statements generates an error? (A) txtFirst.Text = "Hello" (B) txtFirst.Text = Hello (C) txtFirst.Text = "My Text" (D) txtFirst.Text = Me.Text B
4.
Which of the following statements will NOT display the number 5 in the text box? (A) txtBox.Text = 5 (B) txtBox.Text = "5" (C) txtBox.Text = CStr("5") (D) txtBox.Text = CStr(5) A
5.
Which statement will display the words “Hello World” in a text box? (A) txtBox.Text = Hello & World (B) txtBox.Text = "Hello " & World (C) txtBox.Text = Hello & " World" (D) txtBox.Text = "Hello" & " World" D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
Which of the following expressions has as its value the value of strVar with its leading and trailing spaces removed? (A) strVar.Length (B) strVar.ToUpper (C) strVar.Ctrim (D) strVar.Trim D
7.
Which statement can be used to clear the contents of a text box? (A) txtBox.Empty = True (B) txtBox = "" (C) txtBox.Text = "" (D) txtBox.Wipe() C
8.
What character is used to signify the beginning of a comment statement? (A) asterisk (B) exclamation mark (C) apostrophe (D) backslash C
9.
The joining of two strings together to form a new string is called (A) concatenation. (B) addition. (C) compaction. (D) substrings. A
10. Given the data assigned to the string variable str2 shown below, which of the following statements will assign the value ALL to the string variable str1? str2 = "WHEN ALL ELSE FAILS, READ THE DIRECTIONS"
(A) str1 = str2.Substring(5) (B) str1 = str2.Indestr1OfStr("ALL") (C) str1 = str2.Substring(6, 3) (D) str1 = str2.Substring(5, 3) D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
11. What will be the output of the following statement? txtBox.Text = CStr(Math.Round(17 / 2))
(A) 8 (B) 9 (C) 8.5 (D) 17.00 A 12. What will be the output of the following lines? Dim alphabet, soup As String alphabet = "abcdefghijklmnopqrstuvwxyz" soup = alphabet.ToUpper txtBox.Text = alphabet.Substring(0, 5) & soup.Substring(0, 5)
(A) abcdeABCDE (B) ABCDEABCDE (C) eE (D) EE A 13. What is the value of ("12/15/09").Substring(3,2)? (A) /15 (B) 15 (C) /15/ (D) 12/15/09 B 14. What will be the value of numVar when the following code is executed? Dim strVar As String, numVar As Integer strVar = "Now is the time for all good men" numVar = strVar.IndexOf("the time for")
(A) "Now is all good men" (B) "12" (C) 7 (D) 8 C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
15. What will be displayed when the following lines are executed? Dim a, b, c As String a = "THE WHOLE" b = "PART" c = a.Substring(CInt(Math.Sqrt(4)), b.Length) txtBox.Text = CStr(c)
(A) THE WHOLE PART (B) 6 (C) E WH (D) HE W C 16. What will be displayed when the following lines are executed? Dim x As Double = 2 'x = 3 txtBox.Text = CStr(x)
(A) 3 (B) 0 (C) 2 (D) None of the above C 17. The symbol for the string concatenation operator is (A) @ (B) & (C) % (D) # B 18. What will be the output of the following line? txtBox.Text = "2" & 3
(A) 23 (B) 2 3 (C) 5 (D) Syntax error A 19. The following statement contains what type of error? txtAverage.Txt = CStr(CDbl(txtNum1.Text) + CDbl(txtNum2.Text) / 2)
(A) a syntax error only (B) a logic error only (C) both a syntax error and a logic error (D) no errors C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
20. Which of the following expressions will yield the string "John Doe", where name1 = "John Blake" and name2 = "Janet Doe"? (A) name1.Substring(0, 4) & name2.Substring(6, 8) (B) name1.Substring(0, 4) & name2.Substring(0, 3) (C) name1.Substring(0, 5) & name2.Substring(6, 3) (D) None of the above C 21. If strVar.Length is 5, then the value of strVar.Substring(4, 1)
is the last character of the value of strVar. (T/F) T 22. The value of strVar.Length is the number of characters in the value of strVar. (T/F) T 23. The following lines are valid. (T/F) Dim h As String = "Hello" txtBox.Text = CStr(h.IndexOf("h"))
T 24. All comment statements must be placed at the beginning of a program. (T/F) F 25. The statement lstBox.Items.Add("") clears all the text from the list box. (T/F) F 26. The statement txtBox = "Hello" is an example of a syntax error. (T/F) T 27. The following statements assign the lowercase letters to the string variable alphabet. (T/F) Dim alphabet As String alphabet = abcdefghijklmnopqrstuvwxyz
F 28. The following statement is valid where dog and cat are variables of the same type. (T/F) dog = cat
T 29. Option Explicit requires you to declare every variable before its use. (T/F) T 30. With Option Strict On, a statement of the form intVar = dblVar is not valid. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
31. A statement of the form dblVar = intVar is a narrowing assignment statement. (T/F) F 32. An assignment statement of the form intVar = 8 / 4 is not valid. (T/F) T 33. After the statement txtBox.Text = "" is executed, the text box will be clear and the cursor will appear inside the text box. (T/F) F 34. A string literal is a sequence of characters that is treated as a single entity. (T/F) T 35. When declaring a variable of type String, you must specify a length for the string.(T/F) F 36. Typecasting is used to convert a value to a different data type. (T/F) T 37. The Trim method is used to remove all blank space before and after a string. (T/F) T 38. The Substring method is used to extract a portion of a string. (T/F) T 39. The empty string is the same as a string that contains one space. (T/F) F 40. A variable declared inside an event procedure is said to have local scope. (T/F) T 41. A variable declared outside of an event procedure is said to have class-level scope. (T/F) T 42. A variable declared inside an event procedure cannot have the same name as a variable declared inside another procedure. (T/F) F 43. When a variable declared in one event procedure has the same name as a variable declared in another event procedure, any change to the value of one of the variables will affect the value of the other variable. (T/F) F 44. Write a statement that assigns the first character of the value of strVar1 to strVar2. strVar2 = strVar1.Substring(0,1)
45. Write a statement that assigns the last character of the value of strVar1 to strVar2. strVar2 = strVar1.Substring(strVar1.Length - 1, 1)
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Section 3.3 Input and Output 1.
What is the proper syntax when using a message dialog box? (A) MessageBox.Show("Hi there", "Hi") (B) MessageBox.Show(Hi there, Hi) (C) MessageBox.Show "Hi There", "Hi" (D) MessageBox.Show Hi There, Hi A
2.
Which of the following is not used to assign a value to a variable? (A) InputBox statement (B) Dim statement (C) an assignment statement (D) MessageBox.Show statement D
3.
Which statement prompts the user for a name and then assigns the name to the string variable strName? (A) strName = InputBox("What is your first name?", "First Name") (B) strName = MessageBox.Show("What is your first name?", "First Name") (C) InputBox("What is your first name?", "First Name") = strName (D) MessageBox.Show "What is your first name?", "First Name" = strName A
4.
Suppose you are 20 years old, num is an integer variable, and the statement num = 1 + CInt(InputBox("How old are you?"))
is executed and responded to? What will be the output of txtBox.Text = num & " years old"
(A) 1 + "How old are you?" (B) Syntax error (C) 20 years old (D) 21 years old D 5.
What message will be displayed by the following statement: MessageBox.Show("This is a test.", "Test")
(A) This is a test. (B) Nothing. (C) Test (D) This is a test. Test A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
What is displayed in the title bar of the message dialog box by the following statement? MessageBox.Show("This is a test.", "Test")
(A) This is a test. (B) Nothing. (C) Test (D) The same as the name of the program. C 7.
What will be the output of the following statement? txtBox.Text = (1234.567).ToString("C")
(A) $1234.567 (B) 1,234.57 (C) $1234.57 (D) $1,234.57 D 8.
What will be the output of the following statement? txtBox.Text = FormatNumber(1234.56789).ToString("N3")
(A) 1234.568 (B) 1230 (C) 1,234.568 (D) 1,230 C 9.
What will be the output of the following statement? txtBox.Text = (2/3).ToString("P4")
(A) 0.6666 (B) 0.6667 (C) 66.66% (D) 66.6667% D 10. Which of the following masks is appropriate for a masked text box into which the user will enter a license plate consisting of two digits followed by 4 letters? (A) &&LLLL (B) &&&&&& (C) 00LLLL (D) None of the above C 11. The value of FormatCurrency(12345).ToString("C") is $12,345.00. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
12. The value returned by InputBox is a string. (T/F) T 13. The line continuation character must be preceded with a space. (T/F) T 14. When the Mask property of a masked text box is set to LL, at most two characters (consisting of letters or spaces) can be entered into the masked text box. (T/F) T 15. Write a statement that produces a message dialog box with the words "US Motto" in its title bar and the message "E PLURIBUS UNUM". MessageBox.Show("E PLURIBUS UNUM", "US Motto")
16. If TAX_RATE is a named constant of type Double, which of the following statements is valid? (A) TAX_RATE += 1 (B) dblVar = TAX_RATE + 1 (C) TAX_RATE = 2 * TAX_RATE (D) All of the above B 17. Which statement assigns the date of the first day of the year 2014 to the Date variable firstDay? (A) firstDay = 1/1/2014 (B) firstDay = "1/1/2014" (C) firstDay = &1/1/2014& (D) firstDay = #1/1/2014# D 18. The value of (#1/1/2014#).ToString("D") is (A) 01/01/2014 (B) January 1, 2014 (C) January 01, 2014 (D) Sunday, January 1, 2014 D 19. Which statement assigns today's date to the Date variable currently? (A) currently = #Now# (B) currently = #Today# (C) currently = Now (D) currently = Today D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
20. If the value of the Date variable currently is the first day of 2011, then what is displayed by the statement txtBox.Text = CStr(currently.AddMonths(1))? (A) 1/2/2011 (B) 2/1/2011 (C) February 1, 2011 (D) Tuesday February 1, 2011 B 21. Named constants must have class-level scope. (T/F) F 22. The value assigned to a class-level named constant can be changed in any procedure. (T/F) F 23. The statement Const TAX_RATE As Double is not valid. (T/F) T 24. The value of DateDiff(DateInterval.Day, #1/1/2014#, #2/1/2014#) is 31. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4 Decisions Section 4.1 Relational and Logical Operators 1.
Asc("A") is 65. What is Asc("C")?
(A) 66 (B) 67 (C) 68 (D) "C" B 2.
Asc("A") is 65. What is displayed by txtBox.Text = Chr(65) & "BC"?
(A) ABC (B) A BC (C) 656667 (D) Not enough information is available. A 3.
Which of the following expressions has as its value the words "Hello World” surrounded by quotation marks? (A) "Hello World" (B) Chr(34) & "Hello World" (C) Chr(34) & Hello World & Chr(34) (D) Chr(34) & "Hello World" & Chr(34) D
4.
The statement txtBox.Text = Chr(162) causes Visual Basic to display the cents symbol (ANSI 162) in the text box, regardless of the font in use. (T/F) F
5.
Which of the following is true? (A) "Cat" = "cat" (B) "Cat" < "cat" (C) "Cat" > "cat" (D) Relational operators are only valid for numeric values. B
6.
Which of the following is a valid Visual Basic conditional statement? (A) 2 < n < 5 (B) 2 < n Or < 5 (C) 2 < n Or 5 (D) (2 < n) Or (n < 5) D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
7.
Which statement is true? (A) "Ford" < "Ford" (B) “Chevy” > “Chevy” (C) “GMC” <= “GMC” (D) “oldsmobile” < “Oldsmobile” C
8.
Which statement is false? (A) "Ford" >= "Ford" (B) "Chevy" <= "chevy" (C) “GMC” <= “GMC” (D) “Oldsmobile” < “Oldsmobile” D
9.
The operator And is a(n) _________ operator. (A) arithmetic (B) relational (C) logical (D) none of the above C
10. The three main logical operators are ________, _________, and ________. (A) And, Or, Not (B) And, Not, If (C) Or, Not, If (D) False, And, True A 11. When using the logical operator “And”, what part of the expression must be true? (A) only the left part (B) only the right par (C) both parts (D) either the left or right part, but not both C 12. When using the logical operator “Or”, what 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 D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
13. 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 A 14. 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 D 15. 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 B 16. 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 D 17. "1st Place" < "2nd Place" (T/F) T 18. The following two statements are equivalent. (T/F) Not (a < b) a > b
F 19. In Visual Basic, the letter j is considered to be greater than the letter y. (T/F) F 20. The And operator requires that both conditions be true for the compound condition to be true. (T/F) T 21. If two simple conditions are true, the compound condition created from them by the Or operator is also true. (T/F) T 22. The condition "Y" = "y" is true. (T/F) F © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
23. If two simple conditions are true, the compound condition created from them by the And operator is also true. (T/F) T 24. The following condition evaluates to True. (T/F) ("DOG" > "CAT") And ("CAT" > "BIRD") And ("BIRD" > "aardvark")
F 25. Assume that A, B, and C are conditions, with A being true, B being true, and C being false. Give the truth value of (A And (Not B)). False 26. Assume that A, B, and C are conditions, with A being true, B being true, and C being false. Give the truth value of (A And Not (B or C)). False 27. Assume that A, B, and C are conditions, with A being true, B being true, and C being false. Give the truth value of (Not (A And B)). False 28. Is the following condition True or False (assume a = 2, b = 3, and c = 3)? (a = c) Or (b = c) True 29. < and = are examples of relational operators. (T/F) T 30. Conditions can involve variables, operators, and functions. (T/F) T 31. Two strings are compared working from left to right, character by character, to determine which one should precede the other. (T/F) T 32. Write a condition to express each of the following: (a) X is strictly between –10 and 10. (X>–10) And (x<10) (b) Exactly one of X and Y is greater than 6. ((X > 6) And (Y <= 6)) Or ((Y > 6) And (X <= 6)) Another answer is ((X > 6) Or (Y > 6)) And Not ((X > 6) And (Y > 6)) (c) Both X and Y are positive or both X and Y are negative. (X>0 And Y >0) Or (X<0 And Y<0) Another answer is X * Y > 0 33. The value of "education".EndsWith("on") is True. (T/F) T 34. The value of "education".StartsWith("ED") is True. (T/F) F © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
35. The value of IsNumeric("$20.00") is True. (T/F) T 36. The value of IsNumeric("Twenty") is True. (T/F) F Section 4.2 If Blocks 1.
Consider the following two sets of code. (a) If (a = 1) And (b = 1) Then txtBox.Text = "Hi" End If
(b)
If a = 1 Then If b = 1 Then 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 Visual Basic 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. C 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 C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
3.
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 C 4.
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 B 5.
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. B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
Which of the following is not a logical operator in Visual Basic? (A) Not (B) And (C) Or (D) Then D
7.
What will be displayed when the button is clicked? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim num As Double = 9 Dim sqrRoot As Double If num < 0 Then MessageBox.Show("Cannot find square root. Result set to zero.", "Error") sqrRoot = 0 Else sqrRoot = Math.Sqrt(Num) End If txtBox.Text = CStr(sqrRoot)) End Sub
(A) 0 (B) 3 (C) 6 (D) An error will occur. B 8.
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 B
9.
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 B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. 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 C 11. When an If block has completed execution, the program instruction immediately following the If block is executed. (T/F) T 12. 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
F 13. The following lines of code are correct. (T/F) If age >= 13 And < 20 Then txtOutput.Text = "You are a teenager." End If
F 14. The Else part of an If block may be omitted if no action is associated with it. (T/F) T 15. 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
T 16. 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
F 17. Every If block must have a Then and an Else. (T/F) F 18. No more than one ElseIf statement may occur in any one If block. (T/F) F © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
19. No more than one Else statement may occur in any one If block. (T/F) T 20. A variable declared inside an If block ceases to exist after the block is exited. (T/F) T 21. The following line of code is valid. (T/F) If letter = ("The quality of mercy is not strained").Substring(7, 1) Then
T 22. The following line of code is valid. (T/F) If 0 ≤ grade ≤ 100 Then
F Section 4.3 Select Case Blocks 1.
Why is the following Select Case block invalid in Visual Basic? 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 expression 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. A 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 D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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 B 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. D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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. D 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. A
7.
What will be displayed in the list box when the following code runs? 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) Z will always be displayed. A 8.
Different items appearing in the same value list of a Select Case block must be separated by a ____________. (A) semi colon (B) comma (C) colon (D) pair of quotation marks B © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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 B
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 C 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 D 12. The Case Else part of a Select Case block is optional. (T/F) T 13. One may use an If block within a Select Case block. (T/F) T 14. One may use a Select Case block within an If block. (T/F) T 15. Every Select Case block can be replaced by If blocks. (T/F) T 16. One may use a Select Case block within another Select Case block. (T/F) T 17. Select Case choices are determined by the value of an expression called a selector. (T/F) T 18. Items in the value list must evaluate to a literal of the same type as the selector. (T/F) T 19. A single Case statement can contain multiple values. (T/F) T 20. You can specify a range of values in a Case clause by using the To keyword. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
21. A variable declared inside a Select Case block cannot be referred to by code outside of the block. (T/F) T Section 4.4 Input via User Selection 1. At design time, values can be placed into a list box via the _______ . (A) String Editor (B) String Collection Editor (C) Collection Editor (D) Items Editor B 2. A collection of related radio buttons is usually placed in a _______ . (A) List box (B) Text box (C) Collection Editor (D) Group box D 3. The default event procedure for a list box is _______ . (A) SelectedIndexChanged (B) Click (C) CheckedChanged (D) IndexChanged A 4. The default event procedure for a check box is _______ . (A) SelectedIndexChanged (B) Click (C) CheckedChanged (D) Checked C 5. Clicking on a checked radio button unchecks the radio button. (T/F) F 6. Clicking on a checked check box unchecks the check box. (T/F) T 7.
Which of the following statements turns on the radio button radButton? (A) radButton = True (B) radButton.On = True (C) radButton.Checked = True (D) radButton.Selected = True C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
8. Which of the following is not commonly used to allow the user to select among several options? (A) a collection of radio buttons (B) a collection of check boxes (C) a list box (D) a collection of text boxes D 9. . The value of lstBox.Text is the currently selected item of the list box. (T/F) T 10. Which of the following techniques does NOT always toggle the checked and unchecked state of a check box control? (A) Click on the square or its caption with the mouse. (B) Press the spacebar when the square has the focus. (C) Press Alt+Access key (if an access key has been specified). (D) Set the Checked property equal to 1. D 11. Which of the following techniques always toggles the "on" and "off" states of a radio button control? (A) Click on the circle or its caption with the mouse. (B) Press the spacebar when the circle has the focus. (C) Press on another radio button in the same group box. (D) Set the Checked property equal to True. (E) None of the above E 12. What is the key difference between a group of check boxes attached to a group box and a group of radio buttons attached to a group box? (A) Only radio buttons can be toggled. (B) Only one radio button at a time can be checked. (C) Check boxes cannot be attached to a group box. (D) The only difference is the shape of the control. B 13. When a check box is checked, the value of the Checked property will be (A) True. (B) False. (C) 1. (D) 0. A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
14. The title (as seen by the user) for the group box control is set by which one of the following properties? (A) Text (B) Name (C) Tag (D) Anchor A 15. When the user clicks on a check box control at run time, which one of the following events is raised (in addition to the Click event)? (A) ControlAdded (B) Invalidated (C) StyleChanged (D) CheckedChanged D 16. A check box control named chkFirst contains the following code in its CheckedChanged event procedure. Which of the following is true concerning the use of this control? Dim message As String = "hello" MessageBox.Show(message)
(A) The message "hello" will appear when the user checks the control but not when it is unchecked. (B) The message "hello" will appear when the user unchecks the control but not when it is checked. (C) The message "hello" will appear when the user checks the control and again when it is unchecked. (D) The message "hello" will not appear using this code. C 17. Suppose there are four radio button controls attached to a group box control on a form. How many radio buttons can be selected (that is, be “on”) at any given time? (A) 2 (B) 1 (C) 3 (D) 4 B 18. A radio button named radButton is placed on a form, and the statement MessageBox.Show(radButton.Checked) is placed in its CheckedChanged event procedure. At run time, what result will be displayed in the message box when the user clicks on radButton? (A) 1 (B) True (C) 2 (D) False B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
19. After several controls of the same type have been created, they can be moved inside a group box and thereby become attached to it. (T/F) T 20. With a check box control, toggling the state of the small square raises the CheckChanged event. (T/F) T 21. Group boxes are passive objects used to group related sets of controls. (T/F) T 22. Programmers frequently write event procedures for the group box control. (T/F) F 23. If you set a group box control’s Visible property to False, the attached controls will still remain visible. (T/F) F 24. When a check box control has the focus, the spacebar can be used to invoke its CheckedChanged event. (T/F) T 25. As with the button and check box controls, an access key can be set up by placing an ampersand in front of a letter in the radio button’s Text property setting. (T/F) T 26. Suppose a form contains two group box controls with one radio button placed on each. In this case, both radio buttons can be selected (or turned “on”) at the same time. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 5 General Procedures Section 5.1 Function Procedures 1.
Variables appearing inside the parentheses of a calling statement are called ____________. (A) values of the function (B) parameters (C) coordinates (D) arguments D
2.
Variables appearing in the header of a Function procedure are called ____________. (A) values of the function (B) parameters (C) coordinates (D) arguments B
3.
What is displayed when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim a, b as String Dim x as Integer a = "How now brown cow." b = "brown" x = FindIt(a, b) txtBox.Text = CStr(x) End Sub Function FindIt(z1 as String, z2 as String) As Integer Dim x as Integer x = z1.IndexOf(z2) End Function
(A) "How now" (B) 8 (C) 0 (D) An error (E) None of the above C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4.
Consider the following event procedure that calls a Function procedure named Cube, which returns the cube of a number. Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim num, result As Double num = CDbl(InputBox("Enter a number to cube:")) result = Cube(num) txtBox.Text = "The cube of " & num & " is " & result & "." End Sub
Which of the following is a correct Function definition for Cube? 1. Function Cube(var As Double) As Double Return var ^ 3 End Function 2. Function Cube(num As Double) As Double Return num ^ 3 End Function
(A) 1 only (B) 2 only (C) Both 1 and 2 (D) Neither 1 nor 2 C 5.
Which of the following names would be best for the following Function (called NoName)? Function NoName(number As Double) As Double Return number ^ 3 + number ^ 3 End Function
(A) SquareAndCube (B) CubeAndDouble (C) CubeAndSquare (D) DoubleAndCube B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
What will be the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim word, result As String word = "Benjamin" result = Rotate(word) result = Rotate(result & word) result = Rotate(result) txtBox.Text = result End Sub Function Rotate(var As String) As String Dim varlength As Integer varlength = var.Length Return var.Substring(1) & var.Substring(0, 1) End Function
(A) jaminBBenjaminen (B) BenjaminBenjamin (C) njaminBe (D) None of the above A 7.
The arguments appearing in a calling statement must match the parameters in the appropriate Sub or Function header in all but one of the following ways. Which one? (A) Number of arguments (B) Names of arguments (C) Data type of arguments (D) Order of arguments B
8.
Based on what it returns, what would be a better name for the function "Mystery" in the following program? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim sentence As String, result As String sentence = "Socrates is a man." result = Mystery(sentence) txtBox.Text = result End Sub Function Mystery(sentence As String) As String Dim position As Integer position = sentence.IndexOf(" ") Return sentence.Substring(0, position) End Function
(A) FirstWord (B) LastWord (C) FirstLetter (D) LastLetter (E) DoesNothing A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
The input to a user-defined function can consist of: (A) a single value (B) one or more values (C) no values (D) All of the above D
10. A Function procedure need not have parameters. (T/F) T 11. A Function procedure may return up to two values. (T/F) F 12. Function procedures can call other Function procedures. (T/F) T 13. Although a Function procedure can return a value, it cannot directly display information in a text box. (T/F) F 14. The value returned by a Function procedure must be a number or a string. (T/F) F 15. When the button is clicked on, the output of the following program will be 5. (T/F) Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim x, y, result As Double x = 3 y = 4 result = CInt((Norm(x, y))) txtBox.Text = CStr(result) End Sub Function Norm(x As Double, y As Double) As Double Return Math.Sqrt(x^2 + y^2) End Function
T 16. Both the input and output of a Function procedure can consist of several values. (T/F) F 17. The input to a user-defined function can consist of one or more values. (T/F) T 18. The header of a Function procedure must include parameters. (T/F) F 19. A user-defined function cannot be used in an expression. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
20. Function names should be suggestive of the role performed. The names also must conform to the rules for naming variables. (T/F) T Section 5.2 Sub Procedures, Part I 1.
When an End Sub statement is reached in a Sub procedure, execution jumps to (A) the statement before the statement that called the Sub procedure. (B) the statement after the statement that called the Sub procedure. (C) the beginning of the event procedure containing the calling statement. (D) the end of the event procedure containing the calling statement. B
2.
Consider the following Sub procedure. Sub TruncateSum(var1 As Double, var2 As Double, var3 As Double) txtBox.Text = CStr(Int(var1 + var2 + var3)) End Sub
What will be the output when TruncateSum is used in the following lines of code? Dim num1, num2, num3 As Double num1 = 3.5 num2 = 6.75 num3 = 1 TruncateSum(num1, num2, num3)
(A) 10 (B) 12 (C) 0 (D) 11 D 3.
What is wrong with the following calling statement and its corresponding Sub statement? MyProcedure("The Jetsons", 1000, 209.53) Sub MyProcedure(var1 As Double, var2 As Double, var3 As Double)
(A) It is not valid to pass something like "The Jetsons." (B) Constant values like 1000 cannot be passed, only variables. (C) var1 is not of the same data type as "The Jetsons." (D) Nothing is wrong with them. C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4.
Which one of the following is true about arguments and parameters? (A) Arguments appear in calling statements; parameters appear in Sub statements. (B) Parameters appear in calling statements; arguments appear in Sub statements. (C) They are synonymous terms. (D) They are completely unrelated in a program. A
5.
What will be the output when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim first, middle, last As String first = "Augusta" middle = "Ada" last = "Byron" Initials(first, middle, last) End Sub Sub Initials(c As String, b As String, a As String) Dim theInitials As String theInitials = a.Substring(0, 1) & b.Substring(0, 1) & c.Substring(0, 1) txtBox.Text = theInitials End Sub
(A) AAB (B) BAA (C) abc (D) ABA B 6.
What will be displayed when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim lng, wid As Double lng = 5 wid = 10 ComputeArea(lng, wid) End Sub Sub ComputeArea(length As Double, width As Double) Dim area As Double area = length * width txtBox.Text = CStr(area) End Sub
(A) 0 (B) 50 (C) 15 (D) 25 B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
7.
What will be the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim word As String word = "progression" Strange(word) End Sub Sub Strange(var As String) txtBox.Text = var.Substring(CInt(Int(var.Length / 2)), 1) End Sub
(A) progr (B) r (C) e (D) progre C 8.
What is the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim a, b As String txtBox.Clear() a = "A" b = "B" PrintWords(a, b) PrintWords(b, a) End Sub Sub PrintWords(a As String, b As String) txtBox.Text &= a & b End Sub
(A) ab ba (B) abba (C) ABBA (D) AB BA C 9.
Which of the following is NOT a reason for using procedures? (A) They break a complex problem down into smaller pieces. (B) They make a program run faster. (C) They can be reused easily. (D) They make it easier for a team of people to work together on a single program. B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. What will be the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim word1, word2, word3 As String word1 = "First" word2 = "Second" word3 = "Third" Myproc(word1, word2, word3) End Sub Sub Myproc(var3 As String, var2 As String, var1 As String) txtBox.Text = var1 & var2 & var3 End Sub
(A) FirstSecondThird (B) ThirdSecondFirst (C) SecondThirdFirst (D) No output B 11. What will be the output of the following program when the button is clicked on? 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 C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
12. The ______________ of a Sub procedure are vehicles for passing numbers and strings to the Sub procedure. (A) calling statements (B) arguments (C) parameters (D) variables declared inside C 13. Items appearing in the parentheses of a calling statement are known as _______________. (A) call variables (B) call strings (C) parameters (D) arguments D 14. The process of transmitting values to a Sub procedure is known as ______________. (A) passing (B) conveying (C) evaluating (D) referencing A 15. Each argument in a calling statement must have the same name as the corresponding parameter in the header of the called procedure. (T/F) F 16. Both constants and expressions can be used as arguments in calling statements. (T/F) T 17. Both constants and expressions can be used as parameters in Sub statements. (T/F) F 18. A Sub procedure can call another Sub procedure. (T/F) T 19. Sub procedures can be called only once during the execution of a program. (T/F) F 20. Each parameter defined for a Sub procedure corresponds to an argument passed in a calling statement for that procedure. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
21. Arguments and parameters can be used to pass values to Sub procedures from event procedures or other Sub procedures. (T/F) T 22. Parameters appearing in a Sub statement are part of the Sub procedure name. (T/F) F 23. Function procedures are different from Sub procedures because functions return a single value. (T/F) T Section 5.3 Sub Procedures, Part II 1.
What keyword in the header of a sub procedure denotes that a variable is passed by reference? (A) AsRef (B) ByReference (C) ByRef (D) ByVal C
2.
What will be displayed when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim x, y As String x = "tin" y = "can" Swap(x, y) txtOutput.Text = x & " " & y End Sub Sub Swap(ByRef x As String, y As String) Dim temp As String temp = x x = y y = temp End Sub
(A) tin can (B) can tin (C) tin tin (D) can can D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
3.
What will be the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim word As String word = "hairbrush" Decapitate(word) txtBox.Text = word End Sub Sub Decapitate(ByRef word As String) 'Chop the first letter off the word. word = word.Substring(1) End Sub
(A) airbrush (B) hairbrush (C) hairbrus (D) h A 4.
Suppose the variable myName is declared in a Dim statement in two different Sub procedures. Which statement is true? (A) The program will malfunction when it is executed. (B) When the value of myName is changed in one Sub procedure, it will also be changed in the other Sub procedure. (C) Visual Basic’s smart editor will alert you that this is an error before the program is executed. (D) The two variables will be local to their respective Sub procedures. D
5.
What will be the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim var1, var2, var3, num As Integer var1 = 2 var2 = 4 var3 = 6 Add(num) txtBox.Text = CStr(num) End Sub Sub Add(ByRef num As Integer) Dim var1, var2, var3 As Integer num = var1 + var2 + var3 End Sub
(A) 0 (B) 12 (C) 6 (D) None of the above A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
What will be the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim word1, word2, word3, result As String word1 = "The" word2 = "English" word3 = "Channel" CatWords(word1, word2, word3, result) txtBox.Text = result End Sub Sub CatWords(var1 As String, var2 As String, word3 As String, ByRef final As String) final = var1 & var2 & word3 End Sub
(A) TheEnglishChannel (B) TheEnglish (C) The English Channel (D) No output A 7.
What will be the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim number As Double = 3 DoubleAndSquare(number) txtBox.Text = CStr(number) End Sub Sub DoubleAndSquare(ByRef myVar As Double) myVar = myVar + myVar myVar = myVar * myVar End Sub
(A) 3 (B) 36 (C) 6 (D) 0 B 8.
Variables and named constants declared inside a procedure are said to have ________________. (A) local scope (B) procedure-level scope (C) class-level scope (D) None of the above A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
A variable or named constant that is visible to every procedure in a form’s code without being passed is called a __________ variable. (A) local (B) class-level (C) global (D) public B
10. The declaration statement for a class-level variable should be placed __________. (A) inside an event procedure (B) inside a general procedure (C) anywhere in the program region, except inside a procedure (D) above the statement Public Class frmName C 11. Suppose a variable is passed by value to a parameter of a Sub procedure, and the parameter has its value changed inside the Sub procedure. What will the value of the variable be after the Sub procedure has executed? (A) It will have the newly modified value from inside the Sub procedure. (B) Its value can’t be determined without more information. (C) It will retain the value it had before the call to the Sub procedure. (D) None of the above C 12. Suppose a variable is passed by reference to a parameter of a Sub procedure, and the parameter has its value changed inside the Sub procedure. What will the value of the variable be after the Sub procedure has executed? (A) It will have the newly modified value from inside the Sub procedure. (B) Its value can’t be determined without more information. (C) It will retain the value it had before the call to the Sub procedure. (D) None of the above A 13. What happens to a variable declared locally inside a Sub procedure after the procedure terminates? (A) It maintains its value even after the End Sub statement executes. (B) It ceases to exist after the End Sub statement executes. (C) It loses its value temporarily after the End Sub statement executes, but regains that value upon re-entry to the Sub procedure. (D) It is reset to its default value. B 14. Each variable must be passed either by value or by reference. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
15. You can determine whether a variable is being passed by value or reference by looking at the calling statement. (T/F) F 16. When the button is clicked, the output of the following program will be 20. (T/F) Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim num As Integer = 20 DoubleIt(num) txtBox.Text = CStr(num) End Sub Sub DoubleIt(ByRef var As Integer) var = var * 2 End Sub
F 17. The value of an argument in a calling statement can be changed by a Sub procedure only if the same name is used in the Sub procedure's parameter list. (T/F) F 18. Sub procedures can be individually tested before being placed into a program. (T/F) T 19. A value assigned to a variable in one part of a program always affects the value of the likenamed variable in the other part of the program. (T/F) F Section 5.4 Modular Design 1.
Top-down design refers to (A) an obsolete programming practice in which a program is written without procedures in one large module. (B) a program design where the key event procedures act like supervisors, delegating tasks to various Sub procedures. (C) a method of organizing a team of programmers. (D) a method of increasing the speed of a program. B
2.
Stepwise refinement refers to (A) the process whereby a malfunctioning program is fixed by correcting one "bug" at a time. (B) any procedure that calls another procedure (and so on) to accomplish a task. (C) breaking a large task into smaller tasks. (D) the belief that increasing the number of people working on a project decreases the time it will take to complete the project. C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
3.
Breaking up a large problem into smaller subproblems is called _________________. (A) transcribed refinement (B) refined modules (C) stepwise refinement (D) transcribed modules C
4.
Which of the following is NOT considered part of a good top-down design chart? (A) A module should accomplish as many tasks as possible. (B) Modules should proceed from general to specific as you read down the chart. (C) Modules should be as independent of each other as possible. (D) The design should be readable. A
5.
Which of the following logical structures should not be used in structured programming? (A) Sequence (B) Selection (C) Looping (iteration) (D) Unconditional branching D
6.
In modular programming, a driver is (A) another name for the team leader of a project. (B) a "dummy" program designed solely to call a single procedure and examine its returned values. (C) one of the event procedures that a user can invoke. (D) never to be used. B
7.
A program is said to be _________________ if it meets modern standards of program design. (A) top-down (B) logical (C) modular (D) structured D
8.
A(n) __________ is an encapsulation of data and code that operates on the data. (A) object (B) driver (C) decision (D) module A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
One advantage to using top-down design in writing a program is that the program can later be modified with less of a chance of introducing new errors. (T/F) T
10. "Stub programming" is one method of testing and debugging the key event procedures in a program. (T/F) T 11. Studies have shown that despite the many benefits of structured programming, it usually takes a longer amount of time when used on large programming projects. (T/F) F 12. Debugging an unstructured program often requires the programmer to look at the entire program in order to make even a simple modification. (T/F) T 13. With top-down design, higher-level modules control the flow of the program while lower level modules do the actual work. (T/F) T 14. Modular design decreases the programmer’s productivity. (T/F) F 15. Most modern programmers use a blend of traditional structured programming along with object-oriented design. (T/F) T 16. Visual Basic 2012 is an object-oriented language. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 6 Repetition Section 6.1 Do Loops 1.
What is wrong with the following Do loop? Dim index As Integer = 1 Do While index <> 9 lstBox.Items.Add("Hello") index += 1 Loop
(A) The test variable should not be changed inside a Do loop. (B) The test condition will never be true. (C) This is an infinite loop. (D) Nothing D 2.
What numbers will be displayed in the list box when the button is clicked? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim num as Double = 10 Do While num > 1 lstBox.Items.Add(num) num = num - 3 Loop End Sub
(A) 10, 7, and 4 (B) 10, 7, 4, and 1 (C) 10, 7, 4, 1, and -2 (D) No output A 3.
Which While statement is equivalent to Until num < 100? (A) While num <= 100 (B) While num > 100 (C) While num >= 100 (D) There is no equivalent While statement. C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4.
What is wrong with the following Do loop? Dim index As Integer = 1 Do While index <> 10 lstBox.Items.Add("Hello") index += 2 Loop
(A) It should have been written as a Do Until loop. (B) It is an infinite loop. (C) The test variable should not be changed within the loop itself. (D) Nothing B 5.
In analyzing the solution to a program, you conclude that you want to construct a loop so that the loop terminates either when (a < 12) or when (b = 16). Using a Do loop, the test condition should be (A) Do While (a > 12) Or (b <> 16) (B) Do While (a >= 12) Or (b <> 16) (C) Do While (a < 12) Or (b <> 16) (D) Do While (a >= 12) And (b <> 16) (E) Do While (a < 12) And (b = 16) D
6.
When Visual Basic executes a Do While loop it first checks the truth value of the _________. (A) pass (B) loop (C) condition (D) statement C
7.
If the loop is to be executed at least once, the condition should be checked at the __________. (A) top of the loop (B) middle of the loop (C) bottom of the loop (D) Nothing should be checked. C
8.
A Do While loop checks the While condition before executing the statements in the loop. (T/F) T
9.
If the While condition in a Do While loop is false the first time it is encountered, the statements in the loop are still executed once. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. The following statement is valid. (T/F) Do While x <> 0
T 11. The following two sets of code produce the same output. (T/F) Dim num As Integer = 1 Do While num <=5 lstBox.Items.Add("Hello") num += 1 Loop
Dim num As Integer = 1 Do lstBox.Items.Add("Hello") num += 1 Loop Until (num > 5)
T 12. A loop written using the structure Do While...Loop can usually be rewritten using the structure Do...Loop Until. (T/F) T 13. A variable declared inside a Do loop cannot be referred to outside of the loop. (T/F) T 14. Assume that i and last are Integer variables. Describe precisely the output produced by the following segment for the inputs 4 and –2. Dim last, i As Integer last = CInt(InputBox("Enter terminating value:")) i = 0 Do While (i <= last) lstBox.Items.Add(i) i += 1 Loop (Input 4): 0 1 2 3 4
(Input –2): No output 15. The following is an infinite loop. Rearrange the statements so that the loop will terminate as intended. x = 0 Do lstBox.Items.Add(x) Loop Until x > 13 x += 2
Move the last statement one line up, before the Loop Until statement
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
16. What is wrong with the following simple password program where today's password is "intrepid"? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim password As String password = InputBox("Enter today's password:") Do lstBox.Items.Add("Incorrect") password = InputBox("Enter today's password:") Loop Until password = "intrepid" lstBox.Items.Add("Password Correct. You may continue.") End Sub
(A) There is no way to re-enter a failed password. (B) The Loop Until condition should be passWord <> "intrepid". (C) It will display "Incorrect." even if the first response is "intrepid". (D) Nothing C 17. How many times will HI be displayed when the following lines are executed? Dim c As Integer = 12 Do lstBox.Items.Add("HI") c += 3 Loop Until (c >= 30)
(A) 5 (B) 9 (C) 6 (D) 4 (E) 10 C 18. In the following code segment, what type of variable is counter? Dim temp, counter, check As Integer Do temp = CInt(InputBox("Enter a number.")) counter += temp If counter = 10 Then check = 0 End If Loop Until (check = 0)
(A) counter (B) accumulator (C) sentinel (D) loop control variable B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
19. What numbers will be displayed in the list box by the following code when the button is clicked? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim num As Integer = 7 Do num += 1 lstBox.Items.Add(num) Loop Until (num > 6) lstBox.Items.Add(num) End Sub
(A) 7 (B) 8 (C) 7 and 8 (D) 8 and 8 D 20. __________ calculate the number of elements in lists. (A) Sentinels (B) Counter variables (C) Accumulators (D) Nested loops B 21. _________ calculate the sums of numerical values in lists. (A) Sentinels (B) Counter variables (C) Accumulator variables (D) Nested loops C 22. The following are equivalent While and Until statements. (T/F) While (num > 2) And (num < 5) Until (num <= 2) Or (num >= 5)
T 23. A Do…Loop Until block is always executed at least once. (T/F) T 24. A counter variable is normally incremented or decremented by 1. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
25. The following program uses a counter variable to force the loop to end. (T/F) Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim r As Double = 1 Dim t As Double = 0 Do While (t < 5000) t = 2 * t + r r += 1 lstBox.Items.Add(t) Loop End Sub
F Section 6.2 For…Next Loops 1.
When the number of repetitions needed for a set of instructions is known before they are executed in a program, the best repetition structure to use is a(n) (A) Do While...Loop structure. (B) Do...Loop Until structure. (C) For...Next loop. (D) If block with a GoTo statement. C
2.
What is one drawback in using non-integer Step sizes? (A) Round-off errors may cause unpredictable results. (B) Decimal Step sizes are invalid in Visual Basic. (C) A decimal Step size is never needed. (D) Decimal Step sizes usually produce infinite loops. A
3.
When the odd numbers are added successively, any finite sum will be a perfect square (e.g., 1 + 3 + 5 = 9 and 9 = 3^2). What change must be made in the following program to correctly demonstrate this fact for the first few odd numbers? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim oddNumber As Integer Dim sum As Integer = 0 For i As Integer = 1 To 9 Step 2 'Generate first few odd numbers oddNumber = i For j As Integer = 1 To oddNumber Step 2 'Add odd numbers sum += j Next lstBox.Items.Add(sum & " is a perfect square.") Next End Sub
(A) Change the Step size to 1 in the first For statement. (B) Move oddNumber = i inside the second For loop. (C) Reset sum to zero immediately before the second Next statement. (D) Reset sum to zero immediately before the first Next statement. C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4.
What does the following program do with a person's name? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim name, test As String Dim n As String = "" name = InputBox("Enter your first name:") For i As Integer = 0 To name.Length - 1 Step 2 test = name.Substring(i, 1) n = test & n Next txtBox.Text = n End Sub
It displays the name (A) in reverse order. (B) in reverse order and skips every other letter. (C) as it was entered. (D) as it was entered, but skips every other letter. B 5.
Suppose the days of the year are numbered from 1 to 365 and January 1 falls on a Tuesday as it did in 2013. What is the correct For statement to use if you want only the numbers for the Fridays in 2013? (A) For i As Integer = 3 to 365 Step 7 (B) For i As Integer = 1 to 365 Step 3 (C) For i As Integer = 365 To 1 Step -7 (D) For i As Integer = 3 To 365 Step 6 A
6.
Given the following partial program, how many times will the statement lstBox.Items.Add(j + k + m) be executed? For j As Integer = 1 To 4 For k As Integer = 1 To 3 For m As Integer = 2 To 10 Step 3 lstBox.Items.Add(j + k + m) Next Next Next
(A) 24 (B) 60 (C) 36 (D) 10 (E) None of the above C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
7.
Which of the following program segments will sum the eight numbers input by the user? (A) For k As Integer = 1 To 8 s = CDbl(InputBox("Enter a number.") s += k Next (B) For k As Integer = 1 To 8 a = CDbl(InputBox("Enter a number.") s += 1 Next (C) For k As Integer = 1 To 8 a = CDbl(InputBox("Enter a number.") a += s Next (D) For k As Integer = 1 To 8 a = CDbl(InputBox("Enter a number.") s += a Next
D 8.
What will be displayed by the following program when the button is clicked? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim a As String, n, c As Integer a = "HIGHBACK" n = CInt(Int(a.Length / 2)) c = 0 For k As Integer = 0 To n – 1 If a.Substring(k, 1) > a.Substring(7 – k, 1) Then c += 1 End If Next txtBox.Text = CStr(c) End Sub
(A) 1 (B) 2 (C) 3 (D) 4 (E) 5 C 9.
In a For statement of the form shown below, what is the default step value when the "Step c" clause is omitted? For i As Integer = a To b Step c
(A) the same as a (B) the same as b (C) 0 (D) 1 D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. What will be displayed when the following lines are executed? txtBox.Clear() For k As Integer = 1 To 3 txtBox.Text &= "ABCD".Substring(4 – k, 1) Next
(A) ABC (B) CBA (C) DBA (D) DCBA (E) DCB E 11. Which loop computes the sum of 1/2 + 2/3 + 3/4 + 4/5 + … + 99/100? (A) For n As Integer = 1 To 99 s += n / (1 + n) Next (B) For q As Integer = 100 To 1 s += (q + 1) /q Next (C) For d As Integer = 2 To 99 s = 1 / d + d / (d + 1) Next (D) For x As Integer = 1 To 100 s += 1 / (x + 1) Next
A 12. How many times will PETE be displayed when the following lines are executed? For c As Integer = 15 to -4 Step -6 lstBox.Items.Add("PETE") Next
(A) 1 (B) 2 (C) 3 (D) 4 D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
13. What will be displayed by the following program when the button is clicked? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim s As Double s = 0 For k As Integer = 1 To 5 If k / 2 = Int(k / 2) Then s += k End If Next txtBox.Text = CStr(s) End Sub
(A) 12 (B) 9 (C) 15 (D) 6 D 14. How many lines of output are produced by the following program segment? For i As Integer = 1 To 3 For j As Integer = 1 To 3 For k As Integer = i to j lstBox.Items.Add("Programming is fun.") Next Next Next
(A) 8 (B) 9 (C) 10 (D) 11 C 15. Assuming the following statement, what is the For...Next loop's counter variable? For yr As Integer = 1 To 5
(A) 1 (B) 5 (C) To (D) yr D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
16. What is the value of j after the end of the following code segment? For j As Integer = 1 to 23 lstBox.Items.Add("The counter value is " & j) Next
(A) 22 (B) 23 (C) 24 (D) j no longer exists D 17. A For...Next loop with a positive step value continues to execute until what condition is met? (A) The counter variable is greater than the terminating value. (B) The counter variable is equal to or greater than the terminating value. (C) The counter variable is less than the terminating value. (D) The counter variable is less than or equal to the terminating value. A 18. Which of the following loops will always be executed at least once when it is encountered? (A) a For...Next loop (B) a Do loop having posttest form (C) a Do loop having pretest form (D) none of the above. B 19. Which of the following are valid for an initial or terminating value of a Fir...Next loop? (A) a numeric literal (B) info.Length, where info is a string variable (C) a numeric expression (D) All of the above D 20. What is the data type of the variable num if Option Infer is set to On and the statement Dim num = 7.0 is executed? (A) Integer (B) Boolean (C) Double (D) String C 21. The value of the counter variable should not be altered within the body of a For…Next loop. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
22. The body of a For...Next loop in Visual Basic will always be executed once no matter what the initial and terminating values are. (T/F) F 23. If the terminating value of a For...Next loop is less than the initial value, then the body of the loop is never executed. (T/F) F 24. If one For...Next loop begins inside another For...Next loop, it must also end within this loop. (T/F) T 25. The value of the counter variable in a For...Next loop need not be a whole number. (T/F) T 26. One must always have a Next statement paired with a For statement. (T/F) T 27. If the initial value is greater than the terminating value in a For...Next loop, the statements within are still executed one time. (T/F) F 28. When one For...Next loop is contained within another, the name of the counter variable for each For...Next loop may be the same. (T/F) F 29. A For...Next loop cannot be nested inside a Do loop. (T/F) F 30. The following code segment is valid. (T/F) If (firstLetter > "A") Then For x As Integer = 1 to 100 lstBox.Items.Add(x) Next End If
T 31. In a For...Next loop, the initial value should be greater than the terminating value if a negative step is used and the body of the loop is to be executed at least once. (T/F) T 32. If the counter variable of a For...Next loop will assume values that are not whole numbers, then the variable should not be of type Integer. (T/F) T 33. The step value of a For...Next loop can be given by a numeric literal, variable, or expression. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
34. The variable index declared with the statement For index As Integer = 0 To 5 cannot be referred to outside of the For…Next loop. (T/F) T 35. When Option Infer is set to On, a statement of the form Dim num = 7 is valid. (T/F) T 36. What is displayed in the text box when the button is clicked? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim word As String = "Alphabet" Dim abbreviation As String = "" For i As Integer = 0 To word.Length - 1 If word.Substring(i, 1).ToUpper <> "A" Then abbreviation &= word.Substring(i, 1) End If Next txtBox.Text = abbreviation End Sub
lphbet Section 6.3 List Boxes and Loops 1. Which of the following expressions refers to the contents of the last row of the list box? (A) lstBox.Items(lstBox.Items.Count) (B) lstBox.Items(lstBox.Items.Count - 1) (C) lstBox.Items(Count) (D) lstBox.Items.Count B 2. Which of the following expressions refers to the contents of the first row of the list box? (A) lstBox.Items(0) (B) lstBox.Items(1) (C) lstBox.Items.First (D) lstBox.Items(First) A 3. Each item in a list box is identified by an index number; the first item in the list is assigned which of the following values as an index? (A) a randomly assigned value (B) 1. (C) a value initially designated by the programmer (D) 0 D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4. The following lines of code display all the items of lstBox. (T/F) For n As Integer = 1 to lstBox.Items.Count lstBox2.Items.Add(lstBox.Items(n)) Next
F 5. The number of items in ListBox1 is ListBox1.Items.Count. (T/F) T 6. If no item in a list box is selected, the value of lstBox.SelectedIndex is 0. (T/F) F 7. Which of the statements will unhighlight any highlighted item in lstBox? (A) lstBox.SelectedItem = Nothing (B) lstBox.SelectedItem = "" (C) lstBox.SelectedIndex = 0 (D) lstBox.SelectedIndex = -1 D 8. A list box named lstBox has its Sorted property set to True and contains the three items Cat, Dog, and Gnu in its Items collection. If the word Elephant is added to the Items collection at run time, what will be its index value? (A) 2 (B) 1 (C) 3 (D) 0 A 9. A list box named lstBox has its Sorted property set to False and contains the three items Cat, Dog, and Gnu in its list. If the word Elephant is added to the list at run time, what will be its index value? (A) 2 (B) 1 (C) 3 (D) 0 C 10. If a program contains procedures for both the Click and DoubleClick events on a list box and the user double-clicks on the list box, only the Click event will be raised. (T/F) T 11. If a list box has its sorted property set to True and the list box contains all numbers, then the values in the list box will always be in increasing numerical order. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
12. If a list box contains all numbers, then which of the following values can be calculated without using a loop? (A) average value of the numbers (B) largest number (C) smallest number (D) number of numbers D 13. Which of the following is not a main type of event that can be raised by user selections of items in a list box? (A) Click event (B) SelectedIndexChanged event (C) FillDown event (D) DoubleClick event C 14. The value of lstBox.Items(n) is the nth item in the list box. (T/F) F 15. The sorted property can only be set to True at design time. (T/F) F 16. The _____________ data type is most suited to a flag. (A) Boolean (B) Integer (C) String (D) Double A 17. A variable that keeps track of whether a certain situation has occurred is called (A) a counter. (B) an accumulator. (C) a switch. (D) a flag. D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 7 Arrays Section 7.1 Creating and Accessing Arrays 1.
After the following Dim statement is executed, how many elements will the array myVar have? Dim myVar(7) As Double
(A) 0 (B) 1 (C) 8 (D) 9 C 2.
In the line of code Dim scores() As Integer = {55, 33, 12}
the upper bound of the array scores is which of the following? (A) 2 (B) 1 (C) 11 (D) 0 A 3.
Each individual variable in the list student(0), student(1), student(2)
is known as a(n) (A) subscript. (B) dimension. (C) element. (D) type. C 4.
In the statement Dim scores(30) As Double
the number 30 designates which of the following? (A) the highest value of the subscripts of the elements for the array scores (B) the maximum value that can be assigned to any element in the array scores (C) the data type for the array scores (D) the value initially assigned to each element in the array scores A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
5.
Which statement is true regarding the following Dim statement? Dim states(49) As String, populations(49) As Double
(A) It is invalid since more than one array is dimensioned by a single Dim statement. (B) It is invalid since the two arrays must have the same data type. (C) The subscripts of states range from 1 to 49. (D) The subscripts of populations range from 0 To 49. D 6.
The Count method returns what information about an array? (A) the highest number that can be used as a subscript for the array (B) the largest value that can be assigned to an array element (C) the number of elements in the array (D)tThe highest dimension of the array C
7.
In the line of code For index As Integer = 0 to (score.Count - 1)
the Count method is used to carry out which of the following tasks? (A) determine the largest value for each of the elements (B) determine the largest subscript in the array (C) determine the smallest value for each of the elements (D) declare a new array with the name Count B 8.
The ReDim statement causes an array to lose its current contents unless the word ReDim is followed by the keyword (A) CInt (B) MyBase (C) Preserve (D) Add C
9.
Like other variables, array variables can be declared and assigned initial values at the same time. (T/F) T
10. After an array has been declared, its type (but not its size) can be changed with a ReDim statement. (T/F) F 11. If you use the ReDim statement to make an array smaller than it was, data in the eliminated elements can be retrieved by using the Preserve keyword. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
12. What will be the size of the array stones after the following two lines of code are executed? Dim stones() As String = {"Watts", "Jagger", "Wood", "Richards"} ReDim Preserve stones(10)
11 13. The statement Dim newlist(10) As String
is used to declare an array where each element has the value 10. (T/F) F 14. In the line of code Function Sum(scores() As Integer) As Integer
the pair of parentheses that follows scores can be removed. (T/F) F 15. In the line of code Dim scores() As Integer = {55, 33, 12}
the upper bound of the array scores is 12. (T/F) F 16. What two names are displayed in the list box when the button is clicked on? Dim krispies() as String Private Sub frmCereal_Load(...) Handles MyBase.Load krispies(0) = "snap" krispies(1) = "crackle" krispies(2) = "pop" End Sub Private Sub btnDisplay_Click(...) Handles btnDisplay.Click lstBox.Items.Add(krispies.Max) lstBox.Items.Add(krispies.Last) End Sub
(A) crackle and pop (B) crackle and snap (C) snap and crackle (D) snap and pop D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
17. What two numbers are displayed in the list box when the button is clicked on? Dim nums() as Integer Private Sub frmNumbers_Load(...) Handles MyBase.Load nums(0) = 5 nums(1) = 3 nums(2) = 4 End Sub Private Sub btnDisplay_Click(...) Handles btnDisplay.Click lstBox.Items.Add(nums.Average) lstBox.Items.Add(nums.Max) End Sub
(A) 4 and 5 (B) 4 and 4 (C) 3 and 5 (D) 3 and 4 A 18. Either a For...Next loop or a For Each loop can be used to display every other value from an array in a list box. (T/F) F 19. An array can contain both numeric and string values. (T/F) F 20. A Function procedure can return a number, but cannot return an array of numbers. (T/F) F 21. What names are displayed in the list box when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim names() As String = IO.File.ReadAllLines("Data.txt") lstBox.Items.Clear() For i As Integer = (names.Count - 1) To 0 Step -2 lstBox.Items.Add(names(i)) Next End Sub
Assume the five lines of the file Data.txt contain the following entries: Bach, Borodin, Brahms, Beethoven, Britain. (A) Bach, Brahms, and Britain (B) Britain, Beethoven, Brahms, Borodin, and Bach (C) Bach, Borodin, Brahms, Beethoven, and Britain (D) Britain, Brahms, and Bach D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
22. What names are displayed in the list box when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim file As String = "Ships.txt" Dim ships() As String = FillArray(file) lstBox.Items.Add(ships(2)) lstBox.Items.Add(ships.Min) End Sub Function FillArray(file As String) As String() Dim names() As String = IO.File.ReadAllLines(file) Return names End Function
Assume the three lines of the file Ships.txt contain the following entries: Pinta, Nina, Santa Maria. (A) Pinta and Nina (B) Santa Maria and Pinta (C) Nina and Santa Maria (D) Santa Maria and Nina D 23. What numbers are displayed in the list box when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim file As String = "Beatles.txt" Dim fabFour() As String = FillArray(file) lstBox.Items.Add(Array.IndexOf(fabFour, "Ringo") lstBox.Items.Add(fabFour.Count - 1) End Sub Function FillArray(file As String) As String() Dim names() As String = IO.File.ReadAllLines(file) Return names End Function
Assume the four lines of the file Beatles.txt contain the following entries: John, Paul, Ringo, George. (A) 3 and 3 (B) 3 and 4 (C) 2 and 3 (D) 2 and 4 C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
24. What names are displayed in the list box by the following program segment? Dim newYork As String = "Manhatten,Bronx,Brooklyn,Queens,Staten Island" Dim boroughs() As String = newYork.Split(","c) lstBox.Items.Add(boroughs(0)) lstBox.Items.Add(borought.Min)
(A) Brooklyn and Queens (B) Manhatten and Staten Island (C) Bronx and Manhatten (D) Manhatten and Bronx D 25. What numbers are displayed in the list box by the following program segment? Dim numbers As String = "1492,1776,1945" Dim temp() As String = numbers.Split(","c) Dim nums(2) As Integer For i As Integer = 0 to 2 nums(i) = CInt(temp(i)) Next lstBox.Items.Add(nums(1)) lstBox.Items.Add(nums.First)
(A) 1776 and 1492 (B) 1776 and 1776 (C) 1492 and 1492 (D) 1945 and 1492 A 26. What is the output of the following program segment? Dim nums(8) As Integer Dim total As Double = 0 For i As Integer = 0 To 8 nums(i) = i Next For k As Integer = 1 To 4 total += 10 ^ nums(k) Next txtBox.Text = CStr(total)
(A) 10000 (B) 11110 (C) 1110 (D) 0 B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
27. What will be displayed when the following program segment is executed? Dim temp() As String = IO.File.ReadAllLines("Data.txt") Dim n As Integer = temp.Count - 1 Dim a(n) As Double For k As Integer = 0 To n a(k) = CDbl(temp(i)) Next txtBox.Text = CStr(a(3))
Assume the five rows of the file Data.txt contain the following entries: 3, 2, 5, 1, 4. (A) 1 (B) 2 (C) 3 (D) 4 A 28. What is the output of the following program segment? Dim temp() As String = IO.File.ReadAllLines("Data.txt") Dim n As Integer = temp.Count - 1 Dim numbers(n) As Double, h As Double = 0 For i As Integer = 0 To n numbers(i) = CDbl(temp(i)) Next For k As Integer = 0 to n h += numbers(k) Next txtBox.Text = CStr(h)
Assume the four rows of the file Data.txt contain the following entries: 2, 4, 2, 3 (A) 11 (B) 2 (C) 7 (D) 4 A 29. Given the Dim statement below, which set of statements will initialize all elements of myArray to 100? Dim myArray(100) As Double
(A) myArray = 100 (B) For i As Integer = 0 To 100 (i) = 100 Next (C) For j As Integer = 0 to 100 myArray(j) = 100 Next
(D) myArray() is already initialized to 100 by the Dim statement. C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
30. What is the output of the following program segment? Dim c As Double Dim temp() As String = IO.File.ReadAllLines("Data1.txt") Dim n As Integer = temp.Count - 1 Dim a(n) As Double For i As Integer = 0 To n a(i) = CDbl(temp(i)) Next temp() = IO.File.ReadAllLines("Data2.txt") Dim b(n) As Double For i As Integer = 0 To n b(i) = CDbl(temp(i)) Next For k As Integer = 0 To n If a(k) = b(k) Then c += 1 End If Next lstBox.Items.Add(c)
Assume the twenty rows of the file Data1.txt contain the following entries: 3, 2, 5, 1, 7, 8, 3, 5, 6, 2, 3, 6, 1, 6, 5, 5, 7, 2, 5, 3. Assume the twenty rows of the file Data2.txt contain the following entries: 5, 3, 3, 4, 8, 2, 3, 5, 9, 5, 3, 7, 3, 7, 6, 3, 2, 1, 3, 4. (A) 2 (B) 3 (C) 4 (D) 5 (E) 6 B 31. What is displayed in the message box by the following code? Dim message As String Dim teamNames() As String = {"Packers", "Jets", "Seahawks"} ReDim Preserve teamName(1) message = teamNames(1) MessageBox.Show(message)
(A) Packers (B) Jets (C) Seahawks (D) 2 B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
32. If the following statement appears in a program, the array scores must have been declared using the String data type. (T/F) scores(1) = 87
F 33. Consider the following Dim and assignment statements for myArray(). The assignment statement will cause a "Subscript out of range" error. (T/F) Dim myArray(50) As Double myArray(34) = 51
F 34. Unless otherwise specified, Visual Basic assigns the value 0 to each element of a numeric array when it is declared with a Dim statement. (T/F) T 35. The following pair of statement is valid. (T/F) x = CInt(InputBox("Enter number of items (must be a positive integer)")) ReDim myArray(x - 1)
T 36. A fractional number such as 4.6 is not allowed as a subscript. (T/F) T 37. Consider the following Visual Basic statements: Dim nums(4) As Double For index As Integer = 0 To 4 nums(index) = 1 + (index * 2) Next
What values are placed in the array by the above statements? (Show each element of the array and the value for that element.) nums(0)=1, nums(1)=3, nums(2)=5, nums(3)=7, nums(4)=9
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
38. What is the output of the following program when the button is clicked on? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim result As Double Dim number(4) As Double FillArray(number) result = SumArray(number) txtBox.Text = CStr(result) End Sub Sub FillArray(ByRef anyArray() As Double) Dim temp() As String = IO.File.ReadAllLines("Data.txt") For i As Integer = 0 To 4 anyArray(i) = CDbl(temp(i)) Next End Sub Function SumArray(anyArray() As Double) As Double Dim total As Double total = 0 For i As Integer = 0 To 4 Step 2 total += anyArray(i) Next Return total End Function
Assume the five rows of the file Data.txt contain the following entries: 1, 3, 5, 7, 9 (A) 0 (B) 25 (C) 15 (D) None of the above C 39. Which of the tasks is the Join function used to carry out in the following statement? Dim line As String line = Join(strArrData, ",")
(A) Join concatenates the values of all elements of the array strArrData, and adds a comma delimiter between successive values. (B) Join concatenates the values of all elements of line, and adds a comma to the end of the line. (C) Join parses or separates out all items of text that are delimited by a comma in strArrData. (D) Join parses or separates out all items of text that are delimited by a comma in line. A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Section 7.2 Using LINQ with Arrays 1.
What names are displayed in the list box by the following lines of code? Dim oceans() As String = {"Atlantic", "Pacific", "Indian", "Arctic", "Antartic"} Dim query = From ocean in oceans Where ocean.Length = 6 Select ocean For Each ocean As String In query lstBox.Items.Add(ocean) Next
(A) Pacific and Indian (B) Indian and Arctic (C) Indian (D) Atlantic and Pacific B 2.
What numbers are displayed in the list box by the following program segment? Dim numbers() As Integer = {4, 7, 9, 3, 1} Dim query = From number in numbers Where number > 6 Select number lstBox.Items.Add(query.Count) lstBox.Items.Add(query.Average)
(A) 5 and 12 (B) 2 and 12 (C) 2 and 8 (D) 5 and 8 C 3.
What numbers are displayed in the list box by the following program segment? Dim numbers() As Integer = {4, 7, 9, 3, 1, 9, 7} Dim query = From number in numbers Where number > 6 Select number lstBox.Items.Add(query.Count) lstBox.Items.Add(query.Average)
(A) 7 and 12 (B) 4 and 8 (C) 2 and 12 (D) 7 and 8 B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4.
What numbers are displayed in the list box by the following program segment? Dim numbers() As Integer = {4, 7, 9, 3, 1, 7, 7} Dim query = From number in numbers Where number > 6 Select number Distinct lstBox.Items.Add(query.Count) lstBox.Items.Add(query.Average)
(A) 5 and 12 (B) 2 and 12 (C) 2 and 8 (D) 5 and 8 C 5.
What states are displayed in the list box by the following program segment? Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"} Dim query = From state in states Where ContainsE(state) Select state For Each state in query lstBox.Items.Add(state) Next Function ContainsE(word As String) As Boolean If word.IndexOf("E") <> -1 Or word.IndexOf("e") <> -1 Then Return True Else Return False End If End Function
(A) Colorado (B) New Mexico (C) Colorado, New Mexico, Arizona, Utah (D) No states B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
What states are displayed in the list box by the following program segment? Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"} Dim query = From state in states Where state.length < 5 Select state.ToUpper For Each state in query lstBox.Items.Add(state) Next
(A) Utah (B) COLORADO, NEW MEXICO, ARIZONA, UTAH (C) UTAH (D) No states C 7.
What numbers are displayed in the list box by the following program segment? Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"} Dim query = From state in states Where state.EndsWith("o") Select state.Length For Each number in query lstBox.Items.Add(number) Next
(A) 8 and 10 (B) 8, 10, 7, 4 (C) 8 (D) 29 A 8.
What names are displayed in the list box by the following program segment? Dim tonightShow() As String = {"Allen", "Parr", "Carson", "Leno", "O'Brien", "Leno"} Dim query = From host in tonightShow Where host.Length = 4 Select host Distinct For Each host in query lstBox.Items.Add(host) Next
(A) Parr, Leno, Leno (B) Parr, Leno (C) Leno (D) No names B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
What numbers are displayed in the list box by the following program segment? Dim numbers() As Double = {.5, 1, 2, 2.5} Dim query = From number in numbers Where number < 2 Let FormattedPer = number.ToString("P") Select FormattedPer For Each percent In query lstBox.Items.Add(percent) Next
(A) .5 and 1 (B) 50% and 100% (C) 50.00%, 100.00%, 200.00%, 250.00% (D) 50.00% and 100.00% D 10. What years are displayed in the list box by the following program segment? Dim years() As Integer = {1492, 1776, 1840, 1929, 1945, 2005} Dim query = From year in years Where Is20thCentury(year) Select year For Each year in query lstBox.Items.Add(year) Next Function Is20thCentury(num As Integer) As Boolean If (num >= 1900) and (num < 2000) Then Return True Else Return False End IF End Function
(A) 1929 and 1945 (B) 1929 (C) 1492, 1776, 1840, 1929, 1945, 2005 (D) No years A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
11. What states are displayed in the list box by the following program segment? Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"} Dim query = From state in states Order By state Ascending Select state lstBox.Items.Add(query.First) lstBox.Items.Add(query.Min)
(A) Arizona and Colorado (B) Arizona and Utah (C) Colorado and Arizona (D) Arizona and Arizona D 12. What words are displayed in the list box by the following program segment? Dim deadlySins() As String = {"pride", "greed", "anger", "envy", "lust", "gluttony", "sloth"} Dim query = From sin in deadlySins Order By sin Ascending Where sin.StartsWith("g") Select sin lstBox.Items.Add(query.First) lstBox.Items.Add(query.Max)
(A) gluttony and greed (B) gluttony and gluttony (C) pride and gluttony (D) greed and greed A 13. What words are displayed in the list box by the following program segment? Dim deadlySins() As String = {"pride", "greed", "anger", "envy", "lust", "gluttony", "sloth"} Dim query = From sin in deadlySins Order By sin.Length Descending Select sin.ToUpper lstBox.Items.Add(query.First) lstBox.Items.Add(query.Min)
(A) GLUTTONY and GLUTTONY (B) GLUTTONY and SLOTH (C) GLUTTONY and ANGER (D) PRIDE and ENVY C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
14. What colleges are displayed in the list box by the following program segment? Dim ivies() As String = {"Harvard", "Princeton", "Yale", "Dartmouth", "Brown", "Columbia", "Univ. of PA", "Cornell"} Dim query = From college in ivies Order By college Descending Select college lstBox.Items.Add(query.First) lstBox.Items.Add(query.Min)
(A) Yale and Brown (B) Yale and Yale (C) Brown and Brown (D) Harvard and Brown A 15. What colleges are displayed in the list box by the following program segment? Dim ivies() As String = {"Harvard", "Princeton", "Yale", "Dartmouth", "Brown", "Columbia", "Univ. of PA", "Cornell"} Dim query = From college in ivies Where college.Length <= 9 Order By college.Length Descending, college Descending Select college lstBox.Items.Add(query.Last) lstBox.Items.Add(query.Min)
(A) Dartmouth and Princeton (B) Yale and Brown (C) Yale and Cornell (D) Dartmouth and Yale B 16. What colleges are displayed in the list box by the following program segment? Dim ivies() As String = {"Harvard", "Princeton", "Yale", "Dartmouth", "Brown", "Columbia", "Univ. of PA", "Cornell"} Dim query = From college in ivies Where college.Length <= 9 Order By college.Length Descending, college Ascending Select college lstBox.Items.Add(query.First) lstBox.Items.Add(query.Max)
(A) Dartmouth and Princeton (B) Yale and Brown (C) Yale and Cornell (D) Dartmouth and Yale D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
17. What words are displayed in the list box by the following program segment? Dim dimensions() As String = {"width", "height", "depth"} Dim query = From dimension in dimensions Order By dimension.Length Descending, dimension Ascending Select dimension lstBox.DataSource = query.ToList lstBox.SelectedIndex = Nothing
(A) width, height, depth (B) height, depth, width (C) height, width, depth (D) depth, width, height B 18. What numbers are displayed in the list box by the following program segment? Dim numbers() As Integer = {5, 79, 8, 33, 27} Dim query = From number in numbers Let formattedNum = number.ToString("N0") Order By formattedNum Ascending Select formattedNum lstBox.DataSource = query.ToList lstBox.SelectedIndex = Nothing
(A) 5, 8, 27, 33, 79 (B) 79, 33, 27, 8, 5 (C) 27, 33, 5, 79, 8 (D) 5, 79, 8, 33, 27 C 19. In an ascending ordered array, the value of each element is (A) less than or equal to the value of the next element. (B) greater than or equal to the value of the next element. (C) equal to the value of the next element. (D) greater than the value of the next element. A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
20. Which of the following is NOT an example of an ordered array? (A) years 1877
1944
2011
4301
Wapato
Yakima
Zillah
Brush Prairie
Dollars Corner
Battle Ground
457
457
458
(B) cities Selah (C) nbrhoods Hockinson (D) nums 457 C 21. Which of the following is an ordered descending array? (A) years 1011
1112
1112
1377
Wapato
Yakima
Zillah
Dollars Corner
Battle Ground
457
458
(B) cities Selah (C) nbrhoods Parkrose
Hockinson
(D) nums 459
457 C
22. Arrays are said to be ordered only if the values are in ascending order. (T/F) F 23. Searching successive elements of an ordered list beginning with the first element is known as a binary search. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Section 7.3 Some Additional Types of Arrays 1.
Consider the following structure definition. Which Dim statement would correctly declare an array of this structure for elements having subscripts from 0 through 30? Structure carType Dim yr As Integer Dim make As String Dim model As String End Structure
(A) You cannot have an array of structures. (B) Dim carType(30) (C) Dim car(30) As carType (D) Dim carType(30) As car C 2.
Consider the following Structure definition and declaration. Which assignment statement would correctly record that player number 13 had three home runs so far this season? Structure playerType Dim fullname As String Dim team As String Dim position As String Dim homerun As Double Dim average As Double Dim rbi As Double End Structure Dim player(45) As playerType
(A) player(13) = 3 (B) player(13).homerun(3) (C) playerType(13).homerun = 3 (D) player(13).homerun = 3 (E) None of the above D 3.
Suppose a structure is created with the code Structure stateUSA Dim capCity As String Dim stateNum As Integer End Structure
in the Declarations section of the Code Editor. Which of the following statements correctly declares a variable of type stateUSA? (A) Dim stateUSA As stateWA (B) Dim stateWA As Structure (C) Dim stateWA As stateUSA (D) Dim stateWA As Member C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4.
Suppose a structure is created with the code Structure stateUSA Dim capCity As String Dim stateNum As Integer End Structure
in the Declarations section of the Code window. The code Dim stateWA As stateUSA Dim message As String stateWA.stateNum = 42 stateWA.capCity = "Olympia" message = stateWA.capCity & " " & stateWA.stateNum MessageBox.Show(message)
is placed in the Click event procedure for one of the program’s buttons. What output will be displayed in the message box when the button is clicked on? (A) 42 Olympia (B) Olympia42 (C) 42Olympia (D) Olympia 42 D 5.
The members of a structure must all be of the same data type. (T/F) F
6.
Suppose a structure is created with the code Structure stateUSA Dim capCity As String Dim yearFounded As Integer End Structure
in the Declarations section of the Code window. In this structure, the variable capCity is known as a member. (T/F) T 7.
Suppose a structure is created with the code Structure stateUSA Dim capCity As String Dim stateNum As Integer End Structure
in the Declarations section of the Code window. The following statement correctly makes use of the stateNum member of this structure. (T/F) Dim stateWA As stateUSA StateWA.stateNum = 42
T 8.
A structure can contain members that are simple variables only; members cannot be arrays. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
When a variable with a user-defined data type is passed to a procedure, the corresponding parameter in the Sub or Function statement must be declared to be of the same data type. (T/F) T
10. Suppose a structure and an array are created with the code Structure Nation Dim name As String Dim continent As String Dim population As Double 'in millions Dim area As Double 'in square miles End Structure Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192 member nations of the UN. What will be displayed in the list box when the following code is executed? Dim query = From country In nations Where country.continent = "Africa" Order By country.population Descending Select country.name lstBox.DataSource = query.ToList lstBox.SelectedIndex = Nothing
(A) the names of the countries in Africa in alphabetical order (B) the names of the countries in Africa beginning with the name of the least populous country (C) the names of the countries in Africa beginning with the name of the most populous country (D) the names of the countries in Africa in reverse alphabetical order C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
11. Suppose a structure and an array are created with the code Structure Nation Dim name As String Dim continent As String Dim population As Double 'in millions Dim area As Double 'in square miles End Structure Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192 member nations of the UN. What will be displayed in the list box when the following code is executed? Dim query = From country In nations Order By country.area Descending Select country.name, country.population lstBox.Items.Add(query.First.name) lstBox.Items.Add(1000000 * query.First.population)
(A) the name and population of the smallest country in the UN (B) the name and population of the largest country in the UN (C) the name and population of the least populous country in the UN (D) the name and population of the most populous country in the UN B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
12. Suppose a structure and an array are created with the code Structure Nation Dim name As String Dim continent As String Dim population As Double 'in millions Dim area As Double 'in square miles End Structure Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192 member nations of the UN. What will be displayed in the DataGridView control when the following code is executed? Dim query = From country In nations Where country.population > 200 Order By country.area Descending Select country.name, country.continent dgvNations.DataSource = query.ToList dgvNations.CurrentCell = Nothing
(A) Two columns, with headers name and continent. The grid will display the countries (along with their continents) whose populations are greater than 200 million people. The countries will be displayed in order to their population, beginning with the most populous country. (B) Four columns, with each row containing data about a single country whose population is greater than 200 million people. The countries will be displayed in order of their area, beginning with the largest country. (C) Two columns, with headers country.name and country.continent. The grid will display the countries (along with their continents) whose populations are greater than 200 million people. The countries will be displayed in order to their area, beginning with the largest country. (D) Two columns, with headers name and continent. The grid will display the countries (along with their continents) whose populations are greater than 200 million people. The countries will be displayed in order to their area, beginning with the largest country. D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
13. Suppose a structure and an array are created with the code Structure Nation Dim name As String Dim continent As String Dim population As Double 'in millions Dim area As Double 'in square miles End Structure Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192 member nations of the UN. What will be displayed in the DataGridView control when the following code is executed? Dim query = From country In nations Where (country.continent = "Europe") And country.name.StartsWith("S") Let countryUC = country.name.ToUpper Let pop = (1000000 * country.population).ToString("N0") Let area = FormatNumber(country.area, 0) Order By countryUC Ascending Select countryUC, pop, area
(A) Three columns, with headers country, population, and area. The grid will display the countries in Europe (along with their populations and areas) whose names begin with the letter S. The countries will be displayed capitalized in alphabetical order and their populations and areas will be formatted with commas as thousands separators. (B) Four columns, with headers countryUC, continent, pop, and area. The grid will display the countries in Europe (along with their continents, populations and areas) whose names begin with the letter S. The countries will be displayed capitalized in alphabetical order and their populations and areas will be formatted with commas as thousands separators. (C) Three columns, with headers countryUC, pop, and area. The grid will display the countries in Europe (along with their populations and areas) whose names begin with the letter S. The countries will be displayed capitalized in alphabetical order and their populations and areas will be formatted with commas as thousands separators. (D) Three columns, with headers countryUC, pop, and area. The grid will display the countries in Europe (along with their populations and areas) whose names begin with the letter S. The countries will be displayed capitalized in reverse alphabetical order and their populations and areas will be formatted with commas as thousands separators. C 14. Which of the following methods is used to change the words in a column header of a DataGridView control? (A) HeaderText (B) HeaderName (C) HeaderContent (D) HeaderCaption A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Section 7.4 Two-Dimensional Arrays 1.
Given the following array, what value will be assigned to num? 0 0 5 10 15 20
num = myArray(3,4)
0 1 2 3 4
myArray 1 2 1 2 6 7 11 12 16 17 21 22
3 3 8 13 18 23
4 4 9 14 19 24
(A) 19 (B) 4 (C) 24 (D) 0 (E) None of the above A 2.
Assume the array nums has been filled as shown. What is the output of the following program segment? nums s = 0 For k As Integer = 0 To 2 s += nums(k, k + 1) Next lstBox.Items.Add(s)
0 1 2 3
0 1 2 3 |-----------------------| 2 3 4 5 | | 6 5 4 3 | | 2 3 4 5 | | 6 5 4 3
(A) 64 (B) 12 (C) 13 (D) 25 (E) None of the above B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
3.
How many elements are in the array declared by Dim myArray(3, 2) As Double
(A) 12 (B) 8 (C) 6 (D) 2 (E) None of the above A 4.
Which of the following types of variables is capable of holding the information contained in a table that has four rows and four columns? (A) one-dimensional arrays (B) simple variables (C) single-subscripted variables (D) double-subscripted variables D
5.
Which of the following types of variables can only hold a single item of data? (A) two-dimensional arrays (B) simple variables (C) single-subscripted variables (D) double-subscripted variables B
6.
Which of the following declarations creates a two-dimensional array? (A) Dim newVar(2, 2) As Double (B) Dim newVar As Integer (C) Dim newVar(2) As Double (D) Dim newVar(2) A
7.
Which of the following declarations can be used to create a three-dimensional array? (A) Dim newVar(3, 3) As Double (B) Dim newVar(2, 2, 2) As Double (C) Dim newVar(3) As Double (D) Dim newVar(3) B
8.
Arrays that are capable of holding the contents of a table with several rows and columns, are known as two-dimensional arrays or double subscripted variables. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
The statement Dim nextVar(3, 5) As Double
declares a two-dimensional array that can store a table containing 4 columns and 6 rows of numeric data. (T/F) F 10. A two-dimensional array can be declared and initialized at the same time. (T/F) T 11. If the two-dimensional array nums has three rows and six columns, then the value of nums.GetUpperBound(0) is 3 and the value of nums.GetUpperBound(1) is 6. (T/F) F 12. In the two-dimensional array declaration Dim newVar(,) As Double = {{1, 2, 3}, {4, 5, 2054}, {6, 802, 2786}}
the comma in newVar(,) can be removed. (T/F) F 13. The ReDim and Preserve keywords can be used with two-dimensional arrays. (T/F) T 14. Variable arrays can only be one- or two-dimensional. (T/F) F 15. ReDim statements cannot be used to change a one-dimensional array into a two-dimensional array. (T/F) T 16. ReDim statements can be used to change a one-dimensional array into a three-dimensional array. (T/F) F 17. Given the following statements, the first subscript in the second statement references a column. (T/F) Dim myArray(15, 20) As Double myArray(5, 10) = 0
F 18. Two-dimensional arrays are often referred to as tables or matrices. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
19. The third statement below is NOT valid. (T/F) row = 5 col = 3 someArray(col, row) = 25
F 20. Write code using a For...Next loop to fill a 5-by-5 matrix so that the two diagonals have asterisks (*) in them as shown. 0 1 2 3 4
0 *
1
2
*
3
4 *
* *
* * * * Ans: For i As Integer = 0 to 4 matrix(i, i) = "*" matrix(i, 4 - i) = "*" Next
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 8 Text Files Section 8.1 Managing Text Files 1.
What method is used to copy the contents of a string array or LINQ query into a text file? (A) CreateTextFile (B) WriteAllLines (C) CopyToTextFile (D) ReadAllLines B
2.
Which of the following methods is used to change the words in a column header of a DataGridView control? (A) HeaderText (B) HeaderName (C) HeaderContent (D) HeaderCaption A
3.
What property is set to False in order to remove the row header column on the left side of a DataGridView control? (A) ShowRowHeader (B) DisplayRowHeader (C) RowHeaderVisible (D) RowHeaderEnabled C
4.
A statement of the form IO.File.WriteAllLines("fileName.txt", numArray) copies the contents of the numeric array numArray into a text file. (T/F) F
5.
The acronym CSV stands for "Comma Separated Values". (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
6.
Each record of the file UN.txt contains four pieces of information (name, continent, population in millions, area) about one of the 192 member countries of the United Nations. The first two lines of the file are Afghanistan,Asia,28.2,251772 Albania,Europe,3.2,11100
What is the first line of the file created by the following code? Dim query = From line In IO.File.ReadAllLines("UN.txt") Let data = line.Split(","c) Let country = data(0) Let continent = data(1) Select country & " is in " & continent IO.File.WriteAllLines("NewFile.txt", query)
(A) Afghanistan is in Asia (B) Albania is in Europe (C) country is in continent (D) The new file will be empty. A 7.
Each line of the file UN.txt contains four pieces of information (name, continent, population in millions, area) about one of the 192 member countries of the United Nations. The first two lines of the file are Afghanistan,Asia,28.2,251772 Albania,Europe,3.2,11100
What is the first line of the file created by the following code? Dim query = From line In IO.File.ReadAllLines("UN.txt") Let data = line.Split(","c) Let country = data(0) Let population = 1000000 * CDbl(data(2)) Select country & "," & population IO.File.WriteAllLines("NewFile.txt", query)
(A) Afganistan,28.2 (B) Afganistan,28,200,000 (C) Afganistan,28200000 (D) country,population C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
8.
Each line of the file UN.txt contains four pieces of information (name, continent, population in millions, area) about one of the 192 member countries of the United Nations. The first two lines of the file are Afghanistan,Asia,28.2,251772 Albania,Europe,3.2,11100
What is the first line of the file created by the following code? Dim query = From line In IO.File.ReadAllLines("UN.txt") Let data = line.Split(","c) Let country = data(0) Let population = 1000000 * CDbl(data(2)) Let area = CDbl(data(3)) Let density = population / area Select country & "," & density.ToString("N1") IO.File.WriteAllLines("NewFile.txt", query)
(A) Afganistan,28200000/251772 (B) Afganistan,112.0 (C) Afganistan,112.006100757829 (D) country,density B 9.
What elements are in the array newArray after the following code is executed? Dim firstArray() As Integer = {1, 2, 3} Dim secondArray() As Integer = {3, 4, 5, 6} Dim newArray() As Integer = firstArray.Intersect(secondArray).ToArray
(A) 1, 2, 3, 3, 4, 5, 6 (B) 1, 2, 3, 4, 5, 6 (C) 3 (D) 1, 2 C 10. What elements are in the array newArray after the following code is executed? Dim firstArray() As Integer = {1, 2, 3} Dim secondArray() As Integer = {3, 4, 5, 6} Dim newArray() As Integer = firstArray.Union(secondArray).ToArray
(A) 1, 2, 3, 3, 4, 5, 6 (B) 1, 2, 3, 4, 5, 6 (C) 3 (D) 1, 2 B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
11. What elements are in the array newArray after the following code is executed? Dim firstArray() As Integer = {1, 2, 3} Dim secondArray() As Integer = {3, 4, 5, 6} Dim newArray() As Integer = firstArray.Except(secondArray).ToArray
(A) 1, 2, 3, 3, 4, 5, 6 (B) 1, 2, 3, 4, 5, 6 (C) 3 (D) 1, 2 D 12. How many elements are in the array newArray after the following code is executed? Dim firstArray() As Integer = {1, 2, 3} Dim secondArray() As Integer = {3, 4, 5, 6} Dim newArray() As Integer = firstArray.Concat(secondArray).ToArray
(A) 7 (B) 6 (C) 3 (D) none A 13. The following statement causes the Open dialog box to become visible on the form. OpenFileDialog1.ShowDialog()
After a file has been selected and the Open button is pressed, the value of OpenFileDialog1.FileName
will be the file’s filespec; this will include all of the following except (A) the file’s drive. (B) the file’s date of creation. (C) the file’s path. (D) the file’s extension. B 14. When you place the OpenFileDialog control on a form, it will not be visible on the form. (T/F) T 15. In order to use the OpenFileDialog control, the programmer must first change its Name property to something other than the default OpenFileDialog1. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
16. The following statement could be used to set the Filter property of the OpenFileDialog control so that it displays files with the .txt extension. (T/F) Text Files (*.txt)|(.txt)
F 17. The following statement could be used to set the Filter property of the OpenFileDialog control so that it displays files with the .txt extension. (T/F) Text Files (*txt)|*.txt
T Section 8.2 StreamReaders, StreamWriters, and Structured Exception Handling 1. Opening a file for output means that (A) data may be stored onto the disk. (B) data may be read from the disk. (C) anything that appears as output on the screen is written to the disk. (D) data may be read from and stored onto the disk. A 2. Opening a file for input means that (A) data may be stored onto the disk. (B) data may be read from the disk. (C) anything that appears as output on the screen is written to the disk. (D) data may be read from and stored onto the disk. B 3. When a file is opened for Append, (A) data may be read starting at the beginning of the file. (B) data written to the file replaces the data already there. (C) data written to the file are inserted at the beginning of the file. (D) data written to the file are added at the end of the file. D 4. Which of the following statements is used when you want to add data to the end of an existing text file? (A) Dim sr As IO.StreamReader = IO.File.OpenText("Data.txt") (B) Dim sr As IO.StreamReader = IO.File.AddText("Data.txt") (C) Dim sw As IO.StreamWriter = IO.File.CreateText("Data.txt") (D) Dim sw As IO.StreamReader = IO.File.AppendText("Data.txt") D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
5. When an existing file is opened for Output (A) new data are added at the end of the existing file. (B) the file is erased completely and new data are added at the beginning of the file. (C) Visual Basic generates an error message and stops program execution. (D) data may be read from it. B 6. Assume the file Alphabet.txt contains 26 records, the lowercase letters of the alphabet in ascending order. What is stored in Target.txt after the following code is executed? Dim letter As String Dim sr As IO.StreamReader = IO.File.OpenText("Alphabet.txt") Do While Not sr.EndOfStream Dim sw As IO.StreamWriter = IO.File.CreateText("Target.txt") letter = sr.ReadLine sw.WriteLine(letter) sw.Close() Loop sr.Close()
(A) The alphabet in descending order. (B) The alphabet in ascending order. (C) a (D) z D 7. Assume the file Alphabet.txt contains 26 records, the lowercase letters of the alphabet in ascending order. What happens when the following code is executed? Dim letter As String Dim sr As IO.StreamReader = IO.File.OpenText("Alphabet.txt") Do While Not sr.EndOfStream letter = sr.ReadLine Dim sw As IO.StreamWriter = IO.File.CreateText(letter.ToUpper & ".txt") sw.WriteLine(letter.ToUpper & ".txt") sw.Close() Loop sr.Close()
(A) A "too many files open" error is produced. (B) Twenty-six files are created and named with one of the uppercase letters of the alphabet and each containing only its own file name. (C) A file called z is created containing all of the letters of the alphabet in lowercase. (D) An "invalid file name" error is produced. B 8. Select the example statement below that would open a text file for output. (A) Dim sw As IO.StreamWriter = IO.File.OpenText("IncomeData.txt") (B) Dim sw As IO.StreamWriter = IO.File.CreateText("IncomeData.txt") (C) Dim sr As IO.StreamReader = IO.File.OpenText("IncomeData.txt") (D) Dim sr As IO.StreamReader = IO.File.AppendText("IncomeData.txt") B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9. If a text file is opened for input, then this allows (A) data to be placed into the file by the Visual Basic application. (B) data that is in the file to be used in the Visual Basic application. (C) data to be appended to the end of the file by the Visual Basic application. (D) data to be deleted from the file. B 10.The line of code sw.Close()
when used with the line sw = IO.File.CreateText(fileName)
completes which of the following tasks? (A) it closes the current application (B) it closes the current form (C) it closes the currently opened window of the application (D) it closes the file referred to by the variable name sw D 11. Which one of the following is NOT an example of a bug? (A) viruses (B) accessing the wrong property value (C) using an incorrect formula (D) typos A 12. When an exception is generated by a Visual Basic application, the default exception handler will (A) restart the program. (B) terminate the program. (C) continue to execute the program. (D) display a dialog box that asks the user if they wish to restart the computer. B 13. Which of the following exceptions will be generated by trying to access a disk drive that doesn’t contain a disk? (A) NullReferenceException (B) IndexOutOfRangeException (C) IO.DirectoryNotFoundException (D) OverflowException C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
14. Which exception will the following code generate? Dim num1 As Integer, num2 As Integer = 1000000 num1 = num2 * num2
(A) NullReferenceException (B) ArgumentOutOfRangeException (C) OverflowException (D) InvalidCastException C 15. Which of the following statements will result in an IndexOutOfRangeException if placed after the following line of code? Dim arr(2) As Integer
(A) arr(3) = 2 (B) arr(2) = 2 (C) arr(1) = 2 (D) arr(0) = 2 A 16. Consider the following code. The Catch block of code will not be executed if the user enters which of the following when prompted? Private Sub btnCalc_Click(...) Handles btnCalc.Click Dim newNum, finalNum As Integer Dim message, message1 As String Try newNum = CInt(InputBox("How old are you?")) Catch message = "That answer is not an Integer value." MessageBox.Show(message) newNum = 0 Finally finalNum = newNum + 1 message1 = "Your age next year will be " & finalNum & "." MessageBox.Show(message1) End Try End Sub
(A) one (B) 0 (C) 3000000000 (D) * B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
17. The catch block: Catch exc As IO.IOException
handles which of the following exceptions? (A) any exception that hasn’t been handled by the Try block (B) an exception generated by deleting or renaming an open file (C) an exception generated by attempting to access a file that is missing (D) an exception generated by attempting to access a file within a folder that is missing B 18. The code in a Try block refers to a file and the first catch block is written as follows: Catch exc As IO.IOException
The second catch block is written with the following clause: Catch
What type of exception will this second Catch block handle? (A) any exception that hasn’t already been handled (B) an exception generated by deleting or renaming an open file (C) this block isn’t designed to catch any exceptions (D) this block will catch all cases where the file specified by fileName does exist A 19. Which exception will the following code generate? Dim str As String str = "Visual Basic".Substring(14,2)
(A) NullReferenceException (B) ArgumentOutOfRangeException (C) OverflowException (D) InvalidCastException B 20. To get rid of an existing closed file, execute a statement of the form IO.File.Delete(filespec). (T/F) T 21. Text files are updated by opening them for Input and then entering the new data. (T/F) F 22. The value of sr.EndOfStream is True when the end of file accessed by the StreamReader is reached. (T/F) T 23. The following statements are valid. (T/F) Dim filespec As String filespec = InputBox("Enter name of file to process:") Dim sr As IO.StreamReader = IO.File.OpenText(filespec)
T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
24. When a text file is created in Visual Basic using a StreamWriter, the extension .txt is automatically added to the file name if no other extension is specified. (T/F) F 25. In the following statement, sw is the name of a variable. (T/F) Dim sw As IO.StreamWriter = _ IO.File.CreateText("C:\TEXT FILES\INCOME Data.txt")
T 26. If an existing text file is opened for output, the computer will append any new data to the end of this existing file. (T/F) F 27. If the value of IO.File.Exists(filespec) is True, then the specified file exists. (T/F) T 28. An individual item of a text file cannot be changed or deleted directly. (T/F) T 29. Placing the statement Imports System.IO near the top of the Code Editor (before the Class frmName statement) simplifies file handling by eliminating the need to use the words StreamReader, StreamWriter, and File in your code. (T/F) F 30. In a Try-Catch-Finally block, the code in the Finally block is always executed, regardless of whether an exception was thrown. (T/F) T 31. In a Try-Catch-Finally block, the code in the first listed Catch block is executed if an exception is thrown in the Try block. (T/F) F 32. In a Try-Catch-Finally block, if there are multiple Catch blocks, the code in the last listed Catch block is always executed. (T/F) F 33. In the line of code sw = IO.File.CreateText(fileName) sw is a variable that holds the value of a filespec for an existing text file. (T/F)
F 34. A Catch block of code that is initiated by the keyword Catch and doesn’t contain any additional terms will execute regardless of whether an exception occurred. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
35. In a Try-Catch-Finally block, the Catch clauses are considered one at a time, in the order in which they appear in the code, until the proper exception is located. (T/F) T 36. When using Try-Catch-Finally blocks, the programmer is limited to three Catch blocks of code. (T/F) F 37. If an attempt is made to access a file within a missing folder using the OpenText method, the IO.DirectoryNotFoundException will be thrown. (T/F) T 38. Give three different file modes that Visual Basic allows you to use when accessing a file. Input, Output, and Append 39. A text file cannot be open both for input and for output at the same time. (T/F) T 40. Before a text file can be opened for input, it must already exist on a disk. (T/F) T 40. Before a text file can be opened for append, it must already exist on a disk. (T/F) F 41. If any record in a text file is to be changed, the entire file must be rewritten. (T/F) T Section 8.3 XML 1. The XML standard was recommended by the World Wide Web Consortium. (T/F) T 2. XML stands for (A) extreme markup language (B) excessive markup language (C) extraordinary markup language (D) extensible markup language D 3. Which of the following terms is NOT used to refer to a part of an XML document? (A) content (B) element (C) start tag (D) title D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
4. Comment statements in XML begin with <!-- and end with --!> (T/F) F 5. Element names are case sensitive. (T/F) T 6. End tags always begin with a less than symbol followed by a forward slash. (T/F) T 7. Each element of an XML document must contain both a start tag and an end tag. (T/F) T 8. Elements in an XML document can contain other elements. (T/F) T 9. Which of the following terms is NOT used to refer to elements in an XML document? (A) child (B) basis (C) sibling (D) parent B 10. An XML file can contain several root elements. (T/F) F 11. XML files must be loaded into XElement objects before being accessed with LINQ. (T/F) T 12. The characters < and & cannot appear in the content of an element of an XML file. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 9 Additional Controls and Objects Section 9.1 List Boxes and Combo Boxes 1.
Which of the following statements will remove all items from the list box?
(A) lstBox.Items.RemoveAll (B) lstBox.Clear() (C) lstBox.Items.Clear() (D) For i As Integer = 1 to lstBox.Items.Count lstBox.Items.RemoveAt(i) Next
C 2.
Which of the following controls has a drop down arrow and an attached text box that allows users to type selections?
(A) ListBox (B) Simple ComboBox (C) DropDown ComboBox (D) DropDownList ComboBox C 3.
Which of the following controls does not have a DropDownStyle property that must be specified at design time?
(A) ListBox (B) Simple ComboBox (C) DropDown ComboBox (D) DropDownList ComboBox A 4.
The statement lstBox.Items.RemoveAt(lstBox.SelectedIndex) deletes the item currently highlighted in lstBox. (T/F) T
5.
In a Simple combo box, the list is always visible. (T/F) T
6.
If no item in a combo box is selected, the value of lstBox.SelectedIndex is 0. (T/F) F
7.
The value of cboBox.Text is the currently highlighted item. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
8.
For a list box named lstBox, the following statement will delete the first occurrence of the value of num from its list. (T/F) lstBox.Items.RemoveAt(num)
F 9.
For a list box named lstBox, the following statement will delete from its list the item whose index is the value num. (T/F) lstBox.Items.RemoveAt(num)
T 10. With a simple combo box, the list drops down when the user clicks on the arrow and then disappears after a selection is made. (T/F) F 11. The DropDownStyle property of a combo box must be set at design time. (T/F) T 12. Combo boxes have most of the same properties, events, and methods as list boxes. (T/F) T 13. Which of the following expressions refers to the contents of the last row of the combo box? (A) cboBox.Items(lstBox.Items.Count) (B) cboBox.Items(lstBox.Items.Count - 1) (C) cboBox.Items(Count) (D) cboBox.Items.Count B 14. Which of the following expressions refers to the contents of the first row of the combo box? (A) cboBox.Items(0) (B) cboBox.Items(1) (C) cboBox.Items.First (D) cboBox.Items(First) A 15. Which of the following is the default setting for a combo box's DropDownStyle property? (A) List (B) Simple (C) DropDown (D) DropDownList C 16. A list that has dropped down disappears when the user clicks on an item or presses the Enter key. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
17. The value of cboBox.Text is the contents of the text box at the top of the combo box. (T/F) T 18. The number of items in cboBox is cboBox.Items.Count. (T/F) T 19. The value of cboBox.Items(n) is the nth item in the combo box. (T/F) F Section 9.2 Eight Additional Controls and Objects 1.
For a scroll bar, the value of the Value property is
(A) any number. (B) a number between the values of the SmallChange and LargeChange properties. (C) a number between the values of the Minimum and Maximum properties. (D) true or false. C 2.
To move the scroll box of a scroll bar a large distance
(A) click on the scroll box. (B) click on the furthermost arrow button. (C) click on the bar between the scroll box and the furthermost arrow. (D) It can only be moved by writing code. C 3.
Which of the following steps turns off a timer control?
(A) Set the Interval property to zero. (B) Set the Enabled property to False. (C) Make the timer control invisible. (D) None of the above B 4.
The distance that the scroll box of a scroll bar will move when an arrow on the scroll bar is clicked is determined by
(A) LargeChange. (B) SmallChange. (C) Value. (D) Maximum. B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
5.
The largest possible value for the Value property in a scroll bar is determined by
(A) the LargeChange property. (B) the Minimum property. (C) the Value property. (D) the Maximum property. D 6.
Which of the following methods is used to load a new picture into a picture box control?
(A) picBox.Picture = filespec (B) picBox.Image = filespec (C) picBox.Image = Image.FromFile(filespec) (D) picBox.Picture = LoadPicture(filespec) C 7.
Which of the following controls is NOT used to select an item from a list?
(A) List Box (B) ComboBox (C) Timer Control (D) OpenFileDialog C 8.
In order to begin timing with the timer control, which one of the following must be true?
(A) the timer control’s Name property must include the prefix tmr (B) the timer control’s Interval property must be set to 1 (C) the timer control’s Enabled property must be set to False (D) the timer control’s Enabled property must be set to True D 9.
Which of the following settings of a timer's Interval property will raise a timer event every 5 seconds?
(A) 5 (B) 5000 (C) 5 seconds (D) 5000 milliseconds D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. In the following statement which term is used to designate the distance (in pixels) from the left side of the picture box to the left side of the rectangle? picBox.CreateGraphics.DrawRectangle(Pens.Blue, x, y, w, h)
(A) h (B) w (C) y (D) x D 11. A form contains a horizontal scroll bar control named hsbXpos, and the statement lblFace.Top = hsbXpos.Value is placed inside the hsbXpos.Scroll event procedure (where lblFace identifies a label on the form, and the hsbXpos’s Minimum and Maximum properties are set at their default values). What will happen when the hsbXpos.Scroll event is triggered by moving the scroll bar’s scroll box to the right? (A) lblFace will move to the left (B) lblFace will move up (C) lblFace will move down (D) lblFace will move to the right C 12. A form contains a horizontal scroll bar control named hsbXpos, and the code lblFace.Left = hsbXpos.Value is placed inside the hsbXpos.Scroll event procedure (where lblFace identifies a label on the form, and the hsbXpos’s Minimum and Maximum properties are set at their default values). What will happen when the hsbXpos.Scroll event is triggered by moving the scroll bar’s scroll box to the right? (A) lblFace will move to the left (B) lblFace will move up (C) lblFace will move down (D) lblFace will move to the right D 13. With a scroll bar control, the default values of the Minimum and Maximum properties are 1 and 100 respectively. (T/F) F 14. When the SizeMode property of a picture box control is set to StretchImage, a picture placed in the control will be resized to fit the picture control. (T/F) T 15. The main properties of a scroll bar are set in millimeters. (T/F) F 16. The size of a picture assigned to a picture box control is limited by the original size of the picture. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
17. For a timer control named Timer1, the Timer1.Enabled event is triggered each time Timer1.Interval milliseconds elapses. (T/F) F 18. The timer control is invisible during run time. (T/F) T 19. The following statement can be used to assign the Plane.bmp image to the picBox picture box control at runtime. (T/F) picBox.Image = "Plane.bmp"
F 20. The SizeMode property of a picture box control cannot be altered at run time. (T/F) F 21. The Clipboard object is used for all of the following except which one? (A) Copy text to the Clipboard. (B) Paste text from the Clipboard. (C) Clear the screen. (D) Clear the Clipboard. C 22. The correct code for replacing the selected text in txtBox with the contents of the Clipboard is: (A) txtBox.SelectedText = Clipboard.Text (B) txtBox.SelectedText = Clipboard.SetText (C) txtBox.SelectedText = Clipboard (D) txtBox.SelectedText = Clipboard.GetText D 23. What expression should be used to generate random numbers from the set 2, 3, 4, 5, 6, 7? (Assume that the statement Dim randomNum As New Random() has been executed.) (A) randomNum.Next(2, 7) (B) randomNum.Next(1, 7) (C) randomNum.Next(1, 8) (D) randomNum.Next(2, 8) D 24. MenuStrip controls respond to which one of the following events? (A) Change (B) Click (C) Double (D) Load B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
25. Which of the following statements is true concerning the Clipboard object? (A) It is used to produce the Microsoft Help. (B) It is used to create a text file that permanently maintains copied items on the hard drive. (C) It is an object that appears on the form and is raised by its Click event. (D) It is a portion of memory that holds information and has no properties or events. D 26. A form contains a text box named txtBox and a button named btnClip. The statement Clipboard.SetText(txtBox.Text) is placed inside btnClip’s Click event procedure. At run time, what will be placed into the Clipboard if the user enters the word “Hello” into txtBox and then clicks on btnClip? (A) the word “txtBox” (B) the word “Hello” (C) the Clipboard will be emptied of all contents (D) the word “btnClip” B 27. A form contains a text box named txtBox and a button named btnClip. The statement Clipboard.SetText("") is placed inside btnClip’s Click event procedure. At run time, what will be placed into the Clipboard if the user enters the word “Hello” into txtBox and then clicks on btnClip? (A) the word “txtBox” (B) the word “Hello” (C) the Clipboard will be emptied of all contents (D) the word “btnClip” C 28. A form contains a button called btnDisplay, and the following code is placed inside btnDisplay’s Click event procedure. At run time, which one of the following cannot possibly be displayed in the message box when the user clicks on btnDisplay? Dim rndNum As New Random() Dim num1 As Integer num1 = rndNum.Next(0, 3) MessageBox.Show(CStr(num1))
(A) 3 (B) 2 (C) 1 (D) 0 A 29. Pressing Ctrl+V with the cursor in a text box inserts the contents of the Clipboard at the cursor position. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
30. The statement Clipboard.SetText(str) replaces the current contents of the Clipboard with the value of str. (T/F) T 31. Menus are designed with the MenuStrip control invoked from the Toolbox. (T/F) T 32. Given that the ANSI value for the letter A is 65, the pair of statements below will always display one of A, B, or C. (T/F) Dim randomNum As New Random() txtBox.Text = Chr(randomNum.Next(65,68))
T 33. The Clipboard object is used to move or copy information from one location to another, including from one Windows application to another. (T/F) T 34. A random number generator object can be declared with the following statement. (T/F) Dim rndNum As New Random()
T 35. A form contains a button called btnDisplay, and the following code is placed inside btnDisplay’s Click event procedure. At run time, identical values will be displayed for num1 and num2 in the message box. (T/F) Dim num1, num2 As Integer num1 = rndNum.Next(0, 3) num2 = rndNum.Next(0, 3) MessageBox.Show(num1 & " " & num2)
F 36. The output of the following lines of code will be a randomly selected whole number from 1 through 6, inclusive. Dim rndNum As New Random() num1 = rndNum.Next(1, 7) MessageBox.Show(num1)
T 37. When working with the MenuStrip control, the programmer is not allowed to set up access keys by placing an ampersand in front of a character of text. (T/F) F 38. When working with the MenuStrip control, the programmer makes the menus useful by placing code in the relevant Click event procedure for each menu item. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
39. After a ToolTip control has been placed into the component tray of the Form Designer, the message that appears when the mouse hovers over a text box is set with which of the following properties? (A) ToolTip (B) Message (C) ToolTip on ToolTip1 (D) ToolTipMessage C 40. After a ToolTip control has been placed into the component tray of the Form Designer, the setting of what property determines how long a tooltip will remain visible when the mouse hovers over a text box? (A) AutomaticDelay (B) AutoPopDelay (C) Duration (D) AutoDelay B 41. After a ToolTip control has been placed into the component tray of the Form Designer, the setting of what property determines the amount of time required for a tooltip to appear when mouse hovers over a text box? (A) AutomaticDelay (B) AutoPopDelay (C) Duration (D) AutoDelay A Section 9.3 Multiple-Form Programs 1.
The term modal refers to
(A) the user not being allowed to shift the focus to any other form until the displayed modal form is hidden. (B) the BorderStyle property is set to "3-Fixed Double." (C) the ability to reposition a form at run time. (D) the ability to display more than one form at one time. A 2.
A variable that is visible to every form in a program is said to have _________ scope.
(A) class-level (B) module-level (C) global (D) namespace-level D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
3.
Namespace-level variables are declared with the keyword _______.
(A) Dim (B) Private (C) Public (D) Global C 4.
The _______ event occurs just before a form closes.
(A) FormClosing (B) Closing (C) CloseForm (D) FormEnding A 5.
The _________ control is used to print a form.
(A) PrintDocument (B) FormPrinter (C) Print (D) PrintForm D 6.
The _________ method is used to display a form as a modal form and give it the focus.
(A) ShowForm (B) ShowDialog (C) ShowModal (D) ShowModalForm B 7.
A program contains two forms with the names Form1 and Form2. Form1 contains a button called btnDisplay, and the following code is placed inside btnDisplay’s Click event procedure. Form2.ShowDialog() MessageBox.Show("Hello")
Which of the following statements is true after the user clicks on btnDisplay at run time? (A) Form2 appears behind Form1 and the user will be able to set the focus to either form. (B) Form2 appears in front of Form1 and the user will only be able to set the focus to Form2. (C) A message box displaying the text “Hello” appears in front of Form2 and Form1. (D) A message box displaying the text “Hello” appears behind Form2 and Form1. B 8.
When working with multiple forms, only one form can be visible at the same time. (T/F) F © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
9.
A modal form cannot lose the focus until the form disappears. (T/F) T
10. Code in one form can refer to controls in another form. (T/F) T 11. A class-level variable declared in one form can be accessed by other forms if it is declared with the keyword Public. (T/F) T 12. The first form loaded when a program starts running is called the ________ form. (A) default (B) startup (C) initial (D) favored B 13. The __________ is used to specify the startup form for a program. (A) Toolbox (B) Properties window (C) Project Designer (D) Toolbar C 14. Forms only can be closed with statements of the form Me.Close(). (T/F) F Section 9.4 Graphics In the following exercises assume the statement Dim gr As Graphics = picOutput.CreateGraphics has been executed. 1.
What is the effect of the statement gr.DrawString("Hello", Me.Font, Brushes.Blue, 50, 60)?
(A) Display the word Hello in blue. (B) Display the word Hello beginning 50 pixels from the left side of the picture box. (C) Display the word Hello with the top of the word 60 pixels from the top of the picture box. (D) All of the above D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
2.
Give a statement that displays the value of the string variable strVar with yellow letters.
(A) gr.DrawString(strVar, Me.Font, Yellow, 50, 50) (B) gr.DrawString(strVar, Me.Font, Brushes.Yellow, 50, 60) (C) gr.DrawString(strVar, Me.Font, Pens.Yellow, 50, 60) (D) gr.DrawString(strVar, Me.Font, Color.Yellow, 50, 60) B 3.
Give a statement that draws a red diagonal from the upper-left corner of the picture box to the lower-right corner.
(A) gr.DrawLine(Color.Red, 0, 0, picOutput.Width, picOutput.Height) (B) gr.DrawLine(Pens.Red, 0, 0, picOutput.Height, picOutput.Width) (C) gr.DrawLine(Pens.Red, 0, 0, picOutput.Width, picOutput.Height) (D) gr.DrawLine(Pens.Red, picOutput.Width, 0, 0, picOutput.Height) C 4.
Give a statement that draws a blue line from the upper-left corner of the picture box to the center of the picture box.
(A) gr.DrawLine(Pens.Blue, 0, 0, picOutput.Width/2, picOutput.Height/2) (B) gr.DrawLine(Pens.Blue, picOutput.Height, picOutput.Width, 0, 0) (C) gr.DrawLine(Pens.Blue, 0, 0, picOutput.Height/2, picOutput.Width/2) (D) gr.DrawLine(Pens.Blue, picOutput.Width/2, 0, 0, picOutput.Height/2) A 5.
The radius of the circle drawn by the statement gr.DrawEllipse(Brushes.Red, 10, 20, 40, 40) is
(A) 10 pixels. (B) 20 pixels. (C) 40 pixels. (D) 80 pixels. B 6.
The center of the circle drawn by the statement gr.DrawEllipse(Brushes.Red, 10, 20, 40, 40) has coordinates
(A) (30, 40). (B) (10, 20). (C) (20, 40). (D) (40, 40). A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
7.
The distance from the top of the picture box to the top of the circle drawn by the statement gr.DrawEllipse(Brushes.Red, 10, 20, 40, 40) is
(A) 10 pixels. (B) 20 pixels. (C) 40 pixels. (D) 80 pixels. B 8.
The statements gr.DrawLine(Pens.Red, 0, 0, 30, 40) and gr.DrawLine(Pens.Red, 0, 0, 40, 30) draw lines of the same length. (T/F) T
9.
The statements gr.DrawLine(Pens.Red, 0, 0, 30, 40) and gr.DrawLine(Pens.Red, 10, 20, 40, 60) draw lines of the same length. (T/F) T
10. The statements gr.DrawLine(Pens.Red, 10, 20, 30, 40) and gr.DrawLine(Pens.Red, 30, 40, 10, 20) draw the same line. (T/F) T 11. The statement gr.FillRectangle(Brushes.Red, 10, 20, 30, 40) draws a solid rectangle of width 40 pixels. (T/F) F 12. The statement gr.FillRectangle(Brushes.Red, 0, 0, picOutput.Width/2, picOuput.Height/2) colors the upper-left quarter of the picture box red. (T/F) T 13. The statement gr.FillPie(Brushes.Red, 10, 10, 30, 30, 0, 180) draws a semicircle. (T/F) T 14. The sector of a circle with sweep angle 90 consists of a quarter of the circle. (T/F) T 15. The sector of a circle with start angle 90 and sweep angle 180 consists of the left half of the circle. (T/F) T 16. Animation can be produced by placing an image in a picture box and moving the picture box. (T/F) T 17. The value of Me.ClientSize.Height is the distance between the form’s title bar and the bottom border of the form. T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
18. The statement Dim pn As Pen = Pens.Blue is valid. T 19. Give a statement that deletes all graphics and text from picOutput. (A) picOutput.Clear() (B) picOutput.Text = "" (C) picOutput.Delete() (D) picOutput.Refresh() D 20. The pair of statements Dim fnt As Font = New Font("Times New Roman", 12) Gr.DrawString("Hello", fnt, Brushes.Blue,10,10)
displays the word Hello in 12-point Times New Roman font. T 21. What control must be added to a form to enable printing? (A) Printer (B) PrintPage (C) PrintDocument (D) PrintDialog C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 10 Databases Section 10.1 An Introduction to Databases 1.
A rectangular array of data is called a (A) field. (B) record. (C) table. (D) database management system. C
2.
Each row in a table is also called a(n) (A) record. (B) array. (C) field. (D) selection. A
3.
Which one of the following statements is NOT true about tables? (A) Each column of a table represents a field. (B) Each row of a table represents a record. (C) Each record in a table contains unrelated collections of fields. (D) A table is a rectangular array of data. C
4.
Which of the following is a collection of one or more (usually related) rectangular arrays of data? (A) a table (B) a field (C) a record (D) a database D
5.
Each entry of a field of a table must contain some information. (T/F) F
6.
The management of databases is one of the major uses of computers today. (T/F) T
7.
DataBase Explorer can be used to browse through all parts of a connected database. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
8.
What is a primary key? (A) A field that uniquely identifies each record. (B) The first field in every record.. (C) The largest field in every record.. (D) The first field specified when a table is created. A
9.
The requirement that no record may have a null primary key and that entries for primary keys be unique is called the (A) Rule of Referential Integrity. (B) Rule of Primary Keys. (C) Rule of Entity Integrity. (D) Rule of Null Values. C
10. Which one of the following statements is true about primary keys? (A) A column of names is usually a good choice for use as a primary key. (B) Only a single record can be used as a primary key. (C) Social Security numbers are never used as a primary key. (D) A primary key is used to uniquely identify records in a table. D 11. Which of the following is used to uniquely identify records in a table? (A) a Rule of Referential Integrity (B) a foreign key (C) a primary key (D) a relational database C 12. Which of the following is used to insure that each value for a foreign key also appears as a value for the primary key in any related table? (A) the Rule of Referential Integrity (B) the Join statement (C) SQL (D) a relational database A 13. Which of the following terms is defined as two or more interdependent tables? (A) Rule of Referential Integrity (B) join (C) joined database (D) relational database D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
14. Which of the following allows the linking together of two or more interdependent tables in a meaningful way? (A) ANSI (B) foreign keys (C) duplicate entries in a single field (D) null values B 15. The error message You cannot add or change a record because a related record is required in table 'Countries'.
will be generated by attempting which of the following with the Megacities database? (A) A user tries to enter a record in the table Countries that doesn’t contain data in the primary key. (B) A user tries to add a record to a table that is related to the Countries table, and that record contains a foreign key value that does not correspond to a primary key value in the Countries table. (C) A user tries to enter a record to the table Countries with the same primary key as another record. (D) The user tries to request a joined table using LINQ. B 16. The error message Index or primary key cannot contain a Null Value.
will be generated by attempting which of the following? (A) A user tries to enter a record in the table Countries that doesn’t contain data in the primary key. (B) A user tries to add a record to a table that is related to the Countries table, and that record contains a foreign key value that does NOT correspond to a primary key value in the Countries table. (C) A user tries to enter a record in the table Countries with the same primary key as another record. (D) A user tries to request a joined table using LINQ. A
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
17. The error message The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.
will be generated by attempting which of the following? (A) A user tries to enter a record in the table Countries that doesn’t contain data in the primary key. (B) A user tries to add a record to a table that is related to the Countries table, and that record contains a foreign key value that does NOT correspond to a primary key value in the Countries table. (C) A user tries to enter a record in the table Countries with the same primary key as another record. (D) A user tries to request a joined table using the SQL language. C 18. The Rule of Referential Integrity insists that each value in the foreign key must also appear in the primary key of the other table. (T/F) T 19. A foreign key allows Visual Basic to join together two tables from a relational database in a meaningful way. (T/F) T 20. Once a field has been specified as a primary key, Visual Basic will not allow that field to contain an empty entry for any record. (T/F) T 21. LINQ can be used in Visual Basic to sort records. (T/F) T 22. Consider the Countries table discussed in the text. The table has three fields - name, pop2010, and monetaryUnit - with name as the key field. Give a LINQ query that can be used to show the records from Countries in descending order based on the 2010 population. Dim query = From country In MegacitiesDataSet.Countries Order By country.pop2010 Descending Select country.name, country.pop2010, country.monetaryUnit
23. Consider the Countries table discussed in the text. The table has three fields - name, pop2010, and monetaryUnit - with name as the key field. Give a LINQ query that can be used to show the records from Countries whose 2010 population is greater than 100 million. Dim query = From country In MegacitiesDataSet.Countries Where country.pop2010 > 100 Select country.name, country.pop2010, country.monetaryUnit
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
24. A LINQ clause beginning with Join and ending with a phrase of the form On foreign key of tblA Equals primary key of tblB
causes two tables to be joined. (T/F) T Section 10.2 Editing and Designing Databases 1.
What control causes a binding navigator toolbar to appear anchored to the top of the form? (A) BindingNavigator (B) NavigatorToolbar (C) Navigator (D) BindingToolbar A
2.
Which of the following is NOT a button on the navigation toolbar? (A) Current position (B) Add new (C) Find (D) Delete C
3.
When a navigation toolbar is used with a table from a database, the first record of the table has position _______. (A) 0 (B) 1 (C) First (D) -1 B
4.
When a navigation toolbar is used with a table from a database, the first record of the table has index _______. (A) 0 (B) 1 (C) First (D) -1 A
5.
The Move next button of a navigator toolbar is never disabled. (T/F) F
6.
When a navigation toolbar is used with a table from a database, the value of BindingSource1.Position is the position of the current record. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
7.
When a navigation toolbar is used with a table from a database, the statement BindingSource1.Position = n makes the record of index n the current record. (T/F) T
8.
When a navigation toolbar is used with a table from a database, the Find method of the BindingSource control returns the position of a record. (T/F) F
9.
When a navigation toolbar is used with a table from a database, the Find method of the BindingSource control returns the value -1 if it cannot locate the sought after record. (T/F) F
10. Data normalization is the process of avoiding redundancy by splitting a table into two or more related tables. T 11. For purposes of readability it is often a good idea to have fields whose values can be computed from other fields. (T/F) F 12. Dragging a field name form the Data Sources window to the form automatically generates a label and a text box bound to the field. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 11 Object-Oriented Programming Section 11.1 Classes and Objects 1.
Which line of code will declare and create an instance of a new object variable called instructor using a class called Faculty? (A) Dim Faculty As instructor (B) Dim instructor As Faculty (C) Dim Faculty As New instructor() (D) Dim instructor As New Faculty() D
2.
What is the purpose of the Get property procedure? (A) retrieve the value of a property (B) assign values (C) access private methods (D) verify the data A
3.
Which procedure will set default values for member variables when an object is instantiated? (A) Form1_Load (B) Get property procedure (C) New (D) Set property procedure C
4.
Which one of the following statements does NOT apply to object-oriented programming? (A) Object-oriented programming helps to prevent the complete rewriting of a program every time a minor change needs to be made. (B) Object-oriented programming makes use of the principle of “data hiding.” (C) Object-oriented programming makes use of the encapsulation within an object of data and procedures that act on the data. (D) Object-oriented programming depends on the programmer having access to and understanding all of the code that contains the data and procedures that are encapsulated within an object. D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
5.
Which of the following is an example of a control object? (A) string variable (B) array (C) primary key (D) list box D
6.
Control objects and code objects share many things in common. Which of the following statements applies only to control objects (in other words, which of the following statements does not apply to code objects)? (A) these objects have properties (B) these objects are predefined and have physical manifestations (C) these objects respond to methods (D) these objects are an encapsulation of data and procedures that act on the data B
7.
Which of the following statements applies to code objects? (A) they are created from the Toolbox (B) they are predefined and have physical manifestations (C) they require the writing of a class block of code (D) they include objects of the type TextBox and ListBox C
8.
Which one of the following statements does NOT accurately describe object-oriented programs? (A) they are populated with objects that don’t have properties (B) they are populated with objects that hold data (C) they are populated with objects that respond to methods (D) they are populated with objects that raise events A
9.
Which of the following is an accurate definition of the term class? (A) A class is a specific instance of a user-defined type. (B) A class is an encapsulation of data and procedures that has properties and responds to methods. (C) A class is populated with objects that hold data, have properties, respond to methods, and raise events. (D) A class is a template from which objects are created. D
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10. Which of the following is an accurate definition of the term object-oriented program? (A) An object-oriented program is a specific instance of a user-defined type. (B) An object-oriented program is an encapsulation of data and procedures that has properties and responds to methods. (C) An object-oriented program is populated with objects that hold data, have properties, respond to methods, and raise events. (D) An object-oriented program is a template from which objects are created. C 11. Which statement form below assigns the value of a property to a variable? (A) objectName.propertyName = value (B) varName = objectName.propertyName (C) objectName.methodName(arg1, ...) (D) RaiseEvent eventName B 12. Which statement form below assigns a value to a property? (A) objectName.propertyName = value (B) varName = objectName.propertyName (C) objectName.methodName(arg1, ...) (D) RaiseEvent eventName A 13. Which statement form below carries out a method? (A) objectName.propertyName = value (B) varName = objectName.propertyName (C) objectName.methodName(arg1, ...) (D) RaiseEvent eventName C 14. Member variables in a class are normally specified as Private. (T/F) T 15. Set property procedures are used to assign values to member variables. (T/F) T 16. Get property procedures are used to retrieve values of member variables. (T/F) T 17. The New event procedure is automatically invoked when an object is instantiated. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
18. Two instances of the same class may exist in a single program. (T/F) T 19. An object can be created in the Declaration section of a form's Code editor with a pair of statements of the form. (T/F) Dim objectname As className objectName = New className(arg1, arg2, ...)
F 20. A class specifies the properties and methods that will be common to all objects that are instances of that class. (T/F) T 21. A good rule of thumb for object-oriented programming is that classes are the verbs in your analysis of the problem. (T/F) F 22. An object is an encapsulation of data and procedures that act on that data. (T/F) T 23. An object is a template from which classes are created. (T/F) F 24. If member variables are declared as Private, they cannot be accessed directly from outside an object. (T/F) T 25. Methods can be either Sub or Function procedures. (T/F) T 26. The principle of “data hiding” is the exact opposite of the concepts that serve as the basis for object-oriented programming. (T/F) F 27. Code objects are specific instances of a user-defined type, called a class. (T/F) T 28. Each list box is said to be an instance of the class ListBox. (T/F) T 29. The lines of code used to create an instance of a class cannot be used inside of a procedure. (T/F) F
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
30. A line of code of the form Dim objectName As New className(arg1, arg2, ...)
is used to instantiate an object variable or instance of an object. (T/F) T 31. In the following statement, the word Private is used to guarantee that the variable cannot be accessed directly from outside the object. (T/F) Private m_name As String
T 32. In the following statement, the word m_name demonstrates the proper name for a member or instance variable. If these variables are declared without the prefix “m_”, an exception will be thrown. (T/F) Private m_name As String
F 33. Member or instance variables should not be accessed directly; instead, these variables are accessed indirectly through the use of a property block of code. (T/F) T 34. Property blocks of code, that are used to access (indirectly) member or instance variables, must always contain both Get and Set property procedures. (T/F) F 35. Methods are constructed with a Function procedure when the method returns a value, and with a Sub procedure otherwise. (T/F) T 36. Each class has a special method called a constructor that is sometimes invoked when an object is instantiated. (T/F) F 37. The constructor method that is possessed by each class is used to set default values for member variables and to create other objects associated with this object. (T/F) T 38. The constructor method requires at least one argument, and the code inside the procedure block performs any tasks needed for initializing an object. (T/F) F 39. The constructor method takes zero or more arguments, and the code inside the procedure block performs any tasks needed for initializing an object. (T/F) T 40. A line of code that contains arguments and instantiates an object passes the value of these arguments to the object’s New procedure. (T/F) T © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
41. In the following code block, which of the following represents the line of code that assigns the value of the Name property? Class Student Private m_name As String Public Property Name() As String Get Return m_name End Get Set(Value As String) m_name = Value End Set End Property End Class
(A) End Class (B) m_name = Value (C) Return m_name (D) Private m_name As String B 42. In the following code, which of the following represents the line of code that guarantees that the variable m_name cannot be directly accessed from outside a Student object? Class Student Private m_name As String Public Property Name() As String Get Return m_name End Get Set(Value As String) m_name = Value End Set End Property End Class
(A) End Class (B) m_name = Value (C) Return m_name (D) Private m_name As String D 43. The _________________ feature of Visual Basic allows certain types of property blocks to the written with neither a Get or a Set block? (A) default properties (B) self-implemented properties (C) auto-implemented properties (D) self-explanatory properties C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Section 11.2 Arrays of Objects; Events; Containment 1.
Which of the following is an example of an array declaration whose data type is a user-defined object? (A) Dim students(50) As String (B) Dim students(50) As Student (C) Dim students(50) As Integer (D) Dim students(50) As TextBox B
2.
Which of the following statements best applies to the term user-defined events? (A) User-defined events represent specific instances of a user-defined type. (B) User-defined events are used to communicate changes of properties, errors, and the progress of lengthy operations. (C) User-defined events are predefined objects with physical manifestations. (D) User-defined events are populated with objects that hold data, have properties, respond to methods, and raise events. B
3.
Statements of which of the following kind should be placed in the Declarations section of a class code block? (A) Public Event UserDefinedEvent(par1 As dataType1, par2 As dataType2, ...)
(B) RaiseEvent UserDefinedEvent(arg1, arg2, ...) (C) Dim WithEvents object1 As ClassName (D) Private Sub object1_UserDefinedEvent(par1, par2, ...) _ Handles object1.UserDefinedEvent
A 4.
Statements of which of the following kind should be placed in the form’s code to declare an instance of a class that contains an event? (A) Public Event UserDefinedEvent(par1 As dataType1, par2 As dataType2, ...) (B) RaiseEvent UserDefinedEvent(arg1, arg2, ...) (C) Dim WithEvents object1 As ClassName (D) Private Sub object1_UserDefinedEvent(par1, par2, ...) _ Handles object1.UserDefinedEvent
C
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
5.
Statements of which of the following kind should be placed at locations in the class block code at which the event should be raised? (A) Public Event UserDefinedEvent(par1 As dataType1, par2 As dataType2, ...)
(B) RaiseEvent UserDefinedEvent(arg1, arg2, ...) (C) Dim WithEvents object1 As ClassName (D) Private Sub object1_UserDefinedEvent(par1, par2, ...) _ Handles object1.UserDefinedEvent
B 6.
The header for an event procedure for an instance of a class will be of the following type: (T/F) Private Sub object1_UserDefinedEvent(par1 As DataType1,_ par2 As DataType2, ...) Handles object1.UserDefinedEvent
T 7.
The statement for raising a user-defined event is located in the form’s code and the event is dealt with in the class block of code. (T/F) F
8.
The statement for raising a user-defined event is located in the class block of code and the event is dealt with in the form’s code. (T/F) T
9.
The keyword WithEvents is inserted into the standard declaration statement for instantiation of a class to allow the instance to respond to an event. (T/F) T
10. It can be said that class Student contains class College when a member variable of class Student makes use of an object of type class College. (T/F) T Section 11.3 Inheritance 1.
Which of the following terms does not describe a relationship between classes? (A) use (B) subclass (C) inheritance (D) containment B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
2.
Which of the following best defines the term hierarchy? (A) the process by which the properties, methods, and events of one class are passed onto another class (B) the collection of a parent class along with its descendants (C) when a member variable of class A makes use of an object of type class B (D) when one class manipulates or changes objects of another class B
3.
Which of the following best defines the term inheritance? (A) the process by which the properties, methods, and events of one class are passed onto another class (B) the collection of a parent class along with its descendants (C) when a member variable of class A makes use of an object of type class B (D) when one class manipulates or changes objects of another class A
4.
In the hierarchy chart shown below, GrandChild1 has access to each of the following items (as a result of inheritance), except one. Which of the following is GrandChild1 NOT able to access?
Parent Property A Sub B
Child1
Child2 Event C
GrandChild1 Function E Sub F
Child3 Property D
GrandChild2 Event G
(A) Property A (B) Property D (C) Event C (D) Sub B B
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
5.
In the hierarchy chart shown below, which of the following is GrandChild2 able to access? Parent Property A Sub B
Child1
Child2 Event C
GrandChild1 Function E Sub F
Child3 Property D
GrandChild2 Event G
(A) Property A (B) Function E (C) Property D (D) Sub F A 6.
Which of the following is a benefit of inheritance? (A) Inheritance leads to the duplication of code in multiple classes. (B) Inheritance allows two or more classes to share some common features yet differentiate themselves on others. (C) Inheritance allows variables to be collected in arrays. (D) Inheritance allows Visual Basic programs to directly connect to databases. B
7.
Inheritance does not enhance code reusability. (T/F) F
8.
Inheritance is the process by which the parent or base class inherits the properties, methods, and events of the child or derived class. (T/F) F
9.
The programmer needs to be able to identify useful hierarchies of classes and derived classes in working with object-oriented programming. (T/F) T
10. The ISA test is one guideline used in determining when and how to establish a hierarchy. It posits that if one class is a more specific case of another class, then it should be derived from that other class. (T/F) T
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
11. The class interface is a set of properties, methods, and events that define how the class should behave. (T/F) T 12. The feature that two classes can have methods with different names but that have the same purpose (and essentially the same implementation), is known as polymorphism. (T/F) F 13. In inheritance, the keyword Overridable is used to designate the parent’s methods that may be overridden by its children. (T/F) T 14. In inheritance, the keyword Overrides is used to designate the parent’s methods that are overridden by its children. (T/F) F 15. The keyword MustInherit is used to declare an abstract base class. (T/F) T In the exercises below, fill in the blank with one of the following six words: a. user-defined d. interface
b. polymorphism e. uses
c. derived f. Get
16. The set of properties, methods, and events for a class is called a class ____________. interface 17. The feature that two classes can have behaviors that are named the same and have essentially the same purpose but different implementations is called _____________. polymorphism 18. In a Property block, the _____ property procedure retrieves the value of a member variable. Get 19. In addition to the predefined events for classes, such as New, other events can be defined by the programmer to communicate changes of properties, errors, and the progress of lengthy operations. Such events are called _____________ events. user-defined 20. One class_____________ another class if it manipulates objects of that class. uses 21. If class A inherits the properties, methods, and events of class B, then class A is referred to as a _________ class of class B. derived
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.