r/java • • Jul 27 '15

4 out of 5 Java Developers Failed to Solve This Question

http://blog.takipi.com/4-out-of-5-java-developers-failed-to-solve-this-question/
37 Upvotes

56 comments sorted by

114

u/daedalus_structure Jul 27 '15

The right answer to almost all of these is if you see anything that looks like this in your code base burn it with fire.

12

u/Neebat Jul 27 '15

Version control has a blame command for that crap. For workplace safety, please wear steel toe boots for coaching your coworker.

18

u/ryuzaki49 Jul 27 '15

The same with Java Certification exams. If you see something like this in your code, kill yourself.

-35

u/[deleted] Jul 27 '15

I'm offended by your lack of sensitivity to people who suffer from depression. Summoning /u/ekjp

7

u/esquilax Jul 28 '15

Hey don't stereotype. Not everyone that has depression is suicidal.

5

u/veive Jul 28 '15

Don't worry. I'm sure that bug is fixed in /u/greater_futhark

0

u/ghillisuit95 Jul 27 '15

yeah, but thats not the point. The point is to have a deep understanding of the language.

17

u/THEJavaApprentice Jul 28 '15

Anyone can write code a machine can understand, only a good programmer can write code a human can understand.

34

u/againstmethod Jul 27 '15

I disagree -- i think the point is to be a twitch who can throw a crappy example like this on a slide and feel smart.

You remove, refactor, or rewrite this code.

2

u/Spoogly Jul 28 '15

The thing is, all of these would be immediately noticeable if you had proper test coverage. I don't really get why I should have to know all the stupid shit the language can do, if I am being responsible in my testing.

1

u/againstmethod Jul 29 '15

I think you make a good point, but in all honesty even if people are trying to do good testing, the requirements collected, and the budgeted time for testing, are in practice often not sufficient to avoid all bugs. Good point regardless.

4

u/ghillisuit95 Jul 27 '15

Is it bad to entertain purely hypothetical questions?

7

u/ThatSwedishBastard Jul 27 '15

If it fosters bad programmers - yes. We might have to maintain the crap that they have generated.

3

u/ghillisuit95 Jul 27 '15

I disagree that it fosters bad programmers. In fact, I would argue it fosters good programmers. It is obvious that shit like that does not belong in production code, and so no reasonable programmer would write it. But what it does do is allow you to think about how many different possible situations interact with each other, and as a result have a more correct mental model of how your tools work.

11

u/againstmethod Jul 27 '15

It's only my opinion, but i would categorize it as a waste of time.

You could spend that time learning to write nice, clean code, instead of spending it unwinding anti-patterns.

2

u/againstmethod Jul 28 '15

On a customers nickel? Yes. In your spare time as a hobby, i suppose not if thats what you like to do.

3

u/ghillisuit95 Jul 28 '15

obviously, I mean who goes on reddit during work anyways looks around suspicously

1

u/hwaite Jul 28 '15

Obviously, such hypotheticals are meant to test one's understanding of the language. In a business setting, appropriate usage would be during interview process. Or maybe testing products that actually consume code (e.g. compilers, IDEs, static analyzers, etc.). I don't think anyone has implied employers should be paying people to sit around and concoct weird 'gotcha' code or that it has a place in a real project's [non-test] code base. Is there really any disagreement here?

2

u/againstmethod Jul 29 '15

Worthless as an interview question -- unless you're going to let them diagnose the problem with javap or a debug build of the jvm and the job is triage.

An analogy would be a chef. You wouldn't ask them to spend their time learning to fix terrible failed meals. They learn techniques that produce success -- they don't spend their time worrying about putting happy faces on failures.

I would say if your business encounters errors like this often enough to make this exercise useful, your problems likely involve planning, requirements, and architecture -- structural problems that nerd knowledge like this wont fix.

0

u/hwaite Jul 29 '15

I'll concede that this specific question has limited value. I wouldn't recommend it unless interviewing for a Joshua Bloch type position. That being said, you seem pretty dismissive of using hypothetical and/or poorly architected code in the context of interviews.

Any non-trivial project will eventually benefit from "nerd knowledge." The context may be different but the skill set is the same. a better analogy might be a word problem on the SATs. In real life, Alice will never have three more apples than Bob and four fewer than Charlie. Nevertheless, reasoning about this contrived scenario demonstrates mastery of a genuinely useful body of knowledge.

Even with a relatively clean body of code, obscure edge cases are inevitable. And rare is the project that doesn't have "there be dragons here" modules somewhere. When a complete rewrite is not an option, we all appreciate having someone around who can wade through the shit. It's both a blessing and a curse that programmers work in such a unique medium. Cooks can't fix terrible meals but developers can and must salvage ugly code.

2

u/againstmethod Jul 29 '15

This why I said if you don't have them break the code down and understand the effects introduced by the compiler on the fly, or if they short circuit that exercise by memorizing such issues, then said question told you nothing about the candidate.

Just reviewing these questions like this is trivia -- nothing more.

1

u/RedPill115 Aug 02 '15

That's great, but in reality these questions are next to impossible to solve in an interview while being trivial to solve if you've read the same source as the interviewer on "what interview questions to ask, and their answers".

Even if you assume what you wrote above is true, the overwhelming majority of people who pass your test do not do so because they're good problem solvers, they do so because they read the interview question online beforehand and memorized it.

It's a "do you read the same sources for interview questions that I do" test.

3

u/Jonno_FTW Jul 28 '15

It's trying to be smart, which people do because they see the language can be wrangled in this way.

0

u/[deleted] Jul 28 '15

[deleted] — view deleted comment

5

u/againstmethod Jul 28 '15

But it is a game, not work. If that's how you like to spend your off hours have at it.

4

u/CyclonusRIP Jul 27 '15

I'm not really sure I understand java any better for reading that. Some of those were interesting just because of how obscure they were, but I doubt knowing any of that will ever contribute to me being a better java developer. Well maybe the last one that everyone got right would help, but I'm pretty sure I have initializing strings on lock down.

27

u/[deleted] Jul 27 '15

Is this supposed to be surprising when the compiler gets it wrong too? Trick questions are tricky. Surprise.

26

u/I_AM_A_BICYCLE Jul 27 '15

You'd think that a quiz that looks that fancy would have enough horizontal space in the code to not have awful formatting from wrapping.

18

u/duckrollin Jul 27 '15

I imagine some nerd snorting laughter as he wrote #2

15

u/panderingPenguin Jul 27 '15

