It's probably the simplest thing in history java Interview questions .

The subject is very simple , Completion code , Determine whether an integer is an odd number :
public boolean isOdd(int i)

I believe a considerable number of people are already preparing to make complaints about the problem. , Just look at it 《 Programming Pearls 》 We all know the answer to this question and the simple truth . But don't swear , Believe it or not , Many of the answers I got to this written test are like this :


public boolean isOdd(int i) {

    if (i % 2 == 1) {

        System.out.println(" It's odd ");

    } else {

        System.out.println(" It's even ");



   And compile it , Errors found , To flex one's head , At most, it's like this :

public boolean isOdd(int i) {

    if (i % 2 == 1) {

        return true;

    } else {

        return false;



ok , I admit I have some problems with my ability to screen resumes , But believe it or not , Programmers who have worked in many large factories for several years , Will write code of the above style .

So I continued to lead :

I :“ What kind of value does the definition of this function require ?”

The candidate looked at the question stem :“ Boolean type .”

I :“ that , you if What kind of value is the expression in parentheses ?”

When it comes to this step , As many as 20% of the candidates still choose to give up , They don't know . ok , I really don't know your confidence in the job interview . But most people think about it , And the right answer :

candidate :“ It's also a boolean type .”

I :“ so what ?”

There are a few candidates who haven't said it , But I can see that they think it's just a coincidence , I don't know how to proceed . however , Most people think about it , It will still be optimized to the following code :

public boolean isOdd(int i) {

    return i % 2 == 1;


It's over the first hurdle , Guide the second level :

I :“ Then I'm sending in a -1 What about ?”

Nearly half of them, after thinking about it, would say that they were taught that only natural numbers can be odd or even , Negative numbers have no parity . The rest of the people accepted the setting , Thought for a while , Change to this :
public boolean isOdd(int i) { return i % 2 == 1 || i % 2 == -1; }
  And optimize to this after the prompt :
public boolean isOdd(int i) { return i % 2 != 0; }

ok , This is by far the first code that can be compiled and fully meet the requirements . tell the truth , It was written like this from the beginning , If there are no other obvious shortcomings , I can basically pass here . I admit that my requirements are relatively low , But there's not a lot of people who come to the interview who can write this directly , A rough estimate , About 10 to 20 percent .

But it's not over here , And the third most important level :

I :“ Is there a better way ?”

candidate :“?”

I :“ I think the mold taking operation is slow , Is there a faster solution ?”

Except for a few people who can think for themselves , most ( No exaggeration ) The candidate said he didn't or didn't know , So I'll take the next step :

I :“ What's the difference between odd and even numbers converting to binary ?”

Quite a few candidates say they don't know binary and bitwise operations , Some also say java no c language , You don't have to study this , I make complaints about many reviews. x equally . A few candidates think about it , Answer timidly .

candidate :“ The last bit of the odd number is 1, Even last bit is 0.”

I :“ so what ?”

What's strange here is , Most of the candidates who can talk to us here will remember the shift operation , I really don't know why , Although this problem can have this kind of operation :
public boolean isOdd(int i) { return i >> 1 << 1 != i; }
But that's not the point at all okay !!!

in short , in any case , After all kinds of guidance in the third level , Who can write the following results , Really not much . Someone who can write without any guidance at the beginning , Only two have been seen so far , One I take everywhere I go , One turned me down offer.
public boolean isOdd(int i) { return (i & 1) == 1; }
Don't think it's over ! ultimate boss coming :

I :“ Is this faster than the above modular operation ?”

candidate :“ Of course , Bit operation must be fast .”

I :“ But we tested the actual code , Find the location and operation and mold taking operation above , The actual running time is about the same , Why? ?”

Candidate's heart mmp:“ You've been playing with me for a long time ???”

But the one who can really answer the question , I didn't see it during the interview , Maybe Daniel can't even see my company . Only in a company , A colleague thought about it , Gave me the right answer . Is it because the companies I've been through are too low Are you ready ……

Summary :

In the end, I compiled a copy Java Relevant information , You can get the needed partners for free



©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple )os Simple use of module Browser kernel ( understand ) Some East 14 Pay change 16 salary , Sincerity or routine ?HashMap Explain in detail It's unexpected Python Cherry tree (turtle The gorgeous style of Library )html Writing about cherry trees , Writing about cherry trees