0
0
Javaprogramming~20 mins

Try–catch block in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Try-Catch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of try-catch with arithmetic exception
What is the output of this Java code snippet?
Java
public class Main {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        }
    }
}
ACompilation error
BResult: 0
CCannot divide by zero
DRuntime error: NullPointerException
Attempts:
2 left
💡 Hint
Think about what happens when dividing by zero in Java.
Predict Output
intermediate
2:00remaining
Output when no exception occurs in try block
What will be printed when this Java code runs?
Java
public class Main {
    public static void main(String[] args) {
        try {
            int result = 10 / 2;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        }
    }
}
ANo output
BResult: 5
CCannot divide by zero
DCompilation error
Attempts:
2 left
💡 Hint
No exception happens here, so catch block is skipped.
🔧 Debug
advanced
2:00remaining
Identify the error in try-catch syntax
Which option shows the code that will cause a compilation error due to incorrect try-catch syntax?
A
try {
    int x = 5;
} catch (Exception e) {
    System.out.println(e);
}
B
}
;)e(nltnirp.tuo.metsyS    
{ )e noitpecxE( hctac }
;5 = x tni    
{ yrt
C
try {
    int x = 5;
} catch (Exception e) {
    System.out.println(e);
} finally {
    System.out.println("Done");
}
D
try {
    int x = 5;
} catch Exception e {
    System.out.println(e);
}
Attempts:
2 left
💡 Hint
Check the syntax of the catch block parentheses.
Predict Output
advanced
2:00remaining
Output with multiple catch blocks
What will this Java program print?
Java
public class Main {
    public static void main(String[] args) {
        try {
            int[] arr = new int[2];
            System.out.println(arr[5]);
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception caught");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index out of bounds caught");
        } finally {
            System.out.println("Finally block executed");
        }
    }
}
A
Array index out of bounds caught
Finally block executed
B
Arithmetic Exception caught
Finally block executed
CRuntime error: NullPointerException
DNo output
Attempts:
2 left
💡 Hint
Which exception is thrown by accessing arr[5] when arr length is 2?
🧠 Conceptual
expert
2:00remaining
Behavior of try-catch-finally with return statements
What is the output of this Java code?
Java
public class Main {
    public static int test() {
        try {
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            return 3;
        }
    }
    public static void main(String[] args) {
        System.out.println(test());
    }
}
A3
B2
CCompilation error
D1
Attempts:
2 left
💡 Hint
Remember that finally block executes even after return statements.