Menu
Question Index
...

What will be the output of the following program?

public class Ghosts {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            try { execute(i); } catch (Exception e) { }
            System.out.print("-");
        }
    }
    public static void execute(int i) {
        p('G');
        try {
            p('H');t(i == 1);
            try {
                p('O');t(i == 3);
            } finally {
                p('S');
            }
            p('T');t(i == 2);
        } catch (Exception e) {
            p('S');t(i == 3);
        }
        p('T');t(i == 4);
    }

    public static void p(char c) { System.out.print(c); }
    public static void t(boolean thrw) { if (thrw)throw new RuntimeException(); }
}


GHOSTT-GHST-GHOSTST-GHOSS-GHOSTT-
GHOSTT-GHST-GHOSTST-GHOS-GHOSTT-
GHOSTT-GHOSTT-GHOSTST-GHOS-GHOSTT-
Some other output or Compilation Error

Doubts

Problems

Topic: Java Throw Keyword - Java Throws Keyword

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App