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 com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. 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:

c:\>java com.xyz.hr.Employee

* 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 (http://yakovfain.com) 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
rajasekhar 04/30/06 11:56:34 PM EDT

I am a 2+ years experience candiate in java please give me suggestions of how to prepare for the interview

AB Iyer 04/29/06 03:25:15 PM EDT

Please send me some java /j 2ee questions

Jitendra Pant 04/27/06 04:21:43 PM EDT

Give me interview Questions on Core Java, JSP,JDBC that usually asked to 1-2 years exp. people.Very urgent.

aravind 04/21/06 12:39:21 AM EDT

hi i m looking for the job.i needed question regarding corejava and j2ee related topics.plz help me.
thanks

Md. Farid Khan 04/17/06 08:36:38 AM EDT

I am currently working on JAVA/J2EE struts.
so i desparately need questions from the above topics.
Plz help

jhgeer 04/10/06 03:11:45 AM EDT

fsdfsdf

Chakrapani Panda 03/31/06 03:08:46 AM EST

Please give me some core java and swing interview question.

amit r ambre 03/16/06 04:23:33 AM EST

amit

amit r ambre 03/16/06 04:23:29 AM EST

amit

amit r ambre 03/16/06 04:14:43 AM EST

Amit

amit r ambre 03/16/06 04:14:41 AM EST

Amit

Padmanabhan G 02/18/06 01:07:45 PM EST

I need the type of questions asked in java/j2ee in interview for Freshers

Nitin 02/09/06 12:49:52 AM EST

Thanx Jack

jack 02/07/06 09:27:13 PM EST

You can get good java interview questions for freshers in the following link.

http://www.geocities.com/srcsinc/interview_questions/java_interview_ques...

Nitin Tyagi 02/07/06 02:51:51 AM EST

Dear friends,
I need the type of questions asked in Core java/j2ee in interview for 1+ year experience.
please send me to my email-id

sravya 02/06/06 02:36:03 AM EST

Dear frnds,
I am applying for jobs with 2+ experience.I Want to know how the interviewer may ask the questions related to real time experience..Can anyone suggest few questions which can show that how deapth they may question on Servlets/JSP/Struts/RMI and EJB

Mukesh 02/02/06 01:36:07 AM EST

hi,
i in need of inteview questions of corejava,servlets,jdbc,jsp

Sujit Singh(NIIT-Malleswaram) 01/08/06 03:02:29 PM EST

Dear friends,
I need the type of questions asked in java/j2ee in interview for 1+ year experience.
please send me to my email-id

suresh 01/06/06 10:15:33 AM EST

respected sir,
i am in need of interview questionpapers for corejava,servlets,jsp,jdbc.please send me to my email-id

suresh 01/06/06 10:06:08 AM EST

hi,
i in need of inteview questions of corejava,servlets,jdbc,jsp

sujit Singh 01/01/06 12:15:28 PM EST

Hi,i want the 1 to 5 experience java questions

Arun Kumar 12/29/05 12:16:29 AM EST

Hi, I need some sample questions Regarding Jcp Exam

karthik 12/19/05 05:20:21 AM EST

I need some basic core java,servlets,jsp and structs question,kindly help me.

akash 12/17/05 08:52:52 AM EST

I need Core JAVA and J2ee Interview Questions.

monalisa sahoo 12/09/05 11:09:09 AM EST

i want the 1 to 5 experience java questions.

Yakov Fain 11/04/05 04:43:40 PM EST

Dear friends,

These are all questions I was planning to publish at this point. Unfortunately, most of the feedback I've received so far started with the words "Give me", "I want", "I had required"... I understand that English is not your first language (so as mine), but such requests just sound rude.
Anyway, if you'd like to provide some feedback, be my guest, but it has to be a constructive one. I'm planning to publish another set of non-core Java questions soon. When? I do not know, whenever I'll have a bit more time.

All the best,
Yakov

sudhanu sekhar swain 11/04/05 01:48:34 PM EST

I had required a servlet/core java/jsp/ejb/struts question. if possible please send to my id.

sudhansu sekhar swain 11/04/05 01:44:33 PM EST

hi , i am sekhar working on java tech since 2004 .

Hemantha Kumar 11/04/05 02:38:14 AM EST

I had required a servlet/core java/jsp question. if possible please send to my id.

rakesh kumar 10/28/05 05:24:06 AM EDT

i want some java questions from JCP exam

rakesh kumar 10/28/05 05:24:01 AM EDT

i want some java questions from JCP exam

liming 10/20/05 05:02:32 PM EDT

I wan to see the questions

Ashish Chiwhane 10/19/05 05:07:58 AM EDT

Hi,
I would appreciate if someone pls send me java,j2ee interviews que and ans.
Thanks in advance...

Sarbajit 10/14/05 04:02:35 AM EDT

Hi,
I want the corejava,jsp,servlet, awt ,swing interview questions.

Shankar 10/12/05 10:13:13 AM EDT

Hai i need the java interview questions, i want to prepare for myself for an interview so it may be useful.

kumaravelu 10/09/05 10:18:05 AM EDT

Give me interview Questions on Core Java, JSP,JDBC that usually asked to 1-2 years exp. people.Very urgent.

kumaravelu 10/09/05 10:18:01 AM EDT

Give me interview Questions on Core Java, JSP,JDBC that usually asked to 1-2 years exp. people.Very urgent.

madhav 09/27/05 11:46:11 AM EDT

i saw quiet a post in your website asking for the java technical interview questions and answers.Are you really sending them ? if yes, i would also like to get a copy of it. Thanks in advance

shruti kondreddi 09/27/05 08:41:56 AM EDT

thanks.

Akshay's Notebook 09/26/05 05:22:30 PM EDT

Trackback Added: Decent Java Interview Questions; So generally I'm against asking code specific questions during an interview. You can tend to weed out really good developers who just happen to be rusty in a particular area. This article over at JDJ (Secrets Of The Masters: Core Java Interview Quest...

sudhansu.swain 09/24/05 05:13:20 PM EDT

i want to java interview question

ketaketi 09/20/05 11:15:06 AM EDT

i saw quiet a post in your website asking for the java technical interview questions and answers.Are you really sending them ? if yes, i would also like to get a copy of it. Thanks in advance.

JDJ News Desk 09/17/05 09:24:23 AM EDT

Secrets Of The Masters: Core Java Interview Questions. 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. JDJ Enterprise Editor Yakov Fain here offers thirty of the core Java questions that you might expect during the interviews, 20 for mid-level developers and the final ten for senior-level developers. Why not try them and see how well you do?

JDJ News Desk 09/17/05 08:31:07 AM EDT

Secrets Of The Masters: Core Java Interview Questions. 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. JDJ Enterprise Editor Yakov Fain here offers thirty of the core Java questions that you might expect during the interviews, 20 for mid-level developers and the final ten for senior-level developers. Why not try them and see how well you do?

anwarbasha 09/13/05 03:54:14 PM EDT

plz send me java interview questions. i desparately in need of them

Ashish Chiwhane 09/08/05 05:47:56 AM EDT

Hi,
I want the corejava,jsp,servlet,ejb and struts interview questions.

kuldeep kumar 09/01/05 07:27:19 AM EDT

Hi,
I want the corejava,jsp,servlet,ejb and struts interview questions.

Hoshiar Singh 08/27/05 03:43:57 AM EDT

Hi
The questions for Senior Level were Interesting. Thanx

bandita 08/26/05 11:59:33 PM EDT

Hi,
I want the corejava,jsp,servlet,ejb and struts interview questions.

Sailaja 08/16/05 01:53:52 AM EDT

Too good questions. Waiting eagerly for the J2EE questions. It would be better to post some more questions and answers of Core Java.