Java Platform, Enterprise Edition

Java EE Journal

Subscribe to Java EE Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Java EE Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

J2EE Journal Authors: Stackify Blog, Sumith Kumar Puri, Javier Paniza, Yakov Fain, Ken Fogel

Related Topics: Java EE Journal, Java Developer Magazine

J2EE Journal: Article

Secrets Of The Masters: Core Java Job Interview Questions

JDJ Enterprise Editor Yakov Fain Offers 30 Core Java Questions You Might Expect During Job Interviews

JDJ's Enterprise Editor, Yakov Fain (pictured) writes: If you are planning to hit the job market,  you may need to refresh some of the Java basic terms and techniques to prepare yourself for a technical interview. Let me offer you some of the core Java questions that you might expect during the interviews. 

For  most questions  I’ve provided only  short  answers to encourage further research.  I have included only  questions for mid (*) and senior level (**) Java developers. These sample questions could also become handy for people who need to interview Java developers (see also the article "Interviewing Enterprise Java Developers").

Disclaimer. This article has been originally published three or four years ago, hundreds of thousands Java developers have read it, but I still use some of these questions while interviewing Java developers. Guess what? Every other Java developer doesn't now the answers to some of them.

30 Java Interview Questions

* Q1. How could Java classes direct program messages to the system console, but error messages, say to a file?

A. The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:

Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st);

* Q2. What's the difference between an interface and an abstract class?

A. An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

* Q3. Why would you use a synchronized block vs. synchronized method?

A. Synchronized blocks place locks for shorter periods than synchronized methods.

* Q4. Explain the usage of the keyword transient?

A. This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

* Q5. How can you force garbage collection?

A. You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

* Q6. How do you know if an explicit object casting is needed?

A. If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example:

Object a; Customer b; b = (Customer) a;

When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.

* Q7. What's the difference between the methods sleep() and wait()

A. The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

* Q8. Can you write a Java class that could be used both as an applet as well as an application?

A. Yes. Add a main() method to the applet.

* Q9. What's the difference between constructors and other methods?

A. Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

* Q10. Can you call one constructor from another if a class has multiple constructors

A. Yes. Use this() syntax.

* Q11. Explain the usage of Java packages.

A. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

* Q12. If a class is located in a package, what do you need to change in the OS environment to be able to use it?

A. You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package; and is located in the file c:\dev\com\xyz\hr\ In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:


* Q13. What's the difference between J2SDK 1.5 and J2SDK 5.0?

A.There's no difference, Sun Microsystems just re-branded this version.

* Q14. What would you use to compare two String variables - the operator == or the method equals()?

A. I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.

* Q15. Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.

* Q16. Can an inner class declared inside of a method access local variables of this method?

A. It's possible if these variables are final.

* Q17. What can go wrong if you replace && with & in the following code:

String a=null; if (a!=null && a.length()>10) {...}

A. A single ampersand here would lead to a NullPointerException.

* Q18. What's the main difference between a Vector and an ArrayList

A. Java Vector class is internally synchronized and ArrayList is not.

* Q19. When should the method invokeLater()be used?

A. This method is used to ensure that Swing components are updated through the event-dispatching thread.

* Q20. How can a subclass call a method or a constructor defined in a superclass?

A. Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.

More Stories By Yakov Fain

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs ( and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

Comments (176) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
sarbajit 08/15/05 09:22:57 AM EDT

can u give me various interviews questions of Swing

Bikas Chandra Rout 08/06/05 08:00:52 AM EDT

Give me interview Questions on Servlet, JSP,JDBC that usually asked to 1-2 years exp. people

chandu 08/04/05 01:50:34 AM EDT

hi i want some technical papers on core java

Haseen Haq 08/01/05 11:33:10 PM EDT


You have asked a question regarding the Object Pooling and you have shown it to be a performance gain. Considering the GC changes in last few JVMs and the other advancements in JVMs where objects are placed in generations, do you think it is a good idea to pool objects?

More objects you pool, more objects will use the tenured space and as a result more major collections will be required. Do you think now is the time we change our old habits of coding that includes object pooling etc?

Please suggest.



praful 07/30/05 01:18:53 AM EDT

Its nice

Trinath Mahala 07/29/05 05:08:20 AM EDT

I would like to get questions that are mostly asked in interview

Trinath Mahala 07/29/05 05:08:15 AM EDT

I would like to get questions that are mostly asked in interview

kalkish 07/27/05 05:26:34 PM EDT

Hi, I want to know corejava, jsp interview questions

S.BINDHU 07/27/05 12:20:16 AM EDT

Hi, i want core java, jsp, servlet interview questions..

Noor 07/22/05 10:11:56 AM EDT

Hi, I Need a interview questions for java

venkata phanikumar.g 07/16/05 12:44:57 AM EDT

hi i want to take core java interview questions

anirban basu 07/01/05 12:32:42 PM EDT

i am final year computer engg student and i want to specialize in java.

ram 07/01/05 06:43:46 AM EDT


Ivaylo Milev 06/30/05 11:28:48 AM EDT

These questions set the bar a bit too low. Please consider revising them. For junior java developers I would expect them to be able to handle questions from the Java Programmer Certification Exam. For senior developers I would ask more class / pattern design related questions. Just a thought.

kishor kona 06/22/05 11:21:37 AM EDT


Ken 06/17/05 07:45:44 PM EDT

Regarding question 14 on your interview questions. It is valid to compare with '==' when using strings to find if they are both the same string and object on the heap. There is a special place on the heap for strings and duplicate String's will not be created. I believe this is taking advantage of the fact that they are immutable or visa versa.

Nils 06/06/05 06:59:34 PM EDT

> Q25. If you're overriding the method
> equals() of an object, which other
> method you might also consider?
> A. hashCode()

Not just "consider". You MUST override hashCode().

> Q27. How would you make a copy of
> an entire Java object with its
> state?
> A. Have this class implement
> Cloneable interface and call its
> method clone().

clone() method has a bunch of problems. Better to use copy-constructors.

shyam 05/25/05 06:33:52 AM EDT


sharad nangia 05/24/05 03:48:36 AM EDT


Answers provided to the questions only provide insight at the upper level suited for junior level programmers. But for people with high experience, they have to be more specific.

Examples to prove my point :

1. Difference between sleep() and wait()
--> Apart from what is being said, one would be more concrete saying, sleep() method doesnt reqlinquish the lock on the object.

2. Difference between abstract and interface.
--> one could be more concrete saying that, when i want a functionality to be provided in a particular method and everyone use it, rather than user providing a implementation, i would go in for a abstract class.

Now these are just answers are at higher level explanations.

I would expect from FrontPage Feature to be more explicit and technical and give examples too.

gautham 05/20/05 01:58:07 AM EDT

The questions are ok. I think the answer's are too concise for some questions where there can be more digging be done. i feel more standard questions need to be introduced for senior developers.It would be good idea to have some questions on core java patters also.

nmvijay 05/19/05 05:47:43 AM EDT

On 6-May-2005,
The question was

if (a == null && a.length()>10).......

u should read the editor comment above mine.
it was fixed after my feedback.

'editor commented on the 6 May 2005'

It is now your turn to get fired!!!

Donald Trump 05/16/05 01:42:54 PM EDT

nmvijay is wrong about Q17 --
if (a!=null && a.length()>10)
The 'a!=null' short-circuits the test so the 'a.length()' is never evaluated. "You're Fired!"

Dan 05/12/05 09:10:49 AM EDT

I believe question 28 is not correct. Object pooling just for memory management is, in general, a mistake because it interferes with modern generational garbage collection. Pooling makes sense for resources that are expensive for other reasons, e.g. database connections.

Shiv P. Ojha 05/12/05 02:43:10 AM EDT

A good question set to judge basic java concept and practicle knowledge. It has very concise and clear answers as well.

There is a small change suggested.

Q3 - Synchronized Block is necessary when you want to take lock on the object other than "this".

Eagerly waiting for j2ee question set.


editor 05/06/05 03:38:57 PM EDT

Code now fixed!

nmvijay 05/06/05 09:25:49 AM EDT

Q17, is wrong
The current code itself gives the NullPointerException