70-306 Question

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

  1. Guest

    Guest Guest

    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
    #1
    1. Advertising

  2. Guest

    Kline Sphere Guest

    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
    #2
    1. Advertising

  3. Guest

    someone Guest


    >-----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
    #3
  4. Guest

    Guest Guest

    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
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian
    Replies:
    0
    Views:
    396
    Brian
    Jan 10, 2005
  2. Al Green

    exam choice: 72-306 ? or 70-306?

    Al Green, Jul 5, 2005, in forum: MCSD
    Replies:
    1
    Views:
    1,406
    Cindy Winegarden
    Jul 9, 2005
  3. Ashish Patel
    Replies:
    0
    Views:
    299
    Ashish Patel
    Feb 26, 2004
  4. Jon
    Replies:
    2
    Views:
    415
    Sharon
    Jul 7, 2004
  5. Sean
    Replies:
    0
    Views:
    252
Loading...

Share This Page