Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question

 
Ashley Bideau
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output of below program?

public class test
{
private int count = 1;

public synchronized void someMethod()
{
for(int i=10;i<10;i++)
{
System.out.println(count++);
}

}
public static void main(String[] args) throws Exception
{
test demo = new test();
Thread a1 = new XYZ(demo);
Thread a2 = new XYZ(demo);
a1.start();
a2.start();

}
}

class XYZ extends Thread
{
test demo;
XYZ(test td)
{
demo=td;
}
public void run()
{
demo.someMethod();
}
}


A) print numbers 0-19 sequentially
B) print numbers 1-20 sequentially
C) print numbers 1-20, order can not be determined
D) print numbers 0-19, order can not be determined
E) code will not compile
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its option F.) None of the above.
 
Ashley Bideau
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what if we have got only option till (E) in mock paper
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The mock question may have typo for "i=10".
public synchronized void someMethod()
{
for(int i=0;i<10;i++)
{
System.out.println(count++);
}
}

In this case, Answer B is correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic