File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JDiscuss Test5 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 "JDiscuss Test5 Question" Watch "JDiscuss Test5 Question" New topic
Author

JDiscuss Test5 Question

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026


What will the above code print?

Options

Select 1 correct option.

1.It will keep on printin same values for x and y incrementing by 1 on each line.
2.It will keep on printin same values for x and y but they may be incrementing by more than 1 on each line.
3.It may print different values for x and y but both the values will be incrementing by 1 on each line.
4.You cannot say anything about the values.

The answer given is option 1. I think the answer is option 4.

Please give your suggestion


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
Option 1 is correct.

Both the incrementation and the printing operations are in the same synchronized block. As such it is like an atomic operation. If you increment, then you have to print.

At any given time since only one thread can increment x and y, and since you are incrementing in steps of one, the correct choice would be option 1 (assuming x and y were initialized with the same value)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDiscuss Test5 Question
 
Similar Threads
synchronized run()
JQ+ Mock - Threads - Question ID: 988383703406
Threads Q
Static variables accessed in an Instance Method
synchronized context