Which two are possible outputs?

<code>
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”);
}
</code>
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 3

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


gaga

gaga

Under B. should be 4
3
1

sully

sully

C & D have the same number for them

So is it 3 & 1 or 2& 4

Oene Bakker

Oene Bakker

De source code is wrong, it should be:

public class Two {
public static void main(String[] args) {
try {
doStuff();
System.out.println(“1”);
}
catch (Exception e) {
System.out.println(“2”);
}
}

public static void doStuff() {
if (Math.random() > 0.5)
throw new RuntimeException();
doMoreStuff();
System.out.println(“3 “);
}

public static void doMoreStuff() {
System.out.println(“4”);
}
}

Possible output:
If RuntimeException is will print 2
Otherwise it will print 4 3 1

Correct Answers are A and B (if B states 4 3 1 like in other exams)