In: Computer Science
2. Will the following statements cause any error? (Yes/No) If a statement causes an error, what type of errors it is? (logic error, syntax/compile error, or runtime error). Again, there is no need to do the java code and test in jGrasp. You don’t need to provide the error message from the compiling or running results. The types of the error will be enough.
a. byte temp = 850;
b. celsius = 5 / 9 * (fahrenheit – 32);
c. int x = 3 / 0;
d. int y = 7 / 3.0;
e. int z = (int) 2.34567;
f. int value = 2147483647 + 1;
/* a. byte temp = 850; Solution:-> Compile time error. Since 850 is a int type and incompatible with byte type. b. celsius = 5 / 9 * (fahrenheit – 32); Solution:-> It is logical compile time error. since celsius and fahrenheit is not declared any anywhere in the program c. int x = 3 / 0; Solution:-->Run time error. Since it is a correct syntax so no compile time error. but you are dividing by zero. So a run time error occurs d. int y = 7 / 3.0; Solution: -->It is compile time error. Since you are assigning a float value to int type. Which is not possible otherwise you need to cast it before assigning. e. int z = (int) 2.34567; Solution :--> Logical error, Since there is a loss of data. You are assigning a float to int which result is 2 f. int value = 2147483647 + 1; Solution :--> Logical error, Since int is now out of its range , int range in java is -2,147,483,648 to 2,147,483, 647. So after adding 1 it goes to negative side and result is -2147483648. */ //If you need any help regarding this solution .......... please leave a comment ...... thanks