aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes bit shift, thread q's 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 "bit shift, thread q Watch "bit shift, thread q New topic
Author

bit shift, thread q's

kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
int b1 = 1 << 31�� int b2 = 1 << 31 ; <br /> b1>>>= 31 ; b1>>>= 1 ;
b2>>= 31 ; b2>>= 1 ;
��A. b1 is all zero, b2 is all zero.
��B. b1 is all zero, b2 is all ones.
��C. b1 is all ones, b2 is all zero.
��D. b1 is all ones, b2 is all ones

Ans? b?
Q2)
I posted this one a while ago but no one answered yet, if anyone knows, pls, reply thank you
Can abstract method or class be static??
Also, Consider the following Thread coding1.public class SyncTest
2.private int x;
3.private int y;
4.private synchronized void set X (int i) x=i;
5.private synchronized void set y (int i) y=i;
6.public void setXY (int i) setX(i);set Y(i);
7.public synchronized boolean check() return x!=y;
8.
Under which conditions will check() return when called from a different class
A.check()can never return true
B.check() can return true when set XY is called by multiple treads
C.check() can true when multiple threads call set X and set Y separately
D.check() can only return true if synchTest is changed to allow x and y to be set separately
Correct answer(s)??

Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Kevin
For Q1, answer B is correct:

int b1 = 1 << 31; int b2 = 1 << 31 ; <br /> b1>>>= 31 ; b1>>>= 1 ;
b2>>= 31 ; b2>>= 1 ;

after the 1st line both b1 and b2 are:
10000000 00000000 00000000 00000000
b1>>>=31 gives
00000000 00000000 00000000 00000001
b1>>>=1 gives
00000000 00000000 00000000 00000000
b2>>=31 gives
11111111 11111111 11111111 11111111
b2>>=1 gives the same answer
11111111 11111111 11111111 11111111
so B is correct.
hope that helps
Dave
[This message has been edited by Dave Vick (edited June 18, 2001).]


Dave
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Kevin,
Dave has already verified your Q1 answer, so ...
Can abstract method or class be static??
A static class (by definition in java only inner classes can be static) can be abstract. The following code shows that it will compile and run.

An abstract method can't be static for a simple reason. Abstract means that the method must be overridden, while static negates overridding (static methods can not be overridden, only hidden!).
Regards,
Manfred.
kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
Thank you all for reply=)
Manfred,
Could explain in more detail what you mean by "hidden"?? You said->"static methods can not be overridden, only hidden!".
Does that mean once u override static method with another static method, the original method could never be reached(called)??
thank you
kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
Also,
Can anyone explain this problem??Q2)
I posted this one a while ago but no one answered yet, if anyone knows, pls, reply thank you
Can abstract method or class be static??
Also, Consider the following Thread coding1.public class SyncTest
2.private int x;
3.private int y;
4.private synchronized void set X (int i) x=i;
5.private synchronized void set y (int i) y=i;
6.public void setXY (int i) setX(i);set Y(i);
7.public synchronized boolean check() return x!=y;
8.
Under which conditions will check() return when called from a different class
A.check()can never return true
B.check() can return true when set XY is called by multiple treads
C.check() can true when multiple threads call set X and set Y separately
D.check() can only return true if synchTest is changed to allow x and y to be set separately
Correct answer(s)??

thanks!!!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Kevin
What Manfred is saying is that when a method is static it is a class method. The method is called without any reference to a specific object. That means that the method to be called is determined at compile time, not at run time as with overriding.
check out this code here:

I call the static methods with object references to illustrate the point that they are not subject to late binding. The static methods called are determined by the type of the variable not by the type of the object it references. Unlike the nonstatic methods that are called based on the type of the object referenced.
hope that makes it a little clearer for you
Dave
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
What Dave said ... he types faster than I do!
Manfred.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by Manfred Leonhardt:
What Dave said ... he types faster than I do!
Manfred.

Or, he has no life and spends much of his free time here, sucking all the knowledge around him. And occasionally letting a little slip back out.
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Originally posted by Manfred Leonhardt:
Hi Kevin,
Dave has already verified your Q1 answer, so ...
[b]Can abstract method or class be static??

A static class (by definition in java only inner classes can be static) can be abstract. The following code shows that it will compile and run.

An abstract method can't be static for a simple reason. Abstract means that the method must be overridden, while static negates overridding (static methods can not be overridden, only hidden!).
Regards,
Manfred.[/B]

But I had read somewhere "static methods can not be overridden to be non static, does it mean static methods can be overridden by static methods only?
Please Clarify.
Thanks in advance
Jyotsna
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jyotsna
from the JLs section 8.4.6.2
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.

If you have a static metod in a class and then extend that class, the sub class can not have the same method unless it is also static. That method is considered hidden not overridden because it is subject to early binding, not late binding as in instance methods that are overridden.
I hope that answers your question, if not let me know...
Dave
 
Consider Paul's rocket mass heater.
 
subject: bit shift, thread q's