VB+Test+1

__ICS3M – First Summative Test__ 1. In class, we talked about using appropriate prefixes for each object on your form. Fill in the blanks in the following table [2 marks]:

Command Button- cmd Label - lbl Text Box - txt

Image - img

2. Explain the difference between a local variable and a global variable. [2 marks] These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called. These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled. || 3.
 * ** Local **
 * Global **
 * || The value of intValue would be 4.43. I know this because, originally, the dblValue variable is defined as equaling 4.43. The next line of code states that intValue is equivalent to dblValue. This means that intValue takes on the value of the dblValue variable. ||

“dblValue” is declared as a double and “intValue” is declared as an integer. What is the value of intValue after the following lines of code are executed? Explain your reasoning in the box. [2 marks]

dblValue = 4.43 intValue = dblValue

4. What happens in computer memory when you declare a variable? Draw a diagram and use an example (of an integer counter) to explain. [3 marks] An example of an integer counter would be having a button that someone can click. Each time they click the button, the value of a variable intCount would increase by one. This variable is holding the value of the number of clicks within intCount, to be used later on. || 5. List 3 ways we make our code easy to read for other people? [3 marks] 2. Use proper naming conventions. 3. Use organization (define as many variables together as possible, in one location!) || 6. Write one line of code that would properly define pi as a __constant__ (pi is 3.14). [1 mark] Const conPi = 3. 14159265358979
 * Variables are the memory locations which are used to store values temporarily. A defined naming strategy has to be followed while naming a variable. A variable name must begin with an alphabet letter and should not exceed 255 characters. It must be unique within the same scope. It should not contain any special character like %, &, !, #, @ or $.
 * 1. Comments.

7. Explain what the “rnd” function does and how it is used with “Randomize”. [4 marks] Dim rndRand As New Random = Declaring a new random object! Why am I referring to an “integer __ variable __ ”, but a “random __ object __ ”? Because Visual Basic has data types like integer, decimal, string and so on. It also has things called CLASSES. What we are doing with the Random bit here is **__ creating a new object of the Random class __**.
 * Dim intRand As Integer = Declaring a simple integer variable.

Classes are different from variables because // variables simply hold values //. That’s it. Classes are more powerful because they have their own methods. This means that they are __ programmed to do stuff for you __ ! So every “Random” object comes with a method (called Next) that will generate a random number for you and you can even define the minimum and maximum bounds.

So after I create “rndRand” (an object that is an instance of the Random class), I can use the built –in “Next” method to generate a random number between (for example) 10 and 20 as follows:

intRandomNumber = rndRand.Next(10, 20)

The above line of code would create a random number between 10 and 20 and store that value in the variable called “intRandomNumber”. || 8. Write a chunk of //code// that declares a variable “intCounter”, and gives it an initial value of 50 (i.e. that’s the value it is when the program first runs). [3 marks] Dim intCounter As New Integer intCounter = 50 End Sub End Class || 9. CODING: Name the objects as you wish using the appropriate conventions and comment your code. [6 marks] || Dim intValue As Integer = txtInput.Text If (intValue) < 50 Then lblOutput.Text = "Small Number" ElseIf (intValue) > 50 Then lblOutput.Text = "Large Number" End If End Sub || What will happen if the user inputs 50 and clicks the button? [1 mark] NOTHING, Since it only checks for values > or < 50!!!!!
 * Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load
 * || For this form, write code such that if the user puts a value below 50 in the text box and clicks the button, “Small Number” is displayed as the label caption. If the user puts a value above 50 and clicks the button, “Large Number” is displayed as the label caption.
 * Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

10. Write a loop (use any of the 3 we learned in class) that displays the numbers 2, 4, 6, 8, 10, …, 200 in a text box when a button is clicked. [4 marks] Dim x As Integer For x = 200 To 2 Step -2 lblOutput.Text = x & vbNewLine & lblOutput.Text Next x End Sub || 1. You are the teacher and a student submits this code. Examine the code and rewrite it, correcting all of the problems you see. Ensure proper form, tabs and naming conventions. Don’t worry about commenting the code or what the code does. Use a scrap or blank piece of paper if you need more room. [4 marks]  ‘ Jon Swaine ‘ April 17, 2008 Dim intyearsold as Integer Dim price as Double Dim word, input, output as String Const year = 2008 **//__Dim constYear As Constant__//** Private Sub Command1_Click intyearsold = year - Int(txtinput.Text) If intyearsold > 9 Then price = 50 word = “Old” MsgBox = “This is “ & word & “ - it’s “ & intyearsold & “ years old” Else price = 100 word = “New” MsgBox = “This is “ & word & “ - it’s “ & intyearsold & “ years old” End If   End Sub Private Sub Form_Load input = “Hi” output = “Bye” End Sub
 * Private Sub btnClick_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click