I suggest you ...

Bug in program

Give a piece of code. Which two are possible results? (Select two)
public class Cruiser {
private int a = 0;

public void foo() {
Runnable r = new LittleCruiser();
new Thread(r).start();
new Thread(r).start();
}

public static void main(String arg[]) {
Cruiser c = new Cruiser();
c.foo();
}

public class LittleCruiser implements Runnable {
public void run() {
int current = 0;
for (int i = 0; i < 4; i++) {
current = a;
System.out.print(current + ", ");
a = current + 2;
}
}
}
}
A) 0, 2, 4, 0, 2, 4, 6, 6,
B) 0, 2, 4, 6, 8, 10, 12, 14,
C) 0, 2, 4, 6, 8, 10, 2, 4,
D) 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E) 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

Correct Ans Displayed in test: A, B

Actual Correct Ans: A
I ran the above code 5-10 times, the sequence always changes as thread behaviour cat be certain. The only fact remained that the value never exceed 6., hence Ans A applies

1 vote
Vote
Sign in
(thinking…)
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
Komal shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

0 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base