20 of the trickiest Java Interview Questions for Developers in 2019

Last Updated: January 1, 2019

Treat your prep for a Java programming job interview the same as you would do for any other: it’s a platform to pitch your skills & personality to the company’s requirements. But don’t forget to brush up on the core fundamentals of your skillset. Check out these 20 tough Java interview questions, with some answers, to prepare you for what lies in store.

by AGENT Staff
java interview questions
Image Source: Pixabay

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.

But before we get into those, let’s get the interview fundamentals in order. Ensure your appearance is neat and tidy, and make sure you carry any documents, laptop, tablet or other essentials in a quality briefcasea tidy backpack, or nifty, modern  or messenger bag / satchel.

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 reviewedCoding Interview Ninja, and the popular Java Programming Interviews Exposed.

 

 

Image Source: Amazon

Check Price >

 

 

20 Brain-Teasing Java Interview Questions

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?

Question Source: knowledge powerhouse

 

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.”

QUESTION & ANSWER SOURCE: QUORA

 

3. Can you create an immutable object that contains a mutable object?

Question Source: KNOWLEDGE POWERHOUSE

 

4. How can you mark an array volatile in Java?

Question Source: KNOWLEDGE POWERHOUSE

 

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.”

QUESTION & ANSWER SOURCE: QUORA

 

6. Will this return 5*0.1 = 0.5? True or false?

Question Source: KNOWLEDGE POWERHOUSE

 

7. What is the right data type to represent Money (like Dollar/Pound) in Java?

Question Source: KNOWLEDGE POWERHOUSE

 

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.”

QUESTION & ANSWER SOURCE: QUORA

 

9. Is ++ operation thread-safe in Java?

Question Source: KNOWLEDGE POWERHOUSE

 

10. In Java, can we store a double value in a long variable without explicit casting?

Question Source: KNOWLEDGE POWERHOUSE

 

11. How can you do constructor chaining in Java?

Question Source: KNOWLEDGE POWERHOUSE

 

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.

Question & Answer Source: Quora

 

13. How can we find the memory usage of JVM from Java code?

Question Source: KNOWLEDGE POWERHOUSE

 

14. Can you catch an exception thrown by another thread in Java?

Question Source: KNOWLEDGE POWERHOUSE

 

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.”

java interview questions

Question & Answer Source: Quora

 

16. How can you check if a String is a number by using regular expression?

QUESTION SOURCE: KNOWLEDGE POWERHOUSE

 

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.”

Question & Answer Source: Quora

 

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

Question & Answer Source: Quora

 

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’.”

java interview questions

Question & Answer Source: Quora

 

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.

java interview questions

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”.

Question & Answer Source: Quora

 



9 thoughts on “20 of the trickiest Java Interview Questions for Developers in 2019

  1. Hello There,

    I love all the posts, I really enjoyed.
    I would like more information about this, because it is very nice., Thanks for sharing.
    I have made a small Java project in Eclipse which I use to run some JUnit tests given to me by my UNI team members.
    I am using an ant build script and Ivy to resolve my dependencies at the moment. What I want to do is create a simple swing GUI which will allow any team member to upload new JUnit test cases, update the libraries to the latest from our Ivy repository and then compile and run these tests. My ant build script can already resolve dependencies, compile code, execute JUnit tests etc.
    Thank you very much and will look for more postings from you.

    Obrigado,
    Jessy

  2. Interviews always create some tensed situation and to make you feel easy about them you have provided some nice and important java programming interview questions which will be very useful for people who are preparing for interviews. Going to save this for sure.

  3. Well, your blog is quite interesting and helpful. I just loved reading your blog. Thanks a lot for sharing the java interview question here, i have found your article very good and useful as i have an interview as java developer and was looking for some java questions to prepare for.

  4. Thanks for providing this information.
    Its really help me at the time of interview.
    Good article for those who are preapring for interview.
    Keep sharing.

  5. Thanks for the post..
    I have my java interview in few days, i searched many article but found only questions related to java no answer were mention, your article is realy helpful as i got answers to all my questions..
    Thanks for sharing this with all..

  6. Excellent Java Interview questions for developer, it help me to prepare my interview. Do you have these question in PDF format to download? keep up the great work.

Leave a Reply

Your email address will not be published. Required fields are marked *

* Checkbox GDPR is required

*

I agree