wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Promotions? 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 "Promotions?" Watch "Promotions?" New topic
Author

Promotions?

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

Options are :
1)Code will fail to compile
2)It will print d
3)It will print c
4)It will print b
5)It will print a
Ans : 5)It will print a
I tried running the program , it prints "a"
I feel that the statement(i=f) is not correct and even c++ is wrong?
Am i right?
A bit confused since I cannot understand how are we getting such an output??
Any suggestions?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
the answer is correct. a char primitive contains a numeric value, thus comparison and incrementation are valid operations for chars.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Tony reedy
Greenhorn

Joined: Jan 11, 2002
Posts: 21
The expressions are correct.
When "if (i == f) c++;" is evaluated ,i is converted to float and so 5.0 . As 5.0 != 5.5 ,C++ is not evaluated
when"if (((int) (f + d)) == ((int) f + (int) d)) c += 2;"
f+d is added to get a double value 9.3 which is casted to int and result is 9 .
(int)f =5 + (int)d= 3 and the result will be 8.
9!= 8 so rest of the expression is not evaluated
so the answer is 'a'.
hope this explains.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
This is a good discussion of assignment of literals that's also useful to understand:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014061


Rob
SCJP 1.4
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Promotions?