aspose file tools*
The moose likes Mock Exam Errata and the fly likes error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "error" Watch "error" New topic
Author

error

Sunil Chand
Greenhorn

Joined: Oct 31, 2001
Posts: 1
Can anyone help me, the whole statement here is being casted to 'byte'. So the casted value will be truncated to its byte value but in the answers they dont even give you the byte option to select and answer is 'double'. I compiled and checked it out and the value is a double. I cant figure it out, hoe can this return a double value when it is casted into a 'byte'? Does it not cast a complicated statement into a byte or something? can any1 help.
Thanks in advance.

43. What will be the return type of this statement?
(byte)10/10.2*7;

A. short
B. int
C. double
D. float
E. Compilation Error
I got it from http://java.ditmas.net/mocks.htm
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Sunil
The cast only applies to the value it is right beside, in this case the 10. To make the result of the expression into a byte you would do this:
(byte)(10/10.2*7);
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Matt Burba
Greenhorn

Joined: Oct 29, 2001
Posts: 8
Sunil,
I'm studying for the exam, and here's the operator precedence I have in my notes (got most from somewhere on web, then made a couple of additions):
Highest precedence on top of list:
1. postfix ops: [] . (params) i++ i--
2. unary ops: ++i --i +i -i ~ !
3. creation or cast: new (type)expr
4. multiplicative: * / %
5. additive: + -
6. shift ops: >> << >>>
7. relational: > < >= <= instanceof<br /> 8. equality: == !=<br /> 9. bitwise/logical AND: &<br /> 10. bitwise/logical XOR: ^<br /> 11. bitwise/logical OR: |<br /> 12. short circuit AND: &&<br /> 13. short circuit OR: | |<br /> 14. conditional: a?b:c<br /> 15. assignment: = += -= *= /= %= >>= <<= >>>= &= ^= |=

The way it should work, for your statement:
(byte)10/10.2*7;
a. integer literal 10 cast to a byte = 10
b. / and * are same precedence, so evaluated left to right. So, division evaluated first. On right hand side of / is byte, left hand side has double, so byte is automatically promoted to double. 10.0/10.2 = 0.9...
c. That result is multiplied by 7. Again, "7" is an integral literal so it is promoted to 7.0 and the result is a double that is probably pretty close to 6.9.
Make sense?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error
 
Similar Threads
Need last minute help about io and awt
java.ditmas.net mock
SCJP Exam
not cleared
answers to java.ditmas.net Mock Exam