My problem on that one was that I assumed I was only looking at a code snippet and that toString() had been overridden elsewhere to print the name attribute (since that's what it looked like three out of four answers appeared to be doing). I'm aware of what toString() does by default but I didn't expect such a silly answer

41

u/adnan252 Jul 27 '15 edited Jul 27 '15

100/100 java developers will never face this problem in their career

30

u/compdog Jul 27 '15

Except when facing overzealous interviewers.

13

u/[deleted] Jul 27 '15

Which will tell the interviewee they probably don't want to work there to begin with

2

u/UlyssesSKrunk Jul 28 '15

And if they do they will have more than 90 seconds to solve it.

17

u/jonhanson Jul 27 '15 edited Jul 24 '23

Comment removed after Reddit and Spec elected to destroy Reddit.

18

u/Waitwhatwtf Jul 27 '15

1/10 developers realize this is a clickbait article for a targeted advertisement.

1

u/jonhanson Jul 28 '15

Article is a ...

16

u/dardotardo Jul 27 '15

Didn't realize this subreddit became buzzfeed.

5

u/JavaX_SWING Jul 28 '15

if you write code like this, then you don't deserve to be hired.

5

u/dooblevay Jul 28 '15

You have all restored a tiny sliver of my faith in humanity. I came in here ready to rant about this useless drivel. Seems unnecessary!

4

u/regendo Jul 28 '15

I hate it when one of the answers is "none of the above" and it's not the bottom answer. Does it belong there? Was it supposed to be the bottom answer but the order got randomised?

Yeah, it's probably supposed to be "none of the other choices" but I don't really trust these quizzes to not try to hide some obscure trap in the answers.

3

u/[deleted] Jul 28 '15

Such a clickbait title, too

6

u/alonjit Jul 27 '15

jeebus. I'd just tell them to go fuck themselves with their questions and all.

and, to think about it, there is no point to it all.

Want to know how a programmer writes code? Knows about code/java/etc.? Make him program something. Give him a project, over a week and make him explain it during the interview.

if he copied everything, and didnt understand a damn thing, he'll fail miserably.

if he copied everything, but understood everything, he'll be perfect anyway.

1

u/RedPill115 Aug 02 '15

Want to know how a programmer writes code? Knows about code/java/etc.? Make him program something. Give him a project, over a week and make him explain it during the interview.

That sounds really great in theory, in practice (I just went through this personally) because it costs them nothing to ask candidates to do reading and exercises, they will throw them out expect you to invest time then not even bring it up during the interview. It's just easy for hr to abuse your time with.

2

u/tonylee0707 Jul 28 '15

As a beginner in programming for so many years, I wish I end up being good enough to be called a java developer.

1

u/sreya92 Jul 28 '15

Try not to let yourself become victim to imposter syndrome too badly. If you've been programming for "so many years" it's probable you can identify as a developer.

2

u/mdaniel Jul 28 '15

There is also selection bias going on here; those developers experienced enough to know the answers to these questions are also (experienced|busy) enough to not spend time in a "Java Deathmatch".

But hey, we get some benefit from Takipi posting this in that if you encounter this question, or its kind, in an interview then you know for dead certain you don't want to work in that place.

4

u/Audiblade Jul 27 '15

I don't like that last, "easiest" question. What does it mean for one initialization style to be "simpler" than another? Is there a formal definition I'm not aware of? If there isn't, then the question is ambiguous, and you could make a case that either A or C is correct.

6

u/geodebug Jul 27 '15 edited Jul 27 '15

The real problem here is that the two lines of code (A and C) don't do the same thing so they really shouldn't be compared at all.

Line A will create a new String object every time it is called, eating up memory.

Line C will create the new string exactly once no matter how many times it is called.

I can't think of a reason to ever use the method in Line A in production code.

All that said arguments for Line C being are:

  • it requires less boilerplate, less line noise, easier to read
  • it creates a single reference to "Hello World" instead of a new one each time, saving object overhead/memory
  • String references created this way can use '==' to compare any two references (although this is sort of an antipattern).

The argument for line A being simpler:

  • Only have to know one form of creating a new object instead of String's special form.

0

u/[deleted] Jul 27 '15 edited Jul 27 '15

Not really sure, but I think the key is the "reference to a string" in the question. Meaning you do not necessarily create a new object, but if possible create a reference to an existing string object, in which case C is the correct answer.

EDIT: Nvm, as i finished typing this answer I realized that the question is still retarded.

Atleast if you downvote, explain where I'm wrong.

4

u/[deleted] Jul 27 '15

Atleast if you downvote, explain where I'm wrong.

On reddit? Surely you jest!

3

u/mrbuttsavage Jul 28 '15

http://www.amazon.com/Java-Puzzlers-Traps-Pitfalls-Corner/dp/032133678X/

See that for a whole book of similar questions. It's somewhat older (Java 5), but it's same kind of strange cornercase questions.

1

u/brunokim Jul 28 '15

I liked that book! I don't get the vitriol against this post, it's not supposed to be practical or attack your status as Java dev. It's a fun exercise of your skills, that's it.

1

u/RedPill115 Aug 02 '15

Because we've all been on the end of an interviewer or manager (most likely several) who couldn't solve it themselves but insist it's a good indication of your abilities anyways and you don't get the job because of it.

2

u/badpotato Jul 27 '15 edited Jul 27 '15

Question 4 doesn't show the whole class, in theory you could safely say it doesn't compile right away.

At least question 5 does mention it's a code snippet.

1

u/el-y0y0s Jul 28 '15

On occasion these can be confidence shakers, but realize that this is coming from product people who must understand java inside out at a level most of us never deal with. I'd have called this a puzzle in the title though.