Question

In: Computer Science

What is the output of the following code? Explain your answer in a paragraph or two....

What is the output of the following code? Explain your answer in a paragraph or two.

class ExceptionThrown {
static int divideByZero(int a, int b){
int i = a/b;
return i;
}   
static int computeDivision(int a, int b) {
int res =0;
try{   
res = divideByZero(a,b);
}
catch(NumberFormatException ex){
System.out.println("NumberFormatException is occured");
}   
return res;
}   
public static void main(String args[]){
int a = 1;   
int b = 0;
try{
int i = computeDivision(a,b);
}
catch(ArithmeticException ex){   
// getMessage will print description of exception(here / by zero)
System.out.println(ex.getMessage());
}
}
}

Solutions

Expert Solution

Please upvote if you are able to understand this and if there is any query do mention it in the comment section.

CODE:

class ExceptionThrown {
static int divideByZero(int a, int b) {//creating a static method with int returntype
int i = a / b;
return i;
}
static int computeDivision(int a, int b) {//creating a static method with int returntype
int res = 0;
try {//using try catch for exception handling
res = divideByZero(a, b);//calling the function divideByZero
} catch(NumberFormatException ex) {//using the class NumberFormatException
System.out.println("NumberFormatException is occured");
}
return res;
}
public static void main(String args[]) {//main function
int a = 1;
int b = 0;
try {//using try catch for exception handling
int i = computeDivision(a, b);//calling the function computeDivision
} catch(ArithmeticException ex) {//using the class ArithmeticException
System.out.println(ex.getMessage());//using the getMessage method of ArithmeticException class
}
}
}

OUTPUT:

A class is created ExceptionThrown which has two static methods name divideByZero in which the two arguments a and b are passed that performs a / b operation and return. The other static method computeDivision uses the try catch block for exception handling. In the computeDivision the method divideByZero is called with a and b as the parameters which were arguments for computeDivision method. If the two integers will not be integers then the exception will be catched using NumberFormatException class. Then in the main method there are two integer variables a = 1 and b = 0. Again try catch block is used. The computeDivision is called with parameters a and b. As when a / b will be performed on calling computeDivision and on calling computeDivision divideByZero will be called. So a / b which is 1 / 0 will be performed. This will raise an error which is handled in the catch block using the ArithmeticException class and it is printed with the getMessage() method.


Related Solutions

What is behavioral economics? Explain your answer in at least a paragraph.
What is behavioral economics? Explain your answer in at least a paragraph.  How is it different than standard economic theory? Explain your answer in at least a paragraph. 
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
What output is produced by the following code?   Explain how it works. public class A {...
What output is produced by the following code?   Explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void main(String[]...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
Determine Theta for the following code fragments in the average case. Explain your answer. a =...
Determine Theta for the following code fragments in the average case. Explain your answer. a = b + c; d = a + e; sum = 0; for (i=0; i<3; i++) for (j=0; j sum++; 3. sum = 0; for (i=1; i<=n; i*=2) for (j=1; j<=n; j++) sum++; 4. Assume that array A contains n values, Random takes constant time, and sort takes n log n steps. for (i=0; i for (j=0; j A[j] = Random(n); sort(A, n); 5. Assume...
Explain the monetary transmission process prior to 2008. Explain your answer in a paragraph of at...
Explain the monetary transmission process prior to 2008. Explain your answer in a paragraph of at least 300 words.
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT