What RuntimeException will the following programs throw, if any? public class Te
ID: 3824723 • Letter: W
Question
What RuntimeException will the following programs throw, if any? public class Test {public static void main (String[] args) {System. out.println(1/0);}} public class Test {public static void main (String[] args) {int[] list = new int[5]; System.out.println(list[5]);}} public class Test {public static void main(String[] args) {Sring s = "abs"; System.out.println(s. charAt(3));}} public class Test {public static void main(String[] args) {Object o = new Object (); String d = (String) o;}} public class Test {public static void main(String[] args) {Object o = null; System.out.println(o. toString());}} public class Test {public string void main (String[] args) {System.out.println(1.0/0);}}Explanation / Answer
Answers)
a) Code gives ArithmeticException :/ by zero
because any number divide by zero gives infinite value therefore gives exception.
b) Code gives ArrayIndexOutOfBoundsException
because in this code list of 5 element is created and list has index 0 to 4. we can access list elements by list[0]....list[4]. Index 5 is i.e. list[5] is out of list bound threfore gives exception.
c) Code gives StringIndexOutOfBoundsException: String index out of range
because, character in a string started from range 0...n-1. In above code abc has index 0,1,2 respectively. Therefore charAt(3) gives StringIndexOutOfBoundsException.
d) code gives ClassCastException
because java.lang.Object cannot be cast to java.lang.String
e) code gives NullPointerException
because object o is null
f) code do not throw any exception it gives output Infinity
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.