Rathore Mahavir. - Java 8 Exception Handling Quiz PDF
Rathore Mahavir. - Java 8 Exception Handling Quiz PDF
Book Series
Java ™ 8
Exception
Handling
Quiz
Mahavir DS Rathore
Copyright
Java 8 Exception handling quiz by Mahavir DS Rathore. While every precaution has been
taken in the preparation of this book the author assume No responsibility for errors or
omissions, or for damages resulting from the use of the information contained herein.
About the author
I have has been programming & teaching Java for last 18 years. This book is an effort to
document my knowledge to share with everyone across the world. I am available to do
training on Java anywhere in the world. My email id is gurumahaveer@gmail.com
Who should read the book?
This book is for programmers who already know Core Java (Java Standard Edition).
Acknowledgement
Java is owned by Oracle and trademark is acknowledged.
Dedication
To the inventors of the Java Language.
Table of Content
Sl.No Chapter HyperLink
1 Java Setup Go
2 What is Go
Exception?
3 What is Exception Go
Handling?
4 Java Exception Go
Hierarchy
6 Exception Go
Handling API
7 Multiple Catch Go
8 Multiple Go
Exceptions
9 Finally Block Go
11 Checked and Go
Unchecked
Exception
12 Nested Exception Go
13 Throw Keyword Go
14 Exception Go
Chaining
16 Overriding Go
Exception
17 User Defined Go
Exception
Chapter 1
Software Setup
A 7
B 6
C 9
D 8
<–-Right Answer
Q.2: JRE stands for …………………
<–-Right Answer
<–-Right Answer
A JDK
B JRE
C JME
D JEE
<–-Right Answer
A CLASSPATH
B JAVAHOME
C PATH
D HOME
<–-Right Answer
A JME
B JDK
C JEE
D JRE
<–-Right Answer
A Javaip.exe
B Javac.exe
C Java.exe
D Javai.exe
<–-Right Answer
A Java.exe
B Javacompiler.exe
C Javacomp.exe
D Javac.exe
<–-Right Answer
A C:\Program Files\
B C:\Windows\
C C:\Java
<–-Right Answer
A C:\Program Files\
B C:\Windows\
C C:\Java
<–-Right Answer
Answers:
10 A 64-bit application on
Windows OS is installed in
“C:\Program Files\” folder
by default.
Chapter 2
What is Exception?
A Error
B Exception
C Trigger
<–-Right Answer
Q.2: Runtime error is also called as
…………..
A Error
B Compiler error
C Exception
<–-Right Answer
A Runtime error
B Exception
C Compiler error
<–-Right Answer
A Compiler error
B JVM error
C Nothing
D Exception
<–-Right Answer
A Exception
B Runtime error
C Compiler error
D JVM error
<–-Right Answer
class Program {
public static void main(String args[]) {
String ar = new String[1];
ar[2] =“Ram”;
System.out.println(ar[0]);
}
}
A Compiler error
B Exception
C No error
D JVM
<–-Right Answer
A ‘throw’ is called
<–-Right Answer
Answers:
5 C Missing semi-colon is
captured as a Compiler error.
6 B Exception
“ArrayOutOfBounds” occur.
Chapter 3
What is Exception Handling?
A Error handling
B Exception handling
C Compiler handling
D Database handling
<–-Right Answer
A Throw
B Throws
C throwing
D Try
<–-Right Answer
A Try
B Catch
C except
D Finally
<–-Right Answer
A throws
B Throw
C except
D Raise
<–-Right Answer
A JVM
B OS
C catch
D finally
<–-Right Answer
Answers:
Q.No Ans
Chapter 4
Java Exception Hierarchy
A Throwable
B Exception
C Error
D Object
<–-Right Answer
A Object
B Error
C Compiler
D Throwable
<–-Right Answer
A Object
B Exception
C Throwable
D RuntimeException
<–-Right Answer
A Error
B Exception
C Throwable
D Object
<–-Right Answer
A Cloneable
B Serializable
C Runnable
D Comparable
<–-Right Answer
A Object
B Exception
C Error
D JVM
<–-Right Answer
A Error class
B Throwable class
C Exception class
D Object class
<–-Right Answer
A Throwable class
B Error class
C Exception class
D Object class
<–-Right Answer
Q.9: Which of the following is/are
derived from “Error class”?
A StackoverflowError
B OutOfMemoryError
C VirtualMachineError
<–-Right Answer
A Thread.abort()
B Thread.stop()
C Thread.exit()
D Thread.quit()
<–-Right Answer
A OutOfMemoryError
B OutOfMemoryException
C StackoverflowException
D StackoverflowError
<–-Right Answer
A Error class
B RuntimeException class
C Exception class
D Object class
C <–-Right Answer
A Exception class
B Throwable class
C Object class
D RuntimeException
<–-Right Answer
A NullPointerException
B ArrayIndexOutOfBoundException
C ArithmeticException
D IOException
<–-Right Answer
A Serializable
B Throwable
C Attribute
<–-Right Answer
A Serializable
B Throwable
C Atrribute
<–-Right Answer
A Throwable class
B Object class
C Error class
D RuntimeException class
<–-Right Answer
Answers:
Q.No Ans Explanation
5 B Serializable interface is
implemented by Throwable
class.
10 B ThreadDeath Exception
occurs when Thread.stop() is
called.
11 D StackoverflowError occur
when a method is deeply
recursive.
13 D RuntimeException class is
not identified by the
compiler.
Chapter 5
Try and Catch Blocks
B Assertion information
C Debug information
<–-Right Answer
A Interface
B Class
C Method
D Nested class
<–-Right Answer
A Compiler
B GC
C JVM
<–-Right Answer
A Code block
B Try block
C Data block
D Nested block
<–-Right Answer
A Class
B Interface
C Constructor
D Method
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
Answers:
Q.No Ans Explanation
3 C An unhandled exception is
handled by JVM (which
crashes an application).
Chapter 6
Exception Handling API
D Collection of functions.
<–-Right Answer
A callStack()
B printStackTrace()
C stackTrace()
D stackInfo()
<–-Right Answer
A message()
B toString()
C getInfo()
D msgException()
<–-Right Answer
A Throwable
B Error
C Exception
D Object
<–-Right Answer
Q.5: toString() method belong to
…………… class.
A Throwable
B Error
C Exception
D Object
<–-Right Answer
A String
B Char
C Void
D char[]
<–-Right Answer
Answers:
Q.No Ans
4 A printStackTrace() method
belong to Throwable class.
6 C printStackTrace() method
does not return anything.
Chapter 7
Multiple Catch
A 1
B 2
C 15
D Any Number
<–-Right Answer
A In any order
<–-Right Answer
A Any order
B Throwable, Exception,
RuntimeException
C Exception,RuntimeException,Throwable
D RuntimeException,Exception,Throwable
<–-Right Answer
Q.4: ‘try’ block cannot encapsulate?
D Class
<–-Right Answer
A Compiler Error
B Runtime Exception
C Nothing happens
D JVM Exception
<–-Right Answer
Answers:
Chapter 8
Multiple Exceptions
A 6
B 5
C 7
D 8
<–-Right Answer
A | (pipe)
B & (ampersand)
C , (comma)
D /(slash)
<–-Right Answer
B catch(Throwable | Exception) {}
D Catch (…) {}
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
A No error or exception
B Compiler Error
C JVM Error
D Runtime Exception
<–-Right Answer
Answers:
Q.No Ans Explanation
3 A Correct syntax is :
catch(Error| Exception obj)
{}
Chapter 9
Finally Block
A 1
B 2
C 3
D Any number
<–-Right Answer
B catch,finally,try
C try,catch,finally
D Any order
<–-Right Answer
B Program exits
C ‘finally’ is guaranteed to be
executed
<–-Right Answer
A ‘try’ block
B ‘catch’ block
C ‘finally’ block
D Constructor
<–-Right Answer
A ‘catch’ block
B ‘finally’ block
C ‘try’ block
D Code block
<–-Right Answer
A ‘catch’ block
B ‘finally’ block
C Any order
<–-Right Answer
A Program crashes
D Compiler error
<–-Right Answer
Answers:
Q.No Ans Explanation
Chapter 10
Finally block & Return keyword
A System.stop()
B System.abort()
C System.kill()
D System.exit()
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
Answers:
1 D System.exit() is used to
abruptly stop a program.
Chapter 11
Checked and Unchecked Exception
A Checked
B Unchecked
C Runtime
<–-Right Answer
A Unchecked
B Checked
C Compiler
D JVM
<–-Right Answer
A Compiler
B Checked
C Unchecked
D JVM
<–-Right Answer
A Compiler
B Checked
C JVM
D Unchecked
<–-Right Answer
A Throw
B Throws
C Decl
D Finally
<–-Right Answer
A Throwable
B Exception
C Error
D RuntimeException
<–-Right Answer
A 1
B 2
C 3
D Any number
<–-Right Answer
A TRUE
B FALSE
<–-Right Answer
Q.9: Unchecked exception derive from
…………. class
A Throwable
B Exception
C Error
D RuntimeException
<–-Right Answer
A NullPointerException
B ArithmeticException
C ClassCastException
D Exception
<–-Right Answer
A NullPointerException
B ArithmeticException
C ClassCastException
D Exception
<–-Right Answer
Answers:
1 A Checked Exception is
identified by the compiler.
8 A Checked exception is
known at compile time.
9 D Unchecked exceptions
derive from
RuntimeException class
10 D Exception class is a
checked exception.
11 A,B,C NullPointerException,
ArithmeticException and
ClassCastException are not
checked exceptions.
Chapter 12
Nested Exception
A TRUE
B FALSE
<–-Right Answer
A 1
B 2
C 3
D Any Level
<–-Right Answer
class Program {
static void performJob() {
try {
int j = 10/0;
}catch(ArrayIndexOutOfBoundsException
e) {
System.out.println(“Handle the array
carefully”);
}
}
public static void main(String args[]) {
try {
performJob();
} catch (Exception e) {
System.out.println(“There is some
problem in the code”);
}
}
}
A No output
D Division by zero
<–-Right Answer
class Program {
static void performJob() {
try {
int j = 10/0;
}catch(ArrayIndexOutOfBoundsException
e) {
System.out.println(“Handle the array
carefully”);
}
finally {
System.out.println(“finally -
performJob”);
}
}
public static void main(String args[]) {
try {
performJob();
} catch (Exception e) {
System.out.println(“There is some
problem in the code”);
}
finally {
System.out.println(“finally - main”);
}
}
}
A No output
B finally - performJob
There is some problem in the code
finally – main
C finally - performJob
finally – main
<–-Right Answer
class Program {
static void performJob() {
try {
int j = 10/0;
}catch(ArrayIndexOutOfBoundsException
e) {
System.out.println(“Handle the array
carefully”);
}
finally {
System.out.println(“finally -
performJob”);
}
}
public static void main(String args[]) {
int ar[] = new int[1];
try {
ar[1]=0;
performJob();
} catch (Exception e) {
System.out.println(“There is some
problem in the code”);
}
finally {
System.out.println(“finally - main”);
}
}
}
A No output
C finally - performJob
There is some problem in the code
finally – main
D finally - performJob
There is some problem in the code
<–-Right Answer
Answers:
Q.No Ans Explanation
Chapter 13
‘Throw’ Keyword
A Object
B Throwable
C GC
<–-Right Answer
A Throw
B Throws
C Raise
D Signal
<–-Right Answer
class Program {
public static void main(String args[]) {
try {
throw new Exception();
} catch (Exception e) {
System.out.println(“There is some
problem in the code”);
}
finally {
System.out.println(“finally - main”);
}
}
}
A No output
D finally – main
<–-Right Answer
A finally - performJob
finally – main
B No output
<–-Right Answer
class Program {
static void performJob() {
try {
throw new
ArrayIndexOutOfBoundsException();
}catch(ArrayIndexOutOfBoundsException
e) {
throw new Exception();
System.out.println(“Handle the array
carefully”);
}
finally {
System.out.println(“finally -
performJob”);
}
}
public static void main(String args[]) {
try {
performJob();
} catch (Exception e) {
System.out.println(“There is some
problem in the code”);
}
finally {
System.out.println(“finally - main”);
}
}
}
A No output
B Compiler Error
D finally - performJob
finally - main
<–-Right Answer
class Program {
static void performJob() {
try {
throw new
ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException
e) {
throw new Exception();
System.out.println(“Handle the array
carefully”);
}
finally {
System.out.println(“finally -
performJob”);
}
}
public static void main(String args[]) {
try {
performJob();
} catch (Exception e) {
System.out.println(“There is some
problem in the code”);
}
finally {
System.out.println(“finally - main”);
}
}
}class Program {
static void performJob() {
try {
throw new
ArrayIndexOutOfBoundsException();
}catch(ArrayIndexOutOfBoundsException
e) {
try {
throw new Exception();
}catch(Exception obj) {
System.out.println(“This is
inner exception”);
}
System.out.println(“Handle the array
carefully”);
}
finally {
System.out.println(“finally -
performJob”);
}
}
public static void main(String args[]) {
try {
performJob();
} catch (Exception e) {
System.out.println(“There is some
problem in the code”);
}
finally {
System.out.println(“finally - main”);
}
}
}
A No output
B Compiler Error
<–-Right Answer
Answers:
5 B Compiler Error
Chapter 14
Exception Chaining
A Exception
B Throwable
C Error
D RuntimeException
<–-Right Answer
A Object
B Error
C RuntimeException
D Throwable
<–-Right Answer
A Throwable(String, Throwable)
B Throwable(String[], Throwable)
C Throwable(char[], Throwable)
D Throwable(char, Throwable)
<–-Right Answer
A start Cause()
B setCause()
C setIssue()
D initCause()
<–-Right Answer
A Throwable(int)
B Throwable(String[])
C Throwable(Throwable)
D Throwable()
<–-Right Answer
A Object
B Throwable
C RuntimeException
D Error
<–-Right Answer
A getMsg()
B getMessage()
C getCause()
D getReason()
<–-Right Answer
Answers:
Q.No Ans Explanation
2 D getCause() method is
declared in Throwable class.
3 A Valid constructor is :
Throwable(String,
Throwable)
4 D initCause() method
initializes the cause of an
exception.
6 B initCause() method is
declared in Throwable class.
Chapter 15
Try with Resource
A 5
B 6
C 7
D 8
<–-Right Answer
A None
B 2
C 3
D Any
<–-Right Answer
A AutoCloseable
B Closeable
C TryClose
D Closed
<–-Right Answer
A Close()
B close()
C Closing
D Closed
<–-Right Answer
Q.5: Identify the method of Closeable
interface?
A closed()
B closing()
C close()
D end()
<–-Right Answer
A Custom
B Forward
C Reverse
D Conditional
<–-Right Answer
import java.io.*;
class Shape implements Closeable {
public void close() {
System.out.println(“Shape
object close is closed”);
}
}
class Program {
public static void main(String args[]){
try(Shape obj = new Shape())
{
}catch(Exception e) {
System.out.println(“Exception
happened”);
}
}
}
A No Output
B Compiler Error
D Exception happened
<–-Right Answer
Answers:
Chapter 16
Overriding Exception
class Shape {
void draw () {
System.out.println(“Drawing
shape”);
}
}
class Circle extends Shape{
void draw() throws
RuntimeException {
System.out.println(“Drawing
Circle”);
}
}
class Program {
public static void main(String args[]) {
Circle obj = new Circle();
obj.draw();
}
}
A Compiler error
B No output
C Drawing shape
D Drawing Circle
<–-Right Answer
class Shape {
void draw () {
System.out.println(“Drawing
shape”);
}
}
class Circle extends Shape{
void draw() throws Exception {
System.out.println(“Drawing
Circle”);
}
}
class Program {
public static void main(String args[])
{
Circle obj = new Circle();
obj.draw();
}
}
A Compiler error
B No output
C Drawing shape
D Drawing Circle
<–-Right Answer
class Shape {
void draw () throws Exception {
System.out.println(“Drawing
shape”);
}
}
class Circle extends Shape{
void draw() throws Throwable {
System.out.println(“Drawing
Circle”);
}
}
class Program {
public static void main(String args[])
throws Exception{
Circle obj = new Circle();
obj.draw();
}
}
A Compiler error
B No output
C Drawing shape
D Drawing Circle
<–-Right Answer
class Shape {
void draw () throws Exception {
System.out.println (“Drawing
shape”);
}
}
class Circle extends Shape{
void draw() throws Exception {
System.out.println(“Drawing
Circle”);
}
}
class Program {
public static void main(String
args[]) throws Exception{
Circle obj = new Circle();
obj.draw();
}
}
A Compiler error
B No output
C Drawing shape
D Drawing Circle
<–-Right Answer
class Shape {
void draw () throws Exception {
System.out.println(“Drawing
shape”);
}
}
class Circle extends Shape{
void draw() throws
RuntimeException {
System.out.println(“Drawing
Circle”);
}
}
class Program {
public static void main(String
args[]) throws Exception{
Circle obj = new Circle();
obj.draw();
}
}
A Compiler error
B No output
C Drawing shape
D Drawing Circle
<–-Right Answer
Rules for exceptions in overridden
methods:
1. If the super class method ‘throws’ an
exception then child class can ‘throws’
the same exception or child class of
that exception.
2. If super class method does not ‘throws’
any exception then the child class
overridden method can ‘throws’
unchecked exception.
3. If checked exception is declared for a
method then it should be raised (throw).
Answers:
2 A Compiler Error
3 A Compiler Error
Chapter 17
User Defined Exception
<–-Right Answer
<–-Right Answer
A Compiler Error
B Runtime Exception
C J is greater
None of the above
<–-Right Answer
A Compiler Error
B Runtime Exception
C J is greater
<–-Right Answer
A RuntimeException
B Compiler Error
C JVM Error
<–-Right Answer
A Object Exception
B JVM Error
C Runtime Exception
D Compiler Error
<–-Right Answer
Answers: