*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q from examulator.com 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 "Q from examulator.com" Watch "Q from examulator.com" New topic
Author

Q from examulator.com

Srinivasan Mani
Ranch Hand

Joined: Apr 03, 2002
Posts: 36
Which of the following statements are true?
1)The following statement will produce a result of 1. System.out.println( -1 >>>2);
2)Performing an unsigned left shift (<<< on a negative number will always produce a negative number result
3)The following statement will produce a result of zero, System.out.println(1 >>1);
4)All the Java integral types are signed numbers
Ans:3.Reason given for not choosing 4,is that 'char' is also an integer type which is unsigned.Is this correct?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The char type is unsigned. It has a range
0 to (2^16)-1 ( here ^ represents "to the power of").
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Srinivasan Mani
Ranch Hand

Joined: Apr 03, 2002
Posts: 36
Whether 'char' can be considered as integer type?
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
char is an integer type, or more specific it can be promoted to int implicitly, but casted to byte and short explicitly as it is unsigned type so there might be a loss of precission with implicit casting.
HTH


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Srinivasan Mani
Ranch Hand

Joined: Apr 03, 2002
Posts: 36
Thanks Alfred & Barry.
Srini
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Alfred,
Can u explin the following more ..
but casted to byte and short explicitly as it is unsigned type so there might be a loss of precission with implicit casting.

The following code runs fine without any explicit casting !


--Shweta<br />SCJP 1.4 <br />SCWCD
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Shweta, in your code you're using literals which are compile time constants - meaning their values are known and fixed at compile time - and that's why the compiler knows there will be no loss of precision. But trying the following code you get this error:
java:4: possible loss of precision
found : short
required: char
char c = s;
^
1 error

as the compiler doesn't know at compile time what value the short s will have. Same goes to byte, char, both ways char <--> byte/short
HTH
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Thanks for clarifying Alfred..!!!
I got it confused with something else.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Q from examulator.com
 
Similar Threads
Marcus Green - What is Java Integral Type
integer/integeral type?????
Integral types -- MG Exam 3 Q 25
From marcus green
true/false