Curriculum
Course: Learn Java Programming
time left:
:
:
Login

Curriculum

Learn Java Programming

Quiz

Quiz: Exception in Java

Submit quiz
Once you submit, you will no longer be able to change your answers. Are you sure you want to submit the quiz?
1.

What is the purpose of assertions?

To handle exceptions.
To test program logic during development.
To improve program performance.
To replace error messages.
2.

Consider the following code:

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            throw new Exception("Test");
        } catch (Exception e) {
            try {
                throw new RuntimeException("Inner");
            } catch (RuntimeException re) {
                System.out.print(re.getMessage() + " ");
            }
        } finally {
            System.out.print("Finally ");
        }
        System.out.print("End");
    }
}

What is the output?

Test Finally End
Inner Finally End
Inner End Finally
Test Inner Finally End
3.

Consider the following code.

public class ExceptionTest {
    public static void main(String[] args) {
        try{
            throw new MyException();
        }catch(Exception e){
            System.out.print(e.getMessage());
        }
    }
    static class MyException extends Exception{
        MyException(){super("Custom Exception");}
    }
}

What is the output?

MyException
Custom Exception
Exception
RuntimeException
4.

Consider the following code:

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            try {
                throw new Exception("Inner");
            } finally {
                System.out.print("F");
            }
        } catch (Exception e) {
            System.out.print("C");
        }
        System.out.print("E");
    }
}

What is the output?

CFE
CE
FC
FCE
5.

Consider the following code:

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            assert false;
            System.out.print("A");
        } catch (AssertionError e) {
            System.out.print("B");
        } finally {
            System.out.print("C");
        }
        System.out.print("D");
    }
}

If assertions are enabled using -ea, what is the output?

 

ABCD
BCD
ACD
AD
6.

What is the purpose of multiple catch blocks?

To handle multiple types of exceptions.
To handle a single exception multiple times.
To avoid using the finally block.
To improve code readability.
7.

By default, are assertions enabled or disabled in Java?

Enabled
Disabled
Depends on the compiler.
Depends on the operating system.
8.

How do you create a custom exception in Java?

By extending the Exception class.
By implementing the Exception interface.
By creating a new class with the exception keyword.
By using the customException keyword.
9.

Consider the following code:

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            System.out.print("1");
            throw new Exception();
        } catch (Exception e) {
            System.out.print("2");
            return;
        } finally {
            System.out.print("3");
        }
        System.out.print("4");
    }
}

What is the output?

1234
123
124
134
10.

Consider the following code:

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            method1();
        } catch (Exception e) {
            System.out.print("C");
        }
    }

    static void method1() throws Exception {
        try {
            throw new RuntimeException("A");
        } catch (RuntimeException e) {
            System.out.print("B");
            throw new Exception("D");
        } finally {
            System.out.print("E");
        }
    }
}

What is the output?

BE
BEC
BC
BCE
11.

Consider the following code:

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            System.out.print("A");
            throw new RuntimeException();
        } finally {
            System.out.print("B");
        }
    }
}


What is the output?

AB
AB followed by program termination.
A followed by program termination.
A
12.

Consider the following code:

public class ExceptionTest{
    public static void main(String[] args){
        try{
            System.out.print("1");
            int a = 1/0;
        }catch(RuntimeException e){
            System.out.print("2");
        }catch(Exception e){
            System.out.print("3");
        }finally{
            System.out.print("4");
        }
    }
}

What is the output?
134
124
Compilation Error
1234
13.

Which keyword is used to declare that a method might throw an exception?

throw
throws
try
catch
14.

Which keyword is used to define an assertion in Java?

verify
check
assert
test
15.

Consider the following code:

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            System.out.print("A");
            int x = 10 / 0;
            System.out.print("B");
        } catch (ArithmeticException e) {
            System.out.print("C");
        } finally {
            System.out.print("D");
        }
        System.out.print("E");
    }
}

What is the output?

ABCDE
ACDE
ADE
AE
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15