• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

ques. from JWhiz seems to have a wrong answer

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic