Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1.In a Try / Catch / Finally block, what type of code goes in each bloc

ID: 3789006 • Letter: Q

Question

Question 1.In a Try / Catch / Finally block, what type of code goes in each block? In other words, what is the purpose of the code in each block?

Try
{
Which code goes here?

}
Catch
{

     Which code goes here?

}
Finally
{

                     Which code goes here?

}

Question 2.

Determine the output of the following code when the input is:
(Provide a response for values A, B and C.)

a) –1
b) 0
c) 12XY

Scanner scanner = new Scanner(System.in);

try {

     int num = scanner.nextInt();

     if (num != 0) {

          throw new Exception("Not zero");

}

System.out.println("I'm happy with the input.");

} catch (InputMismatchException e) {

     System.out.println("Invalid Entry");

} catch (Exception e) {

     System.out.println("Error: " + e.getMessage());

}

Question 4:

What will be the output from the following code?

                StringBuffer word1, word2;

                word1 = new StringBuffer(“Lisa”);

                word2 = word1;

                word2.insert(0, “Mona “);

                System.out.println(word1);

Question 3.Determine the output of the following code when the input is:
(This is similar to Exercise 1, but the code here has the finally clause.)


a) –1
b) 0
c) 12XY

Scanner scanner = new Scanner(System.in);

try {

     int num = scanner.nextInt();

     if (num != 0) {

           throw new Exception("Not zero");

}

System.out.println("I'm happy with the input.");

} catch (InputMismatchException e) {

     System.out.println("Invalid Entry");

} catch (Exception e) {

     System.out.println("Error: " + e.getMessage());

} finally {

     System.out.println("Finally Clause Executed");

}

Explanation / Answer

1)

Try
{
   Error pron code. Code that can throw exceptions
}
Catch
{
Exception handling code
}
Finally
{
Code that need to execute irrespective of whether exception has thrown or not
}

2)

a)

-1

Error: Not zero

b)

0

I'm happy with the input.

c)

12XY

Invalid Entry

3)

a)

-1

Error: Not zero

Finally Clause Executed

b)

0

I'm happy with the input.

Finally Clause Executed

c)

12XY

Invalid Entry

Finally Clause Executed

4)

Mona Lisa

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote