Menu
Question Index
...

What will be the output of the following program?

class StudentMarksTest {
    static int count = 0;
    public static void main(String[] args) {
        Marks marks = null;
        while (count <= 4) {
            marks = createMarks();
            try {
                for (int i = 0; i < 5; i++) {
                    System.out.print(marks.marks[i] + "-");
                }
            } catch (NullPointerException npe) {
                System.out.print("NPE-");
            } catch (ArrayIndexOutOfBoundsException aie) {
                System.out.print("AOE-");
            } catch (Exception ex) {
                System.out.print("E-");
            } finally {
                count++;
            }
        }
    }
    private static Marks createMarks() {
        if (count == 0) return null;
        if (count == 1) return new Marks();
        Marks marks = new Marks();
        marks.marks = new int[count];
        if (count > 3) {
            marks.marks[0] = marks.marks[1] = marks.marks[2] = 3;
        }
        return marks;
    }
}
class Marks { int marks[]; }


NPE-0-AOE-0-0-AOE-0-0-0-AOE-0-0-0-0-AOE-
NPE-NPE-0-0-AOE-0-0-0-AOE-3-3-3-0-AOE-
NPE-NPE-0-0-AOE-0-0-0-AOE-0-0-0-0-AOE-
NPE-0-E-0-0-E-0-0-0-E-0-0-0-0-E-
Compilation Error or some other output not shown here

Doubts

Problems

Topic: Exception Handling Syntax In Java Programming

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App