5 inadequate Java job interview questions
Omar Ismail
Senior Software Engineer @ Digitinary | Java & Spring Expert ? | AWS & Microservices Architect ? | FinTech & Open Banking Innovator ?? | Digital Payments Expert ?? | Top 200 IT Content Creator in Jordan ?? | 40K+ ??
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?
Question #2
Which method will be executed? 1, 2, 3, or Compile error?
Question #3
What string will be printed during the execution of?run()?method?
Question #4
Which line(s) will print?true? 1, 2, or 3?
Question #5
Which line(s) will print?true? 1, 2, both, or none of them?
领英推荐
Answers and explanations
Question #1
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
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.
Senior Backend Engineer | Java | Kotlin | Swypex
2 年Really informative, thank you! I've left you a private message, feel free to check it out.