adplus-dvertising

Welcome to the Exception Handling in Java MCQs Page

Dive deep into the fascinating world of Exception Handling in Java with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Exception Handling in Java, a crucial aspect of Java Programming. In this section, you will encounter a diverse range of MCQs that cover various aspects of Exception Handling in Java, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Java Programming.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Exception Handling in Java. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Java Programming.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Exception Handling in Java. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Exception Handling in Java MCQs | Page 7 of 9

Explore more Topics under Java Programming

Q61.
What will be the output of the program?

public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A"); 
        }  
        catch (Exception ex) 
        {
            System.out.print("B");  
        } 
        finally 
        {
            System.out.print("C"); 
        } 
        System.out.print("D"); 
    }  
    public static void badMethod() 
    {
        throw new Error(); /* Line 22 */
    } 
}
Discuss
Answer: (c).C is printed before exiting with an error message.
Q62.
What will be the output of the program?

public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A");  
        } 
        catch (RuntimeException ex) /* Line 10 */
        { 
            System.out.print("B"); 
        } 
        catch (Exception ex1) 
        { 
            System.out.print("C"); 
        } 
        finally 
        {
            System.out.print("D"); 
        } 
        System.out.print("E"); 
    } 
    public static void badMethod() 
    { 
        throw new RuntimeException(); 
    } 
}
Discuss
Answer: (c).BDE
Q63.
What will be the output of the program?

public class RTExcept 
{
    public static void throwit () 
    {
        System.out.print("throwit ");
        throw new RuntimeException();
    }
    public static void main(String [] args) 
    {
        try 
        {
            System.out.print("hello ");
            throwit();
        }
        catch (Exception re ) 
        {
            System.out.print("caught ");
        }
        finally 
        {
            System.out.print("finally ");
        }
        System.out.println("after ");
    }
}
Discuss
Answer: (d).hello throwit caught finally after
Q64.
Given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
import java.io.*;
public class MyProgram 
{
    public static void main(String args[])
    {
        FileOutputStream out = null;
        try 
        {
            out = new FileOutputStream("test.txt");
            out.write(122);
        }
        catch(IOException io) 
        {
            System.out.println("IO Error.");
        }
        finally 
        {
            out.close();
        }
    }
}
Discuss
Answer: (d).This program fails to compile due to an error at line 18.
Q65.
Which answer most closely indicates the behavior of the program?
public class MyProgram 
{
    public static void throwit() 
    {
        throw new RuntimeException();
    }
    public static void main(String args[])
    {
        try 
        {
            System.out.println("Hello world ");
            throwit();
            System.out.println("Done with try block ");
        }
        finally 
        {
            System.out.println("Finally executing ");
        }
    }
}
Discuss
Answer: (d).The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.
Q66.
At Point X on line 5, which code is necessary to make the code compile?
public class ExceptionTest 
{ 
    class TestException extends Exception {} 
    public void runTest() throws TestException {} 
    public void test() /* Point X */ 
    { 
        runTest(); 
    } 
}
Discuss
Answer: (b).throws Exception
Q67.
Given that EOFException and FileNotFoundException are both subclasses of IOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code?
System.out.print("Start ");
try 
{
    System.out.print("Hello world");
    throw new FileNotFoundException();
}
System.out.print(" Catch Here "); /* Line 7 */
catch(EOFException e) 
{
    System.out.print("End of file exception");
}
catch(FileNotFoundException e) 
{
    System.out.print("File not found");
}
Discuss
Answer: (a).The code will not compile.
Discuss
Answer: (a).catch(X x) can catch subclasses of X where X is a subclass of Exception.
Q69.
Which four can be thrown using the throw statement?

1. Error
2. Event
3. Object
4. Throwable
5. Exception
6. RuntimeException
Discuss
Answer: (c).1, 4, 5 and 6
Discuss
Answer: (d).Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
Page 7 of 9

Suggested Topics

Are you eager to expand your knowledge beyond Java Programming? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!