0% found this document useful (0 votes)
14 views4 pages

Java Exception Puzzles and Solutions

The document contains a series of Java exception puzzles designed to test understanding of exception handling in Java. Each puzzle presents a code snippet with multiple-choice answers regarding the output of the program. The document includes various scenarios involving try-catch blocks and the handling of different types of exceptions and errors.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Java Exception Puzzles and Solutions

The document contains a series of Java exception puzzles designed to test understanding of exception handling in Java. Each puzzle presents a code snippet with multiple-choice answers regarding the output of the program. The document includes various scenarios involving try-catch blocks and the handling of different types of exceptions and errors.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

12/14/24, 3:35 PM Green Java

Green Java

Home Java Puzzles Jsp Servlet Hibernate

Java Exception Puzzles


Java Puzzles Categories
Java Basic

Java Blocks
public class MyClass{
Java Strings
public static void main(String[] args) {
try Java Arrays

{ Java Inheritance
int x = 0; Java Abstract Classes
int y = 5 / x; Java Interface
}
Java Exception
catch (Exception e)
{ Java Thread

[Link]("Exception"); Java Collections


} Garbage Collections
catch (ArithmeticException ae)
{
[Link](" Arithmetic Exception");
}
[Link]("finished");
}
}
[Link]
[Link]
[Link] fails. Answer
[Link] Exception

public class MyClass{


public static void main(String[] args) {
try
{
int y = 5 / 0;
[Link]("Try");
}
catch (ArithmeticException ae)
{
[Link]("Arithmetic Exception");
}
catch (Exception e)
{
[Link](" Exception");
}
[Link](" finished");
}
}
[Link] 1/4
12/14/24, 3:35 PM Green Java

[Link] Arithmetic Exception finished


[Link] finished
[Link] Exception finished Answer
[Link] Exception finished

public class MyClass


{
public static void main(String [] args)
{
try
{
badMethod();
[Link]("A");
}
catch (Exception ex)
{
[Link]("B");
}
finally
{
[Link]("C");
}
[Link]("D");
}
public static void badMethod()
{
throw new Error();
}
}
[Link]
[Link] fails. Answer

C.'C' is printed before exiting with an error message.


D.'BC' is printed before exiting with an error message.

public class MyClass


{
public static void main(String [] args)
{
try
{
badMethod();
[Link]("A");
}
catch (Exception ex)
{
[Link]("B");
}
catch (Error ex)
{
[Link]("E");
}
finally

[Link] 2/4
12/14/24, 3:35 PM Green Java

{
[Link]("C");
}
[Link]("D");
}
public static void badMethod()
{
throw new Error();
}
[Link]
}
[Link] fails.
[Link] Answer
[Link]

public class MyClass


{
public static void main(String [] args)
{
try
{
badMethod();
[Link]("A");
}
catch (Throwable ex)
{
[Link]("B");
}
catch (Error ex)
{
[Link]("E");
}
finally
{
[Link]("C");
}
[Link]("D");
}
public static void badMethod()
{
throw new Error();
}
}
[Link]
[Link] fails.
[Link] Answer
[Link]

public class MyClass


{
public static void main(String [] args)
{
try
[Link] 3/4
12/14/24, 3:35 PM Green Java

{
badMethod();
[Link]("A");
}
catch (Error ex)
{
[Link]("E");
}
catch (Throwable ex)
{
[Link]("B");
}
finally
{
[Link]("C");
}
[Link]("D");
}
public static void badMethod()
{
throw new Error();
}
}
[Link]
[Link] fails.
[Link] Answer
[Link]

[Link] 4/4

You might also like