public class Two {
public static void main(String[] args) {
try {
doStuff();
system.out.println(“1”);
}
catch {
system.out.println(“2”);
}}
public static void do Stuff() {
if (Math.random() > 0.5) throw new RunTimeException(); doMoreStuff();
System.out.println(“3 “);
}
public static void doMoreStuff() {
System.out.println(“4”);
}
}
Which two are possible outputs?
A.
2
B.
4
C.
1
D.
1
The 2 possible values should be 2 and 4, 3, 1.
Reason: In case Math.random() evaluates > 0.5, it the throw an runtime exception, which is Catch by the catch() statement, printing 2.
If Math.random() evaluates to doMoreStuff() > print 4, then print 3 of doStuff() and 1 of the main().
The question should be: Which two are possible first outputs? or something of this.
+1 for Sandeep’s answer
The catch block isnt catching anything. So compilation fails
Obviusly the answer variants and/or snippet are incorrect
Syntex error. The catch block (RunTimeException e) is missing.
More stuff that’s wrong:
Do Stuff is an invalid method name.
When a method throws an exception this should be mentioned in the method signature.