# 70-306 Question

Discussion in 'MCSD' started by Guest, Nov 5, 2003.

1. ### GuestGuest

This is a sample 70-306 question.

There are 2 textboxes textbox1, textbox2 and a button
called button1 on a form. The user enters values in the
textboxes which is a loan amount and number of years. On
clicking button1, the interest is calculated. The
following is the code to calculate interest on clicking
button1.

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim retvalue As Integer
Try
retvalue = validateloanamount()
If retvalue > 0 Then
MessageBox.Show("calculated succesfully")
End If
Catch ex As OverflowException
MessageBox.Show("Overflow exception")
Catch ex As ArithmeticException
MessageBox.Show("arithmatic exception")
End Try
End Sub

Function validateloanamount() As Integer
Return calculateinterest(CShort(TextBox1.Text),
CShort(TextBox2.Text))
MessageBox.Show("Rate calculated successfully")
End Function

Function calculateinterest(ByVal p As Short, ByVal n As
Short) As Integer

Dim interest As Integer
Dim r As Double
r = 5.6
interest = (p * n * r) / 100

If interest < 0 Then
MessageBox.Show("error while calculating interest")
Else
Return interest
End If
End Function

The question is : What is the output on execution if the
user enters 20000 as loan amount and 5 as years.

The options are
a)Rate calculated successfully
b)Calculated successfully
c)Overflow exception
d)arithmatic exception.

The answer is Overflow exception. Can someone explain why
this throws an overflow exception?

Thanks
H

Guest, Nov 5, 2003

2. ### Kline SphereGuest

It is to do with the fact that vb.net does not perform explicit
promotions. This highlights why 'option strict' should NOT be an
'option' at all!

Kline Sphere, Nov 5, 2003

3. ### someoneGuest

>-----Original Message-----
>This is a sample 70-306 question.
>
>There are 2 textboxes textbox1, textbox2 and a button
>called button1 on a form. The user enters values in the
>textboxes which is a loan amount and number of years. On
>clicking button1, the interest is calculated. The
>following is the code to calculate interest on clicking
>button1.
>
>Private Sub Button1_Click(ByVal sender As System.Object,
>ByVal e As System.EventArgs) Handles Button1.Click
> Dim retvalue As Integer
> Try
> retvalue = validateloanamount()
> If retvalue > 0 Then
> MessageBox.Show("calculated succesfully")
> End If
> Catch ex As OverflowException
> MessageBox.Show("Overflow exception")
> Catch ex As ArithmeticException
> MessageBox.Show("arithmatic exception")
> End Try
> End Sub
>
>Function validateloanamount() As Integer
> Return calculateinterest(CShort(TextBox1.Text),
> CShort(TextBox2.Text))
> MessageBox.Show("Rate calculated successfully")
>End Function
>
>Function calculateinterest(ByVal p As Short, ByVal n As
> Short) As Integer
>
> Dim interest As Integer
> Dim r As Double
> r = 5.6
> interest = (p * n * r) / 100
>
> If interest < 0 Then
> MessageBox.Show("error while calculating interest")
> Else
> Return interest
> End If
>End Function
>
>The question is : What is the output on execution if the
>user enters 20000 as loan amount and 5 as years.
>
>The options are
>a)Rate calculated successfully
>b)Calculated successfully
>c)Overflow exception
>d)arithmatic exception.
>
>The answer is Overflow exception. Can someone explain why
>this throws an overflow exception?
>
>Thanks
>H
>.
>

the overflow exception occurs in folowing line:
interest = (p * n * r) / 100
------
resume: it is too early for u to take the test

someone, Nov 7, 2003
4. ### GuestGuest

It's because p and n have been declared as Short. So when
they are multiplied together the answer should still
be 'Short'. However, this is not the case when p=20000
and n=5. Change the declaration of p to Integer and it
will work.
Here's another question for you on the same piece of
code. Which line of code will never be executed ?
Jez
>-----Original Message-----
>This is a sample 70-306 question.
>
>There are 2 textboxes textbox1, textbox2 and a button
>called button1 on a form. The user enters values in the
>textboxes which is a loan amount and number of years. On
>clicking button1, the interest is calculated. The
>following is the code to calculate interest on clicking
>button1.
>
>Private Sub Button1_Click(ByVal sender As System.Object,
>ByVal e As System.EventArgs) Handles Button1.Click
> Dim retvalue As Integer
> Try
> retvalue = validateloanamount()
> If retvalue > 0 Then
> MessageBox.Show("calculated succesfully")
> End If
> Catch ex As OverflowException
> MessageBox.Show("Overflow exception")
> Catch ex As ArithmeticException
> MessageBox.Show("arithmatic exception")
> End Try
> End Sub
>
>Function validateloanamount() As Integer
> Return calculateinterest(CShort(TextBox1.Text),
> CShort(TextBox2.Text))
> MessageBox.Show("Rate calculated successfully")
>End Function
>
>Function calculateinterest(ByVal p As Short, ByVal n As
> Short) As Integer
>
> Dim interest As Integer
> Dim r As Double
> r = 5.6
> interest = (p * n * r) / 100
>
> If interest < 0 Then
> MessageBox.Show("error while calculating interest")
> Else
> Return interest
> End If
>End Function
>
>The question is : What is the output on execution if the
>user enters 20000 as loan amount and 5 as years.
>
>The options are
>a)Rate calculated successfully
>b)Calculated successfully
>c)Overflow exception
>d)arithmatic exception.
>
>The answer is Overflow exception. Can someone explain

why
>this throws an overflow exception?
>
>Thanks
>H
>.
>

Guest, Nov 12, 2003