Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion question

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, while practiticing for the test I came across this question (found on Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 5342-5345). Enthuware. Kindle Edition. ):

Which of the following declarations are valid? Select 3 options

A.
B.
C.
D.
E.


I answered C,E and I was torn between B and D. Eventually I went for D as D felt smaller than B. I got it right, because apparently B is a double but, I wonder, are these kind of questions likely to be appearing on the exam? I thought that conversion wasn't a topic. If it is, or if it could appear in the exam, is there a way to quickly convert numbers?
thanks

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15644
47
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a computer with a JDK installed, then you can very quickly find the answer to this question yourself. Just write a very small program and try to compile it, and see which of these compiles and which ones give you an error.

Whether an assignment to a float is valid has nothing to do with if the value of the number is big or small. It's about the types.

The Java Language Specification specifies the rules about what types of values can be assigned to a different type of variable without casting or explicit conversion.

The literal values on the right side of the = in answers A and B are of type double. Converting these values to a float means you are doing a narrowing primitive conversion, which requires a cast. The cast is required because such a conversion throws away part of the value (bits are discarded), so, with a cast, you must confirm to the compiler that that's OK.

The literal values on the right side of the = in answers C, D and E are of type int. Converting these values to a float means that you are doing a widening primitive conversion, which does not require a cast.

Whether these questions will be on the exam, is something I don't know for sure.
 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder, are these kind of questions likely to be appearing on the exam? 


No. Exam doesn't ask you to convert between one number system value to another numbering system value . Hexadecimal , Octal, binary, Decimal number system values are considered  as integer values in Java. Integer values can be automatically promoted to long, float,double types.  Exam doesn't tests about data types range.  Even If question contains those number system values , Keep in mind they are integer values and can be automatically promoted to long, float,double.

Hope it helps !
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, but if all hex are ints why do they say that 43e1 is  a double? If it's an int then an int can fit in a float so there is no problem
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Thanks guys, but if all hex are ints why do they say that 43e1 is  a double? If it's an int then an int can fit in a float so there is no problem


43e1 is *not* a hex literal. Hexadecimal literals starts with "0x".

Henry
 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
43e1 is decimal number system value and it's value is 430.0 which is double in Java.

Hope it helps !
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, sorry it's that I've never come across that notation before, is there a way to convert that decimal 43e1 into a more readable decimal? If so what's the general rule?
thanks
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Right, sorry it's that I've never come across that notation before, is there a way to convert that decimal 43e1 into a more readable decimal? If so what's the general rule?


It's scientific notation ... https://en.wikipedia.org/wiki/Scientific_notation

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic