Let’s start with the good news. Congratulations! Your resume has made the cut, and you’ve been invited to an interview for a developer position in Java programming. Now for the slightly less welcome news: your job-seeking efforts are about to get busier than ever. Time and effort, research and practice is required to prepare for any interview, not least some intensive research into the difficult Java interview questions that can be posed to candidates.
The most important thing to understand when preparing for any interview is this: never wing it, no matter how confident you feel. The interview is a golden opportunity for you to personify the potential that the recruiters identified in your CV. So don’t blow it.
There are ways of preparing for what’s in store in a Java interview, and to help you anticipate the trickiest Java interview questions. Quora is a brilliant resource, where experts share their advice and feedback freely. There are also guides such as Top 100 Tricky Java Interview Questions, the Java Interview Guide, and Java Interview Notes: 700 Java Interview Questions Answered. Other reference works include Cracking the Coding Interview: 189 Programming Questions and Solutions, the handy four-star reviewed Coding Interview Ninja, and the popular Java Programming Interviews Exposed.
Java Interview Questions 2022
We have boiled down the huge range of typical and most tricky Java interview questions from these guides, and on Quora, to a list of 20, some of them answered, to help you prepare for your important job interview.
The consensus among Java experts online is, concentrate on the fundamentals, don’t wing it, and do your homework. Then no Java interview questions fired at you on the day will seem at all remotely tricky. Good luck, Java AGENTs!
1. How can you determine if JVM is 32-bit or 64-bit from Java Program?
2. Can you override private or static method in Java ?
“Another popular Java tricky question… You can not override private or static method in Java. If you create a similar method with same return type and same method arguments, that’s called method hiding.”
3. Can you create an immutable object that contains a mutable object?
4. How can you mark an array volatile in Java?
5. What is the difference between StringBuffer and StringBuilder in Java?
“StringBuilder was introduced in Java 5… only difference is that Stringbuffer methods are synchronized while StringBuilder is non-synchronized.”
6. Will this return 5*0.1 = 0.5? True or false?
7. What is the right data type to represent Money (like Dollar/Pound) in Java?
8. Can we use multiple main methods?
“Yes. While starting the application, we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned. Hence there is no conflict amongst the multiple classes… We can overload main method but we can not override it. So, we can have many main methods in a class.”
9. Is ++ operation thread-safe in Java?
10. In Java, can we store a double value in a long variable without explicit casting?
11. How can you do constructor chaining in Java?
12. Anything related to Thread Pooling
Top Quora writer Jayesh Lalwani, an experienced Java interviewer, reveals that most people stumble on questions related to Thread Pooling.
“Nearly half (or maybe more) aren’t even aware that Java comes with a Thread Pool. Some of them don’t even know what a Thread Pool is. Most people who do know… are unable to explain why you would use a Thread Pool over simply starting threads on demand.
“This really points to a systemic problem among Java developers. They are too removed from the underlying hardware. Because the language isolates you from the OS doesn’t mean that you stop thinking about how the underlying systems will react to your software. This is especially true when the language gives you access to OS level resources (like Threads, or Files or memory).
“You can’t be starting and stopping thousands of threads, or having millions of files open or having terabytes of memory allocated. Java can’t create resources. It’s not magic.
13. How can we find the memory usage of JVM from Java code?
14. Can you catch an exception thrown by another thread in Java?
15. Does this code compile?
Application programmer Martin Simons submitted this Java interview question to Quora.
“Does this code compile? If so, when executed, what is the output? Just look at it. Don’t compile it, place it in IDE, or run it—figure it out yourself. The answer is obvious, if not immediately apparent.”
16. How can you check if a String is a number by using regular expression?
17. What is the difference between abstract class and interface.
Ankit Jain writes on Quora that most people will stop after saying, correctly, that abstract class can have zero or more implemented methods, and interface cannot have any implemented methods.
However, he adds: “Now think… is this the only difference? …Do not stop here. Keep answering by listing out other differences as well. [For example] one of them could be, members of interface cannot have any access level other than public. [Another is that] variables in interface are not actually variables; they are consants, because they are public static final by default.”
18. Is an empty .java file name a valid source file name?
Yes it is, writes Java developer Suresh Shiyani. You save your java file by .java only, compile it by javac .java and run by java yourclassname.
Here are Suresh’s examples
1. //save by .java only 2. class A{ 3. public static void main(String args[]){ 4. System.out.println("Hello java"); 5. } 6. } 7. //compile by javac .java 8. //run by java A
Compile it by javac .java
Run it by java A
Compile & run successfully.
Output: Hello java
19. Tricky output questions
Rohit Jain submitted one of the tricky output-related Java interview questions to Quora. “Many would say that it’s NullPointerException,” Rohit writes. “But it’s not. It will print ‘hello’.”
20. Beware of over-complicating things
Mathematics student Emin Kura encourages candidates not to be too worried about ‘trick’ questions and to focus on brushing up on the core fundamentals of Java, as ‘trick’ coding, which is not immediately understandable, is not good programming. Instead, he provides an example of one that initially looks tricky but is actually not, and is an opportunity for a candidate with strong knowledge of Java to give an authoritative answer.
Emin writes that most Java programmers are not able to answer it correctly, because they focus on looking for the mistakes.
“The code supposedly starts a thread which is counting on a long variable, then after 1 second, another thread will set the counting thread’s loop condition to false. The correct behaviour should be the thread printing the value to output.
“However, it loops forever. The counting thread doesn’t see the value of the stop variable’s new assignment… How can it be fixed ?
“The solution: stop class variable should be declared as volatile.”
Emin writes that this small sample of code shows clearly the concurrency in Java that’s “so hard to get it right, and so easy to get it wrong.” This particular coding is related to JVM’s caching mechanism. He adds that if a Java programmer does not write code with these things in mind, it leaves the software vulnerable to bugs that can almost never be caught or tested and “your program will be always broken and cause problems”.