• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Thread Question

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its option F.) None of the above.
 
Ashley Bideau
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what if we have got only option till (E) in mock paper
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic