Trick question for clever people over here!



  • Two snippet of code below. What is difference in them? Why?

    class Main
    {
    
    static void Main(){
    
    float f = 0.1f;
    Console.WriteLine(f);
    int price = 100;
    double d = price * f;
    Console.WriteLine(price - d);
    
    }
    }
    
    class Main
    {
    public static void main (String[] args){
    float f = 0.1f;
    System.out.println(f);
    int price = 100;
    double d = price * f;
    System.out.println(price - d);
    
    }
    }
    

    This is our company interview question for senor level programmer



  • You mean besides that the top one is C# and the bottom one is Java?

    1. Why isn't this in coder challenge?
    2. Why is this a senior level developer question?


  • @rad131304 said:

    You mean besides that the top one is C# and the bottom one is Java?

    1. Why isn't this in coder challenge?
    2. Why is this a senior level developer question?

    I do not think this is challenge. Seeing of obvious is first thing. You are correct only in pointing out that.

    Our planning team decided this is fit for experience developer.



  • The answer is some bit of extremely obscure trivia about how C# and Java differ when casting ints to floats, isn't it?



  • @blakeyrat said:

    The answer is some bit of extremely obscure trivia about how C# and Java differ when casting ints to floats, isn't it?

    Absolutely correct! The float in java is very much sane, unlike float in .NET.



  • @Nagesh said:

    @rad131304 said:
    You mean besides that the top one is C# and the bottom one is Java?

    1. Why isn't this in coder challenge?
    2. Why is this a senior level developer question?

    I do not think this is challenge. Seeing of obvious is first thing. You are correct only in pointing out that.

    Our planning team decided this is fit for experience developer.

    Do you write in both C# and Java at your company?


  • @rad131304 said:

    @Nagesh said:
    @rad131304 said:
    You mean besides that the top one is C# and the bottom one is Java?

    1. Why isn't this in coder challenge?
    2. Why is this a senior level developer question?

    I do not think this is challenge. Seeing of obvious is first thing. You are correct only in pointing out that.

    Our planning team decided this is fit for experience developer.

    Do you write in both C# and Java at your company?

    Our company is big. One of top 10 in India. We have several ongoing project works servicing several client around the world. We are 24 hour shop. There are shifts. In my project, we were exclusive java, but our client in his wisdom has start to give us C# work.

    To answer your question, our company has every technology you can think of.

    Also, in code snip above, only 1 will compile with success. The other will not. Can you tell me which one and why?



  • @Nagesh said:

    Also, in code snip above, only 1 will compile with success. The other will not. Can you tell me which one and why?


    IIRC C# doesn't like methods which have the same name as their enclosing types.



  • @pjt33 said:

    @Nagesh said:
    Also, in code snip above, only 1 will compile with success. The other will not. Can you tell me which one and why?


    IIRC C# doesn't like methods which have the same name as their enclosing types.

    That is correct only. I am not followed Microsoft Logic of not letting this happen. Surely some one decided that a long time ago in C# 1.0 spec.



  • There is a similar question on the interview quiz I wrote, too, but we don't pussyfoot around it with a bunch of head-scratching code written in multiple languages. I just ask the candidate, "what data type would you use for a currency amount in your favorite general-purpose language?" Better than 99% say "float" or "double" and I start moving the interview toward its conclusion. I'd give more credit to an interviewee who suggested using a sufficiently large integer type and just storing cents instead of dollars (or, paise instead or rupiah). Calculations would still be an issue, but at least that answer reveals that the interviewee - maybe - has some inkling about how floating-point and fixed-point really compare.



    There is also a 128-bit "Decimal" type in .NET and that's a good candidate if you need to work with sub-cent (sub-paise) data. Using a "float" or anything like it for money is typically inexcusable, though... maybe it would work in some kind of social science study that's more qualitative in nature, but it damn well doesn't do the job correctly for anything resembling a financial application.



    Incidentally, this is something that was stressed to my classmates and me in our Computer Science degree program. Like "writing a compiler is hard," "floating-point works well for currency" is one of those tell-tale beliefs that lets me know that someone doesn't really know Computer Science. It tells me they're a hawsepiper, not a ring-knocker.



  • @bridget99 said:

    "floating-point works well for currency" is one of those tell-tale beliefs that lets me know that someone doesn't really know Computer Science.

    9/10 I almost bit.



  • @bridget99 said:

    There is a similar question on the interview quiz I wrote, too, but we don't pussyfoot around it with a bunch of head-scratching code written in multiple languages. I just ask the candidate, "what data type would you use for a currency amount in your favorite general-purpose language?" Better than 99% say "float" or "double" and I start moving the interview toward its conclusion. I'd give more credit to an interviewee who suggested using a sufficiently large integer type and just storing cents instead of dollars (or, paise instead or rupiah). Calculations would still be an issue, but at least that answer reveals that the interviewee - maybe - has some inkling about how floating-point and fixed-point really compare.



    There is also a 128-bit "Decimal" type in .NET and that's a good candidate if you need to work with sub-cent (sub-paise) data. Using a "float" or anything like it for money is typically inexcusable, though... maybe it would work in some kind of social science study that's more qualitative in nature, but it damn well doesn't do the job correctly for anything resembling a financial application.



    Incidentally, this is something that was stressed to my classmates and me in our Computer Science degree program. Like "writing a compiler is hard," "floating-point works well for currency" is one of those tell-tale beliefs that lets me know that someone doesn't really know Computer Science. It tells me they're a hawsepiper, not a ring-knocker.

    If you are working with Oracle DB, float datatype is the best candidate for storing currency. That is pure back-end stuff only. Float in Oracle is an exact numeric that doesn't have the rounding behavior of IEEE 754. But if you retrieve float value from database and use them in C# float, you loose values.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.