5 inadequate Java job interview questions

5 inadequate Java job interview questions


Thanks to the original writer and article :

https://medium.com/@KosteRico/5-inadequate-java-interview-questions-35390256c79d

The questions were taken from the?lecture?on JPoint 2016.

Questions first, then answers and explanations. Try to ask questions on your own.

There may be multiple answers.

Question #1

Which line(s) will print?false? 1, 2, or 3?

No alt text provided for this image

Question #2

Which method will be executed? 1, 2, 3, or Compile error?

No alt text provided for this image

Question #3

What string will be printed during the execution of?run()?method?


No alt text provided for this image

Question #4

Which line(s) will print?true? 1, 2, or 3?

No alt text provided for this image

Question #5

Which line(s) will print?true? 1, 2, both, or none of them?

No alt text provided for this image

Answers and explanations

Question #1

No alt text provided for this image

equals() method implementation in String

Line #1 doesn’t even compile because there’s type mismatch between?String?and?StringBuilder.

Line #2 gives?false?because the second object should be?String?as well.

Line #3 prints?false?as well because?StringBuilder?doesn’t have implemented?equals()?method.

Question #2

There’s just a rule: methods with vararg parameter have the least priority. And the number casting operation has more priority than boxing operation. That’s why the answer is 2.

Question #3

In Java we can create labels like?label:?. This feature helps us to break outer loops. Here’s a good and short explanation:

https://stackoverflow.com/a/51682383

So, this code will run correctly and print us?My search engine:.

Question #4

Line #1 will print?true?because when we initialized?"hello"?the first time in?s1, the string gets to the string pool. And instead of creating a new string for?s4, Java just takes the existing string from the string pool. And that’s why?s1?and?s4?link to the same object.

Line #2 will print?false?because when we have a non-final string in concatenation we create a new object.

Line #3 gives?true?because when we use only final strings and literals, concatenation happens during compile time.

Question #5

No alt text provided for this image

Let’s go deeper into Java source code of?Long?class. As we know, Java uses?valueOf(..)?method to autobox primitive number values. And if we take a look at the implementation of the method, we can see that while autoboxing of?111L?Java caches our number and doesn’t create a new object. In most wrapper classes Java caches numbers between -128 and 127.

That’s why 1st line prints?true?and 2nd -?false?.

These 5 questions tell us that?Java is huge and it’s almost impossible to know everything.

Nevertheless, hope you’ve leveled up your Java knowledge a bit.

Mohamed ElZaiat

Senior Backend Engineer | Java | Kotlin | Swypex

2 年

Really informative, thank you! I've left you a private message, feel free to check it out.

回复

要查看或添加评论,请登录

Omar Ismail的更多文章

社区洞察

其他会员也浏览了