Java Quiz Day 30

Here are essential multiple-choice questions.

Last Updated :
Discuss
Comments

Question 1

What is the difference between throw and throws?

  • throw is used to declare exceptions, throws is used to throw exceptions

  • throws is used to declare exceptions, throw is used to throw exceptions

  • Both are used to declare exceptions

  • Both are used to throw exceptions

Question 2

What happens when finalize() is called on an object?

  • The object is immediately garbage collected.

  • The garbage collector calls it before collecting the object.

  • The object gets permanently deleted from memory.

  • It prevents an object from being collected.

Question 3

What will happen if an exception occurs in a catch block?

  • The program terminates abruptly

  • The finally block executes, then the program terminates

  • The exception is ignored

  • Another catch block handles it automatically

Question 4

What will happen in this code?

Java
public class Main {
    public static void main(String[] args) {
        try {
            throw new NullPointerException("Demo");
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception");
        } finally {
            System.out.println("Finally block executed");
        }
    }
}


  • Arithmetic Exception Finally block executed

  • NullPointerException Finally block executed

  • Finally block executed

  • Compilation Error

Question 5

What is the output of the following code?

Java
public class Main {
    public static void main(String[] args) {
        try {
            System.out.println("Inside try");
            throw new RuntimeException("Error");
        } finally {
            System.out.println("Inside finally");
        }
    }
}


  • Inside try

  • Inside try Inside finally

  • Inside try Inside finally RuntimeException

  • Compilation Error

Question 6

What will happen in the following code?

Java
public class Main {
    static void method() throws Exception {
        throw new Exception("Error occurred");
    }

    public static void main(String[] args) {
        method();
    }
}


  • Compilation Error


  • Runtime Exception

  • Exception: Error occurred

  • Program runs successfully

Question 7

What will be the output of the following program?

Java
class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new CustomException("Custom error occurred");
        } catch (CustomException e) {
            System.out.println(e.getMessage());
        }
    }
}


  • Custom error occurred

  • Runtime Exception

  • Compilation Error

  • No Output


Tags:

There are 7 questions to complete.

Take a part in the ongoing discussion