Cs80 midterm Name: ________________________
Short answer (one or two sentences, 30 points):
1) The = operator does different things depending on context. Explain, with examples.
compare: if x = 7 then
assign: x = 7
2) Give me an example of "nesting".
if inside another if
3) What does Option Explicit On do?
makes you declare variables
4) Write me a comment, using Visual Basic syntax.
‘ hi mom!
5) What would happen when I run the following snippet? Why?
Dim X as String
TextBox1.Text = X.Length
crash, x is nothing and Nothing.Length would cause an exception
6) Give me an example of a logic error.
avg = first + second / 2
7) What is a breakpoint?
place program stops to allow debugging
8) What is the difference between AndAlso and And?
short-circuiting.
9) What is a property? Give an example.
txtBox1.text
10) If I had a TextBox called TextBox1, what code could I write to give it the focus?
txtBox1.Focus()
Multiple Choice (30 points)
1) To perform concatenation on strings, I would use:
a) &
b) +
c) -
d) More than one of the above
2) If I want to store the value True or False in a variable, that variable should ideally be of type:
a) String
b) Boolean
c) Double
d) Integer
3) To declare a variable, I would use the keyword:
a) Dim
b) Sum
c) Declare
d) Function
4) If I compare two text strings:
Dim X as String = "Hello"
Dim Y as String = "Goodbye"
If X < Y Then ...
a) "Hello" will be greater
b) "Goodbye" will be greater
c) We cannot compare them
d) There is no way of knowing which will be greater
5) The following is NOT a keyword in Visual Basic:
a) OrElse
b) As
c) Textbox
d) Case
6) The expression value of 6 + 2 / 2 is:
a) 4
b) 5.5
c) 16
d) 7
Debugging (20 points):
What is the error in the following code? How would you fix it?
1)
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
Dim num As Double = 0.5
If (1 < num < 3) Then
txtOutput.Text = "Number is between 1 and 3."
End If
End Sub
2)
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
If (2 <> 3)
txtOutput.Text = "Numbers are not equal"
End If
End Sub
3)
Dim hiyo As String
hiyo = "Silver"
txtBox = "Hi-Yo " & hiYo
4)
Dim balance, deposit As Double
balance = 1,234
deposit = $100
lstOutput.Items.Add(balance + deposit)
Tracing (10 points)
| X | Y |
Dim X as Integer, Y as Double | 0 | 0 |
X = 6 | 6 |
|
Y = X * 2 |
| 12 |
lstOutput.Items.Add(Y + 1000) |
|
|
X = 8 | 8 |
|
If X < 100 Then |
|
|
X = X * 100 | 800 |
|
EndIf |
|
|
If X < 100 Then |
|
|
X = Y * 100 |
|
|
ElseIf X > 100 Then |
|
|
Y = 2 |
| 2 |
EndIf |
|
|
Programming (20 points):
1) Calculate the amount of a waiter's tip, given the amount of the bill and the percentage tip as input.
2) Write a program that requests a letter, converts it to uppercase, and gives its first position in the sentence "THE QUICK BROWN FOX JUMPS OVER A LAZY DOG." For example, if the user responds by typing b into the text box, then the message "B first occurs in position 10." is displayed.
3) Write a program to handle a savings-account withdrawal. The program should request the current balance and the amount of the withdrawal as input and then display the new balance. If the withdrawal is greater than the original balance, the program should display "Withdrawal denied." If the new balance is less than $150, the message "Balance below $150" should be displayed.
4) Write a program to determine how much to tip the server in a restaurant. The tip should be 15 percent of the check, with a minimum of $1.