Java MCQ Output based Questions - try-catch

In this article, we will cover some Java MCQ output based questions which will help extend your Java knowledge. Usually In Interviews nowadays, interviewer are more focused on negative questions rather than conceptual questions, and If you go through below questions, It will surely give you an add on and add value to your experience.

Java MCQ Output based Questions

try-catch block in Java

What is the output of below program?
class Geeks
{
public static void main(String[] args)
{
try
{
System.out.println(1/0);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
}
}

1. java.lang.ArithmeticExcetion 

2. / by zero 

3. java.lang.ArithmeticExcetion:/ by zero 

4. ArithmeticExcetion

What is the output of below program?

class Geeks

{

public static void main(String[] args)

{

try

{

System.out.println(1/0);

}

catch(ArithmeticException e)

{

System.out.println("Hello Geeks");

}

catch(Exception e)

{

System.out.println("Welcome");

}

}

}

1. Hello Geeks 

2. No Output 

3. Compile-time error 

4. welcome

 Explanation - In the above program, we are following the approach of try with multiple catch blocks. Here 1/0 is an ArithmeticException, which is caught by the first catch block and it is executed.

What is the output of below program?

class Geeks

{

public static void main(String[] args)

{

try

{

System.out.println(1/0);

}

catch(Exception e)

{

System.out.println("Hello Geeks");

}

catch(ArithmeticException e)

{

System.out.println("Welcome");

}

}

}

1. Hello Geeks 

2. No Output 

3. Compile-time error 

4. welcome


If we are trying try with multiple catch block then we should take care that the child class catch block is first then parent class catch block. Otherwise, we will get compile time error saying error: exception ArithmeticException has already been caught.


class Geeks

{

public static void main(String[] args)

{

try

{

System.out.println(1/0);

}

}

}


1. Run-time Exception 
2. Compile-time error 
3. No Output 
4. Compile-time Exception


Explanation - In the above program, we are declaring a try block without any catch or finally block. We have to always declare try with catch or finally block because single try block is invalid. That’s Why it will give compile time error saying error: ‘try’ without ‘catch’, ‘finally’ or resource declarations.


class Geeks

{

public static void main(String[] args)

{

try

{

System.out.println(1/0);

}

System.out.println("Hello GEEKS");

catch(ArithmeticException e)

{

System.out.println("Welcome");

}

}

}


1. Hello Geeks 

2. Hello Geeks 

Welcome 

3. Run-time Exception 

4. Compile-time error


Explanation - In the above program, we are declaring a try block and also a catch block but both are separated by a single line which will cause compile time error:


import java.io.IOException;

import java.util.EmptyStackException;


public class newclass

{

public static void main(String[] args)

{

try

{

System.out.printf("%d", 1);

throw(new Exception());

}

catch(IOException e)

{

System.out.printf("%d", 2);

}

catch(EmptyStackException e)

{

System.out.printf("%d", 3);

}

catch(Exception e)

{

System.out.printf("%d", 4);

}

finally

{

System.out.printf("%d", 5);

}

}

}


a) 12345

b) 15

c) 135

d) 145


Explanation: The catch statements are written in the order: more specific to more general. In the code above, a new exception of type Exception is thrown. First the code jumps to first catch block to look for exception handler. But since the IOException is not

of the same type it is moves down to second catch block and finally to the third, where

the exception is caught and 4 is printed. Therefore, the answer is 145, as the order

of execution in terms of blocks is: try->catch->finally.


Which of the following is FALSE about finally block?

a) For each try block, there can be only 1 finally block.

b) finally block will not be executed if program exits by calling System.exit();

c) If an exception is not handled in the catch statement, before terminating the program, JVM executes the finally block

d) finally block contains important code segment and so the code inside finally block is executed with/without the presence of try block in java code. 

Explanation:Statement (d) is false because finally blocks can exist only if it succeeds try or a catch block. Using a finally block without try block would give a compile time error.


Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD