Menu
Topics Index
...
`

Ghosts


If you need explanation Read this topic

If you need Answer Take test on this topic

Ghosts
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(); }
}

A. GHOSTT-GHST-GHOSTST-GHOSS-GHOSTT-
B. GHOSTT-GHST-GHOSTST-GHOS-GHOSTT-
C. GHOSTT-GHOSTT-GHOSTST-GHOS-GHOSTT-
D. Some other output or Compilation Error
Topic: Java Throw Keyword - Java Throws Keyword

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans a: finally method will be executed after try block without any throw

Posted by Manasa Jyothi Sree    2014-02-17 13:37:18


Congratulations Manasa Jyothi Sree. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-18 04:44:21


© meritcampus 2019

All Rights Reserved.

Open In App