aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Ashley Bideau
Ranch Hand

Joined: Apr 22, 2006
Posts: 40
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

Its option F.) None of the above.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ashley Bideau
Ranch Hand

Joined: Apr 22, 2006
Posts: 40
But what if we have got only option till (E) in mock paper
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question