The simplest one Java interview questions on history, but no one can

This is probably the easiest one in the history of java face questions of.

Title very simple completion code, determines whether an integer is odd:

public boolean isOdd(int i)

I believe a significant number of people are already preparing Tucao, as long as read "Programming Pearls" people know the answer to this question and in which a very simple truth. But do not worry squalling, whether you believe it or not, this road pen questions I get a lot of answers are a long way:

public boolean isOdd(int i) {
    if (i % 2 == 1) {
        System.out.println("是奇数");
    } else {
        System.out.println("是偶数");
    }
}

Then compile it and found a mistake, scratched his head, at most, change it to this:

public boolean isOdd(int i) {
    if (i % 2 == 1) {
        return true;
    } else {
        return false;
    }
}

Okay, I admit I may have some problems in the ability to resume screening, but believe it or not, a lot of manufacturers worked for several years programmers will write code style as above.

So I continue to boot:

I: "The definition of a function asked to return what type of value?"

Candidates looked casual working: "Boolean."

Me: "So, the value of the expression in brackets behind you if there is a type of what?"

Step guide to this time, there are still up to two percent of the candidates chose to give up, he said they did not know. Well, I really do not know you confidence to interview for this position lies. But most people thought, but also to answer the correct answer:

Candidate: "is also a Boolean."

Me: "Then what?"

Although a small number of candidates did not say it, but I can tell they think it is just a coincidence, do not know how the next step. However, after most people thought, or will be optimized for the following code:

public boolean isOdd(int i) {
    return i % 2 == 1;
}

It took a first pass, the second hurdle guide:

Me: "Then I passed in a -1 it?"

Nearly half of the people after the thought that they would be fooling only natural that I was taught odd and even number of points only, no parity this negative to say. The remaining people to accept this setting, for a moment, change it to this:

public boolean isOdd(int i) {
    return i % 2 == 1 || i % 2 == -1;
}

And optimized in such a way after the prompt:

public boolean isOdd(int i) {
    return i % 2 != 0;
}

Well, this is by far the first and compiles the code to fully meet the needs of the realized. To be honest, the beginning written such a person, if there is no other obvious shortcomings, I can here basically adopted. I admit that my requirements are relatively low, but to interview people directly wrote this really is not too much, a rough estimate would probably account for one to two percent of it.

But this is not over yet, and most importantly, the third shut it:

Me: "There is a better way?"

candidate:"?"

Me: "I think the modulo operation is relatively slow, a faster solution?"

Apart from a few people can think about was thought out than the vast majority (no exaggeration) indicates that no candidate or do not know, so the next step tips:

Me: "odd and even converted into binary What is the difference?"

A considerable part of the candidates said they do not understand what a binary bit computing and, some said not a java c language, these studies do not, just like a lot of comments will Tucao I loaded to force the same. A small number of candidates thought, would sheepishly replied.

Candidates: "The last one is an odd number, the last digit is an even number 0."

Me: "Then what?"

Very strange point here is that most can talk to the candidates will come to think of it shift operation, I really do not know why, although this question may indeed have this operation:

public boolean isOdd(int i) {
    return i >> 1 << 1 != i;
}

But this is simply not the point right! ! !

In short, in any case, to third after various boot off, you can write the following results to the people, really small. You can start without any written guidance on who has only seen two, one where I took her, a refuse my offer.

public boolean isOdd(int i) {
    return (i & 1) == 1;
}

Do not think this is over! The ultimate boss came:

Me: "This is not a modulo arithmetic is faster than the above?"

Candidate: "That, of course, certainly was quick bit computing."

Me: "But we actually tested the code, find the above bit operation and a modulo operation, the actual running time is about the same, why?"

Candidates mind mmp: "This is all the talk, you make me play this game ???"

But the real answer to the cause of the people, I have not seen the interview process, it may be the big cattle companies are failing to appreciate where I was now. Only one company, a colleague thought gives me the right answer. Is my experience of the company are too low yet ......

Author: Fang Zhihong
Source: https://zhuanlan.zhihu.com/p/57859872

you may also like

1, GitHub starred 3.2w! The most complete history and technical personnel interview Manual! FackBoo initiated and summary

2, how to become a good architect?

3, starting from scratch to build a back-end technology stack start-up companies

4, programmers can generally take private live from what platform?

5, 37-year-old programmer to be cut, 120 days not find a job, but unfortunately to small companies, the result of senseless ...

6, drops in Taiwan to build business practice, for the first time exposure

7, do not accept their fate, 10 years from assembly line workers to the work program Google Yuan, an inspirational story of Hunan sister

8, 15 and fuss FIG understand the difference between efficient!

Released eight original articles · won praise 17 · views 60000 +

Guess you like

Origin blog.csdn.net/emprere/article/details/104305588