Write an explanation for the correct answers for items 1 3

Write an explanation for the correct answers for items 1 - 3. SCENARIO: Lionel is a student at the School of Fine Art. To help pay his tuition, he took a job in the school’s student records department. He developed a program that calculates each student’s grade point average (GPA). He thought it would be simple, because he knows that the formula is: total grade points divided by total credit hours attempted However, Lionel’s program crashes with new students who have not attempted any credit hours, instead of just giving them a GPA of 0.0. Here is his code in C# (line numbers added): 1: double GPA; 2: GPA = totalGP / creditHoursAttempted; 3: Console.WriteLine(GPA); 1. Given what we know about the problem, what is the most likely exception this code will throw? a. DivideByZeroException b. NullReferenceException c. StackOverflowException 2. What structure should Lionel use to handle the exception? a. do-catch b. catch-exception c. try-catch 3. Which of the following should Lionel’s catch-block probably include? a. creditHoursAttempted = 1.0; b. totalGP = 4.0; c. GPA = 0.0;

Solution

Please find below the answers :

1) what is the most likely exception this code will throw?

Ans: a. DivideByZeroException : because in the code to find GPA, we are dividing totalGP by creditHoursAttempted, so if creditHoursAttempted is 0, then it will throw DivideByZeroException

2) What structure should Lionel use to handle the exception?

Ans: c. try-catch : try-catch is a kind of exception handling mechanism in which can catch an exception which may expect to happen in the try block.

3) Which of the following should Lionel’s catch-block probably include?

Ans:  c. GPA = 0.0; Because if creditHoursAttempted is 0, then it will throw a DivideByZeroException from the try block, and this exception will be caught in the catch block, so inorder to gracefully exit the program we need to show user the GPA is 0, thus we have to set GPA = 0.0;

Write an explanation for the correct answers for items 1 - 3. SCENARIO: Lionel is a student at the School of Fine Art. To help pay his tuition, he took a job in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site