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
Q1) What are the different methods that can be used to get the exception message?
a) printStackTrace()
b) toString()
c) getMessage()
d) All of these
View Answer
Answer:- d) All of theseWe can use printStackTrace(), toString(), and getMessage() to get the exception message. See More:- Different Ways to Get Exception Message in Java
Q2) The below exception message is given by?
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:3)
a) JDH (Java Default Handler) or JVM
b) printStackTrace()
c) toString()
d) getMessage()
View Answer
Answer:- a) JDH (Java Default Handler) or JVMQ3) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException e) {
System.out.println(e);
}
}
}
a) / by zero
b) java.lang.ArithmeticException: / by zero
c) java.lang.ArithmeticException: / by zero; at Test.main(Test.java:4)
d) None of these
View Answer
Answer:- b) java.lang.ArithmeticException: / by zeroThe toString() method display the exception in the given format:- Exception name: <reason-of-the-exception>
Q4) Which exception class can be used to handle all types of exceptions?
a) Exception
b) Error
c) RuntimeException
d) Anyone from these options
View Answer
Answer:- a) ExceptionTo catch all exceptions in Java we should use the “Exception” class. See More:- How to catch All Exceptions in Java
Q5) Which exception will be raised?
int arr[] = new int[5];
System.out.println(arr[20]);
a) ArithmeticException
b) NegativeArraySizeException
c) ArrayIndexOutOfBoundsException
d) Compile time error
View Answer
Answer:- c) ArrayIndexOutOfBoundsExceptionThe array contains only 5 elements but we are trying to access the 20th element, hence it raises ArrayIndexOutOfBoundsException.
Q6) Which exception will be raised?
int arr[] = new int[5];
System.out.println(arr[-2]);
a) ArithmeticException
b) NegativeArraySizeException
c) ArrayIndexOutOfBoundsException
d) Exception will not be raised
View Answer
Answer:- c) ArrayIndexOutOfBoundsExceptionThe array contains 5 elements and their index is from 0 to 4 but we are trying to access the -2th element which is invalid, hence it raises ArrayIndexOutOfBoundsException.
Q7) Which exception will be raised?
int arr[] = new int[-5];
System.out.println(arr[1]);
a) ArithmeticException
b) NegativeArraySizeException
c) ArrayIndexOutOfBoundsException
d) Compile-time error
View Answer
Answer:- b) NegativeArraySizeExceptionThe array size must be a valid positive integer number.
Q8) Which exception will be raised?
Object obj = new Object();
String str = (String)obj;
a) ClassCastException
b) ArithmeticException
c) NoClassDefFoundError
d) Exception will not be raised
View Answer
Answer:- a) ClassCastExceptionParent class object can’t convert to the child class object directly. In this case we will get:- Exception in thread “main” java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.lang.String (java.lang.Object and java.lang.String are in module java.base of loader ‘bootstrap’)
Q9) Which exception will be raised?
String str = new String("KnowProgram");
Object obj = (Object)str;
a) ArithmeticException
b) NoClassDefFoundError
c) ClassCastException
d) Exception will not be raised
View Answer
Answer:- d) Exception will not be raisedChild class objects can be converted to the parent class object type therefore exceptions will not be raised in this program.
Q10) Which exception will be raised?
Object obj = new String("KnowProgram");
String str = (String)obj;
a) ClassCastException
b) ArithmeticException
c) NoClassDefFoundError
d) Exception will not be raised
View Answer
Answer:- d) Exception will not be raisedThe object is of parent type but internally it holds the child type object, but not the parent type type object. Therefore it can be converted to the child type. Hence, exceptions will not be raised.
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!