Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ques. from JWhiz seems to have a wrong answer

 
neha jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
following is a ques. from jwiz.
What will the result be for the following block of code when it is executed?
int i = 3;
int j = 0;
float k = 3.2F;
long m = -3;
if (Math.ceil(i) < Math.floor(k))
if (Math.abs(i) == m)
System.out.println(i);
else
System.out.println(j);
else
System.out.println(Math.abs(m) + 1);
the correct answer given for this is that output wiil b 4.
but what i guess is that it should give a compile time error,as ceil method takes only double as an argument.if we pass integer it will give a compile time error.
can anybody help me.
neha.
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by neha jain:
the correct answer given for this is that output wiil b 4.
but what i guess is that it should give a compile time error,as ceil method takes only double as an argument.if we pass integer it will give a compile time error.
can anybody help me.
neha.

The correct answer is 4. No compile error in this case. It is *always* possible to pass an integer to a method which takes double as per conversion rules. Conversion rules for primitives take place in -

    * Assignemnt
    * Method Calls
    * Arithetic operations

    HTH,
    - Manish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic