File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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


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

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.

[Blog] [Blogroll] [My Reviews] My Linked In
Tony reedy

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

Joined: Jan 07, 2002
Posts: 2205
This is a good discussion of assignment of literals that's also useful to understand:

SCJP 1.4
I agree. Here's the link:
subject: Promotions?
jQuery in Action, 3rd edition