Which exception will raise in the below statement?String s = null;System.out.println(s.length());A) NumberFormatExceptionB) NullPointerExceptionC) ArithmeticExceptionD) None of these
Question
Which exception will raise in the below statement?String s = null;System.out.println(s.length());A) NumberFormatExceptionB) NullPointerExceptionC) ArithmeticExceptionD) None of these
Solution
The exception that will be raised in the given statement is a NullPointerException. This is because you are trying to access a method (.length()) on a null object (s). In Java, trying to call a method on a null object results in a NullPointerException. So, the correct answer is B) NullPointerException.
Similar Questions
Which exception is thrown when a string contains null?Options: Pick one correct answer from belowArithmeticExceptionClassNotFoundExceptionNullPointerExceptionNone of the above
Which of the following is a checked exception in Java?Select one:a. NullPointerExceptionb. ArrayIndexOutOfBoundsExceptionc. IOExceptiond. ArithmeticException
An attempt to convert string to a number type uses which of the following exception? Example- String s=”xyz” int i=Integer.parseInt(s);a) NumberFormatExceptionc) ArrayIndexOutOfBoundd) NullPointerb) AirthmeticException
What will be the output of the following Java program?class exception_handling { public static void main(String args[]) { try { System.out.print("Hello" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } }a) Hellob) Worldc) HelloWorldd) Hello World
public class Test { String msg = null; public void show(){ try { try { System.out.println(10/0); } catch(ArithmeticException e) { msg = e.getMessage(); //Line 1 } try { System.out.println("IITM".charAt(5)); } catch(StringIndexOutOfBoundsException e) { msg = e.getMessage(); //Line 2 } } finally { System.out.println(msg); } } public static void main(String[] args) { Test obj = new Test(); obj.show(); } }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.