Rajat Gupta

+ Follow
since Sep 16, 2005
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rajat Gupta

I guess <body-content> is a required while specifying the <taglib> in DD. If you don't have it defined, its an error. Hence, the default value doesn't exist. Please correct me if I am wrong.

Rajat Gupta
The output of "Page 2" is correct.

After any Requestdispatch or sendRedirect or jsp:forward, all the contents of the forwarding page are lost. That's what is happening for the Page1. Also, after the RequestRedirect, the control from page1 is transferred to the called jsp, so "page3" never gets printed.
The reason why the IllegalStateexception won't occur, is that, the response is never "committed" or "sent to the browser". Hope it helps.

In response to gutta Rahul..

It seems that your assumptions is perfect in relation to the code that you have written. According to your code, as you changed the SetXY method, the first thread will execute the setX method (at this time only this thread will have the ownership of all the synchronized methods). Once this thread is done with SetX, it leaves the lock and goes to sleep, as SetXY is not synchronized.

In between, the next thread comes in and again takes ownership of the SetX method, and goes to sleep. After setX method is over, this second thread too leaves the ownership of all the synchronized methods.

Threads have different sleep timings and this is the reason that they go and execute the check method at different times, and that result in true.

So, (B) is perfect for the code you have given, but if the SetXY is not changed, I will still stick to (C).

Rajat Gupta
quote riginally posted by Chandrakanth Dn:
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX( int i ) { x = i; }
5. private synchronized void setY( int i ) { y = i; }
6. public void setXY( int i ) { setX(i); setY(i); }
7. public synchronized boolean check() { return x != y; }
8. }
Under which condition will check return true when called from a different class?
A. check can never return true.
B. check can return true when setXY is called by multiple threads.
C. check can return true when multiple threads call setX and setY separately.
D. check can return true only if SyncTest is changed to allow x and y to be set
What is the Answer?


I understand that the answer will be (C). Please correct me if I am wrong.

SetX, SetY and check are all synchronized methods. A lock by 1 thread on any one of these methods will imply an implicit lock on the other 2 remaining synchronized methods.

For option (B) B. check can return true when setXY is called by multiple threads.

Any thread that comes to execute setXY will have to accquire a lock on SetX method and that will place an implicit lock on the SetY too. So, using SetXY, no 2 threads can simultaneouly access SetX and SetY. So, in effect, SetXY can be called only in sequence by the threads. And as SetXY sets the value of both x and y as same, so the correct will always return false for B.

For Option (C)
C. check can return true when multiple threads call setX and setY separately.

If 1 thread changes the value of just x, by calling setX and releases the lock after that, then any next thread can access SetY and change the value of Y to a different value. Hence (C) can return false for correct.

Please correct me if I am wrong. Hope this helps.

Rajat Gupta

Which two declarations prevent the overriding of a method? (Choose Two)
A. Final void methoda() {}
B. Void final methoda() {}
C. Static void methoda() {}
D. Static final void methoda() {}
E. Final abstract void methoda() {}

My Answer: B & D
Explaination: A,E contain illegal final keyword with the capital "F". So, they are incorrect method declerations and there is no final in c. Hence B & D.

Hey kartik,

This is what I understand.

Objects in Java are always passed through the method calls "BY VALUE" and never "BY REFERENCE".

Now, lets go through the code.

After the t object is created, it calls the method first(). Here object v gets defined, with values as -- v.i =25
Then a call to the second() method is made. Here, a copy of the object reference is sent to the second method. That is, if v in first is pointing to some object, the copy of that object address is passed to second method. Lets take that address to be obj123.
So, the value of v passed -- obj123


Value received in second method v = obj123.
v.i changed from 25 to 20

In second method, another new Value object val is created. Lets take that val conatins -- obj456
Now, val.i = 20

After v = val

v (in second method ) = obj456

Therefore, in second method print statement, 15 and 0 are printed.

Now, when it goes back to the first method, the value of v is = obj123, beacuse, you have just passed the copy of the address of the object that v was pointing out to and not the object itself. Even if that value of the copy was changed in the called method (second method) , it wont affect the original copy of the object address in the calling method.
So, the value of v.i = 20 is printed.

I hope this clarifies the doubt.

Rajat upta

What I understand, you can put the return statement anywhere in the try or the catch block, but if there is a "Finally" block, it will be executed, no matter what. Hope this answers the query.