Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
Exception Handling MCQ Part – 8 | Java throw Keyword MCQ | Also see:- Exception Handling Interview Questions in Java, MCQ in Java
Q1) Find the output of the below program?
public class Test {
static ArithmeticException e = new ArithmeticException();
public static void main(String[] args) {
throw e;
}
}
a) Compile-time error
b) ArithmeticException
c) ArithmeticException: / by zero
d) None of these
View Answer
Answer:- b) ArithmeticExceptionThe throw keyword can be used to throw the exception.
Q2) Find the output of the below program?
public class Test {
static ArithmeticException e;
public static void main(String[] args) {
throw e;
}
}
a) Compile-time error
b) ArithmeticException
c) NullPointerException
d) None of these
View Answer
Answer:- c) NullPointerExceptionWe are trying to throw “e” but this variable is not initialized yet. By default static variables hold null value, therefore we are getting NullPointerException.
Q3) Find the output of the below program?
public class Test {
public static void main(String[] args) {
throw new NumberFormatException();
}
}
a) Compile-time error
b) ArithmeticException
c) NullPointerException
d) NumberFormatException
View Answer
Answer:- d) NumberFormatExceptionQ4) Find the output of the below program?
public class Test {
public static void main(String[] args) {
throw new Test();
}
}
a) Compile-time error
b) ArithmeticException
c) NullPointerException
d) NumberFormatException
View Answer
Answer:- a) Compile-time errorTest class is not a direct/indirect child of Throwable class therefore it gives compile time error:- incompatible types: Test cannot be converted to Throwable => throw new Test();
Q5) Find the output of the below program?
public class Test extends Exception {
public static void main(String[] args) {
try {
throw new Test();
} catch(Exception e) {
System.out.println("catch");
}
}
}
a) Compile-time error
b) Exception
c) catch
d) None of these
View Answer
Answer:- c) catchIn this program, Test class is a child class of Exception, therefore indirectly it is a child class of Throwable. Hence, it can throw Test class objects.
Q6) Find the output of the below program?
class A extends ArithmeticException {}
public class Test {
public static void main(String[] args) {
try {
throw new A();
} catch(Exception e) {
System.out.println("catch");
}
}
}
a) Compile-time error
b) Exception
c) catch
d) None of these
View Answer
Answer:- c) catch“A” class extends from the ArithmeticException class so it is an indirect subclass of Throwable. When “throw new A()” is called then this line will throw “A”, and the catch block is handling “Exception” which is super class therefore it is valid and the exception will be handled through the catch block.
Q7) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.println(10/0);
System.out.println("KP");
}
}
a) Compile-time error
b) ArithmeticException
c) KP
d) None of these
View Answer
Answer:- b) ArithmeticExceptionQ8) Find the output of the below program?
public class Test {
public static void main(String[] args) {
throw new ArithmeticException("/ by zero");
System.out.println("KP");
}
}
a) Compile-time error
b) ArithmeticException
c) KP
d) None of these
View Answer
Answer:- a) Compile-time errorWhenever we are using the throw keyword directly in the program then we can’t place any other statements after that. Hence we will get a compile time error:- unreachable statement.
In the previous program for the statement “System.out.println(10/0);” compiler don’t know whether this line will raise any exception or not, but in this program for the statement “throw new ArithmeticException(“/ by zero”);” compiler knows that 100% exception will be raised.
Q9) Find the output of the below program?
public class Test {
public static void main(String[] args) {
if(true) throw new ArithmeticException("/ by zero");
System.out.println("KP");
}
}
a) Compile-time error
b) ArithmeticException
c) KP
d) None of these
View Answer
Answer:- b) ArithmeticExceptionWe are using if block to place a “throw” statement, and if block will be executed only when the condition becomes true. Compiler checks only syntax but not the condition of the if block. Hence, the compiler is not sure whether the if block will be executed or not therefore we can place statements after the if block.
Q10) Find the output of the below program?
public class Test {
public static void main(String[] args) {
if(false) throw new ArithmeticException("/ by zero");
System.out.println("KP");
}
}
a) Compile-time error
b) ArithmeticException
c) KP
d) None of these
View Answer
Answer:- c) KPThe condition of if block is false therefore if block will not be executed.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!