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) Which exception will be raised?
public class Test {
public static void main(String[] args) {
m1();
}
public static void m1() {
m2();
}
public static void m2() {
m1();
}
}
a) StackOverflowError
b) OutOfMemoryError
c) UnknownError
d) VirtualMachineError
View Answer
Answer:- a) StackOverflowErrorIn Java, every thread keeps a record of method calls inside the stack. In this program, the m1() method is called the m2() method and the m2() method is called m1() method therefore StackOverflowError will be raised.
Q2) Which exception will be raised?
public class Test {
static int x = 9/0;
public static void main(String[] args) {
System.out.println(x);
}
}
a) ArithmeticException
b) ExceptionInInitializerError
c) OutOfMemoryError
d) Exception will not be raised
View Answer
Answer:- b) ExceptionInInitializerErrorAny exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.
Q3) Which exception will be raised?
public class Test {
static int arr[] = new int[-5];
public static void main(String[] args) {
System.out.println(arr);
}
}
a) ArithmeticException
b) OutOfMemoryError
c) ExceptionInInitializerError
d) Exception will not be raised
View Answer
Answer:- c) ExceptionInInitializerErrorAny exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.
Q4) Which exception will be raised?
public class Test {
static int arr[] = new int[5];
static {
System.out.println(arr[10]);
}
public static void main(String[] args) {
System.out.println(arr);
}
}
a) ArithmeticException
b) OutOfMemoryError
c) ExceptionInInitializerError
d) Exception will not be raised
View Answer
Answer:- c) ExceptionInInitializerErrorAny exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.
Q5) Which exception will be raised?
public class Test {
int arr[] = new int[-5];
public static void main(String[] args) {
System.out.println("A");
}
}
a) ArithmeticException
b) NegativeArraySizeException
c) ExceptionInInitializerError
d) Exception will not be raised
View Answer
Answer:- d) Exception will not be raisedThe array is instance type but not static, therefore ExceptionInitializerError will never be raised. In the array, size is -5 which is invalid therefore it should raise a NegativeArraySizeException, but the instance variable will be initialized only when we create an object. Hence, in this program exceptions will not be raised, the program runs successfully and gives output:- “A”.
Q6) Which exception will be raised?
public class Test {
int arr[] = new int[-5];
public static void main(String[] args) {
Test t1 = new Test();
System.out.println("A");
}
}
a) ArithmeticException
b) NegativeArraySizeException
c) ExceptionInInitializerError
d) Exception will not be raised
View Answer
Answer:- b) NegativeArraySizeExceptionIn this program we are creating an object of Test class therefore the instance variable will be initialized and NegativeArraySizeException will be raised.
Q7) Which exception will be raised?
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException e) {
throw new NullPointerException();
}
}
}
a) ArithmeticException
b) NullPointerException
c) Compile-time error
d) Compiled successfully, Exception will not be raised, but no output
View Answer
Answer:- b) NullPointerExceptionIn this program, the statement inside the try block raises ArithmeticException. The catch block handled this exception and raised the NullPointerException. Therefore JVM will raise NullPointerException, but not ArithmeticException.
Q8) Which exception will be raised?
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException e) {
throw new StackOverflowError();
}
}
}
a) ArithmeticException
b) StackOverflowError
c) OutOfMemoryError
d) VirtualMachineError
View Answer
Answer:- b) StackOverflowErrorThe try block raised ArithmeticException, the catch block handled it and threw StackOverflowError.
Q9) Which exception will be raised?
public class Test {
public static void main(String[] args) {
try {
Integer.parseInt("two");
} catch(ArithmeticException e) {
throw new NullPointerException();
}
}
}
a) ArithmeticException
b) NullPointerException
c) NumberFormatException
d) Compiled successfully, Exception will not be raised, but no output
View Answer
Answer:- c) NumberFormatExceptionThe try block raised a NumberFormatException, but the catch block is only handling ArithmeticException. Hence, NumberFormatException will be raised.
Q10) Which exception will be raised?
public class Test {
static {
String s = null;
System.out.println(s.length());
}
public static void main(String[] args) { }
}
a) ArithmeticException
b) NegativeArraySizeException
c) ExceptionInInitializerError
d) Exception will not be raised
View Answer
Answer:- c) ExceptionInInitializerErrorAny exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.
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!