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 – 11 | Java throws Keyword MCQ | Also see:- Exception Handling Interview Questions in Java, MCQ in Java
Q1) Find the output of the below program?
import java.io.*;
public class Test {
public static void main(String[] args) {
PrintWriter pw = new PrintWriter("abc.txt");
pw.println("Hello");
}
}
a) “Hello” in the file
b) Compile-time error:- unreported exception FileNotFoundException
d) FileNotFoundException
e) None of these
View Answer
Answer:- b) Compile-time error:- unreported exception FileNotFoundExceptionThe FileNotFoundException is a checked exception. The PrintWriter() constructor throws FileNotFoundException therefore it must be handled either through catch block or throws keyword.
Q2) Which of the following statements is true about the “throws” keyword?
a) It can prevent raising an exception.
b) It is used only to convince the compiler, it can’t prevent raising an exception.
c) It can be used at the class level like- class A throws Exception { }
d) All of these
View Answer
Answer:- b) It is used only to convince the compiler, it can’t prevent raising an exception.The throws keyword required only to convince compiler & uses of throws keyword doesn’t prevent abnormal termination of the program.
Q3) Compile time error will occur in which method?
public class Test {
public static void main(String[] args) {
m1();
}
public static void m1() {
m2();
}
public static void m2() {
Thread.sleep(1000);
}
}
a) main()
b) m1()
c) m2()
d) Compiled Successfully
View Answer
Answer:- c) m2()In m2() method Thread.sleep() method which throws “InterruptedException”, which is a checked exception therefore m2() must catch or handle this exception.
Q4) Compile time error will occur in which method?
public class Test {
public static void main(String[] args) {
m1();
Integer.parseInt("two");
}
public static void m1() {
m2();
System.out.println(9/0);
}
public static void m2() {
Thread.sleep(1000);
}
}
a) main()
b) m1()
c) m2()
d) Compiled Successfully
View Answer
Answer:- c) m2()In this program, there are 3 exceptions which may be raised:- NumberFormatException in main(), ArithmeticException in m1(), and InterruptedException in m2() method. NumberFormatException, and ArithmeticException are unchecked exceptions but InterruptedException is an unchecked exception. The handling for unchecked exceptions is optional but handling for unchecked exceptions is compulsory.
Q5) Compile time error will occur in which method?
public class Test {
public static void main(String[] args) {
m1();
}
public static void m1() {
m2();
}
public static void m2()
throws InterruptedException {
Thread.sleep(1000);
}
}
a) main()
b) m1()
c) m2()
d) Compiled Successfully
View Answer
Answer:- b) m1()The m2() method propagates exceptions to the caller method i.e. m1() method. Hence the caller method must catch the exception or propagate to it’s caller method by using the “throws” keyword.
Q6) Compile time error will occur in which method?
public class Test {
public static void main(String[] args)
throws InterruptedException {
m1();
}
public static void m1()
throws InterruptedException {
m2();
}
public static void m2()
throws InterruptedException {
Thread.sleep(1000);
}
}
a) main()
b) m1()
c) m2()
d) Compiled Successfully
View Answer
Answer:- d) Compiled SuccessfullyThe m2() method throws InterruptedException and propagates to the caller method m1(). The m1() method also propagates InterruptedException to the caller method main(). Similarly, the main() also propagates InterruptedException to the JVM. Hence, compile will not give any compile time error.
Q7) The use of the “throws” keyword has no impact on?
a) Unchecked exception
b) Checked exception
c) Both checked & unchecked exception
d) None of these
View Answer
Answer:- a) Unchecked exceptionThe “throws” keyword is required only for checked exceptions and usage of throws keyword for unchecked exceptions has no impact.
Q8) The “throws” keyword can’t be used with?
a) Methods
b) Constructors
c) Classes
d) All of these
View Answer
Answer:- c) ClassesWe can use throws keywords for method & constructors but not for classes. Example:- public class Test throws Exception { } // invalid
Q9) Find the output of the below program?
public class Test {
public static void main(String[] args) throws Test {}
}
a) Compile-time error
b) Exception
c) No output
d) None of these
View Answer
Answer:- a) Compile-time errorWe can use throws keywords only for throwable types. If we are trying to use it for normal Java classes then we will get compile time errors:- incompatible types.
Q10) Find the output of the below program?
public class Test extends RuntimeException {
public static void main(String[] args) throws Test {}
}
a) Compile-time error
b) Exception
c) No output
d) None of these
View Answer
Answer:- c) No outputWe can use throws keywords for Throwable types. In this program, Test class extends from RuntimeException hence it is an indirect subclass of Throwable.
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!