Which two are possible outputs?

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?

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



Leave a Reply 7

Your email address will not be published. Required fields are marked *


Sandeep

Sandeep

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().

Daniel Reis

Daniel Reis

The question should be: Which two are possible first outputs? or something of this.

tom

tom

+1 for Sandeep’s answer

AchVan

AchVan

The catch block isnt catching anything. So compilation fails

Noname

Noname

Obviusly the answer variants and/or snippet are incorrect

ming

ming

Syntex error. The catch block (RunTimeException e) is missing.

Hans

Hans

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.