Java Programming MCQ - Exception

11. What is the output of this program?
class exception_handling
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
System.out.print("World");
}
}
}

A. Hello
B. World
C. HelloWorld
D. Hello World

View Answer


12. What is the output of this program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}

A. A
B. B
C. Compilation Error
D. Runtime Error

View Answer


13. What is the output of this program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
}
}

A. A
B. B
C. AC
D. BC

View Answer


14. What is the output of this program?
class exception_handling
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e)
{
System.out.print("0");
}
System.out.print(sum);
}
}

A. 0
B. 05
C. Compilation Error
D. Runtime Error

View Answer


15.Predict the output of following Java program
class Main {
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}

A. Got the Exception 10
B. Got the Exception 0
C. Compiler Error
D. None of the above

View Answer


16. class Test extends Exception { }

class Main {
public static void main(String args[]) {
try {v throw new Test();
}v catch(Test t) {
System.out.println("Got the Test Exception");
}
finally {
System.out.println("Inside finally block ");
}
}
}

A. Got the Test Exception
Inside finally block
B. Got the Test Exception
C. Inside finally block
D. Compiler Error

View Answer


17. Output of following Java program?
class Main {
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}


A. Compiler Error
B. Compiles and runs fine
C. Compiles fine but throws ArithmeticException exception
D. None of the above

View Answer


18. class Base extends Exception {}
class Derived extends Base {}
public class Main {
public static void main(String args[]) {
// some other stuff
try {
// Some monitored code
throw new Derived();
}
catch(Base b) {
System.out.println("Caught base class exception");
}
catch(Derived d) {
System.out.println("Caught derived class exception");
}
}
}

A. Caught base class exception
B. Caught derived class exception
C. Compiler Error because derived is not throwable
D. Compiler Error because base class exception is caught before derived class

View Answer


19. class Test
{
public static void main (String[] args)
{
try
{
int a = 0;
System.out.println ("a = " + a);
int b = 20 / a;
System.out.println ("b = " + b);
}
catch(ArithmeticException e)
{
System.out.println ("Divide by zero error");
}
finally
{
System.out.println ("inside the finally block");
}
}
}

A. Compile error
B. Divide by zero error
C. a = 0
Divide by zero error
inside the finally block
D. a = 0
E. inside the finally block

View Answer


20. What is the output of this program?
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}

A. Finally
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.

View Answer







Also check :


Discussion


* You must be logged in to add comment.