Welcome!

Java Authors: Sematext Blog , Elizabeth White, Liz McMillan, Yeshim Deniz, Torben Andersen

Related Topics: SYS-CON MEDIA

SYS-CON MEDIA: 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 co-founder of two software companies: Farata Systems and SuranceBay. He authored several technical books and lots of articles on software development. Yakov is Java Champion (https://java-champions.java.net). He leads leads Princeton Java Users Group. Two of Yakov's books will go in print this year: "Enterprise Web Development" (O'Reilly) and "Java For Kids" (No Starch Press).

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
Raghu 07/09/08 10:38:32 AM EDT

A message to all,interview questions given here can be generic ONLY.Its not like for 2+ years,4+ years etc.It purely depends on the company and the role for which you are interviewed.Some of the interview questions i faced recently.I was able to clear all the theoretically related so they asked me by giving a scenario.

1. You need to read all the details from say.. www.yahoo.com/index.html, save it in a file in yor local PC. What API will you use in Java and what method??
2. You are saving a set of employee names and their salaries in a HashMap like that.. How will you sort them in descending order based on the salary?
This can be done by implementing the comparator interface.

If you need further information,drop a mail 2 [email protected]

Cheers,
Raghu

aruna 06/20/08 02:40:58 AM EDT

Hi,
Could you please send me the interview questions in java,jsp,struts,jdbc,corejava for 1+ yers of experience as software engineer.

Thanks you,

Ashvini 06/16/08 12:23:12 AM EDT

Hi,
Could you please send me the interview questions in java,jsp,struts,jdbc,corejava for 2+ yers of experience as software engineer.

Thanks you,
Ashvini. S

. 06/13/08 04:29:20 PM EDT

these questions are stupid

these are moronic questions that idiots that don't know how to interview asks

lets get to some real world questions

deepa 06/11/08 07:48:12 AM EDT

Please send me expert level questions on jdbc 4.0

sandeep 06/06/08 04:55:35 AM EDT

try http://www.java-interview.com for all the java related interview questions

shikha 05/30/08 04:05:33 AM EDT

please send me the interview questions in java,jsp,jdbc,corejava for 1+ yers of experience as a software engineer.
thanks you,

sreekanth 05/28/08 04:29:58 PM EDT

Thank you very much for such a good article.....as I'm in search of job this article would certainly be helpful for my preparation.

sandeep 04/20/08 04:13:09 AM EDT

please send me the interview questions in java,jsp,jdbc,corejava for 1+ yers of experience as a software engineer.
thanks you,
sandeep

siddharth 04/18/08 07:54:20 AM EDT

I have 1+ yrs experience of working in java.. please help me by sending the interview questions..

varun 04/05/08 06:38:16 AM EDT

Data Migration/Conversion: It can be used to convert sql server tables into mysql tables

rajyalakshmi.M 03/26/08 12:53:10 AM EDT

I need the type of questions asked in an interview for a job in java/j2ee foe 2+ years experience

Thanks

Ranjani 03/12/08 09:39:19 AM EDT

I need the type of questions asked in an interview for a job in java/j2ee with about 2 years experience.

Thanks

archana 03/07/08 04:51:00 AM EST

Hi,
Could you please send me the interview questions in java,jsp,struts,jdbc,corejava for 2+ yers of experience as software engineer.
Thanks you,
Archana.

sony 02/01/08 09:37:29 PM EST

I have acquired abt 4-5yrs of Java/J2EE exp and will be attending an interview in the States...Could you pls let me know on what are the tips that I need and pls help me with some questions that they will ask me?

srinivas 01/20/08 07:28:34 AM EST

plz send j2ee interview questions with 2+ exp

Mitesh 01/12/08 12:40:28 AM EST

I need to Prepare for Internship as Java Programmer

VANI SARIN (Recruiter at GE Healthcare) 01/03/08 05:03:29 PM EST

GE Healthcare is hiring Sr. J2EE Developer and Web Client Developers with Ajax and or googleweb toolkit.

Please visit www.gecareers.com and enter job #s 756156 and 756161

vani 01/03/08 12:58:02 PM EST

can u plz send me 2+yrs core,jsp,servlet,jddbc questions

nirupama reddy 11/14/07 02:05:53 AM EST

hi,plz send me 1+ yrs core,adv,servlets,jsps questions

Y.V.Rajesh 11/07/07 01:31:27 AM EST

what is the need of constructors and what is the difference between class variables and instance variables?

sankari 11/02/07 04:56:36 AM EDT

hi,plz send me the 2+yrs exp questions in java/j2ee

praveeb 10/16/07 11:15:51 AM EDT

Hi,send me the 2+years interview questions please

prashant 09/19/07 08:19:58 AM EDT

Hi,
please send me the interview questions in java,jsp,jdbc,corejava for 2+ yers of experience as a software engineer.
Regards
Prashant

Rahul 09/05/07 08:09:02 AM EDT

Hi,
please send me the interview questions in java,jsp,struts,jdbc,corejava for 2+ yers of experience as a software engineer.

Regards,
Rahul Deshmukh

Rhea 08/14/07 01:07:54 PM EDT

Hi, I have acquired abt 4-5yrs of Java/J2EE exp and will be attending an interview in the States...Could you pls let me know on what are the tips that I need and pls help me with some questions that they will ask me?

gayatri 08/14/07 03:30:11 AM EDT

Hi,
I have 2+ yrs exp on java ,j2ee
I want some exp interview questions
and project Expletion]
plz help me

Soumitra 08/01/07 10:32:55 AM EDT

need some middle-level/expert-level core Java/EJB/JSP/Servlets Questions and answers

Srinivasarao Pentakota 07/28/07 07:40:00 AM EDT

Hi,
I have 2+ yrs exp on java webtechnologies,i am completed M.Sc Applied Mathematics and i have applied for Software companies but no calls for interviews .what is the reason???/

surya 07/20/07 08:06:29 AM EDT

some of the answers are wrong given by the other person so plz. beware of this.Go for RND and make it sure whether the answer is right or wrong

Svaba 07/20/07 06:09:40 AM EDT

Questions are exilent. I wrote a technical test few days ago and I had few qestions from this list.

Infernoz 07/13/07 04:31:45 PM EDT

Q2 Answer has confused grammar, so maybe part wrong.

Q3 Answer is only half right.
synchronized blocks are not for saving time they are for locking a specified object (which maybe a class), so that one thread owns that object while executing inside the block, they can be less efficient and less safe that synchronized methods, you can even have nested synchronized blocks for different objects, but that can be an invitation for Thread deadlocks and maybe a sign of poor coding practice.

Q17 Answer is correct, for runtime Atul Gupta!

Q19 Answer .. and (from recent Sun advice) for the creation of active Swing and AWT objects like JFrame, JWindow, Frame, Window etc.

Dave the Free Credit Report Guy 07/12/07 08:54:50 AM EDT

Oh yes, it would be totally helpful to master these important Java basics before getting out there for those job interviews...Good job!

Atul Gupta 07/12/07 03:29:45 AM EDT

Hi,

The answer to Question 17 is wrong, it wont give in any compilation errors.

Durga Prasad 07/04/07 02:39:07 AM EDT

Hi,
please send me the interview questions in java,jsp,struts,jdbc,corejava for 2+ yers of experience as a software engineer.
Regards,
Durga Prasad A.

Rajalakshmi K 06/25/07 12:51:56 AM EDT

I need some middle-level/expert-level core Java/EJB/JSP/Servlets Questions and answers

Govindaraj.S. 06/18/07 11:08:27 PM EDT

Hai,
Please send me the java question papers in programs also for 1.5+ years of experience

Govindaraj.S. 06/18/07 11:06:11 PM EDT

Please send me java and j2ee question papers for 1.5+ years of experience

N.V.Balasubramaniam 05/24/07 04:14:18 AM EDT

Hi,
please send me interview questions in core java,jdbc,servlet,jsp,struts,teradata,unix,coldfusion,SQL which is generally asked for 2+ years experience.

Thanks in Advance for your Help.
With Regards,
Bala

Amit 02/22/07 02:22:48 AM EST

Hi,
please send me the interview questions in java,jsp,struts,jdbc,corejava for 2+ yers of experience as a software engineer.
thanks you,
Amit

Anand 12/27/06 01:13:11 PM EST

All the interview questions are answered here,
for each topic the complexity increases in ascending order, so as your experience grows you can get deeper in each topic.

http://www.java-interview.com

I just love this site

akumaran 11/02/06 02:15:07 AM EST

Very good questions and answers. Also would be nice to add a "How would you go about..." section. Sometimes the candidate does not have to know the exact answer (e.g. performance, memory profiling etc) but should know how to go about solving a given problem or a task. Some other useful Q&A covering wide range of topics relating to Java/J2EE can be found at http://www.lulu.com/java-success and also google for "Java Job interview questions" or "J2EE Job interview questions"

himanshu 09/19/06 05:35:55 AM EDT

Hi,
please send me the interview questions in java,jsp,struts,jdbc,corejava for 2+ yers of experience as a software engineer.
thanks you,
himanshu

sachin 09/17/06 02:36:59 AM EDT

Hi,
please send me the interview questions in hibernet and EJB for 3 yers of experience as a software engineer.

madhavi 09/16/06 01:37:04 AM EDT

Hi,
please send me the interview questions in java,jsp,struts,jdbc,corejava for 2+ yers of experience as asoftware engineer. Also i need satyam papers also.
thanks you,
madhvai.

prajjal nag 09/15/06 03:19:48 AM EDT

Please send some interview question for Core Java for 2+ yrs experience, preferably java 1.4 and java 1.5

ABDULLAHI ABUBAKAR 09/14/06 08:30:00 AM EDT

I want you to send me the questions because i will start to study, Certified Enterprise Developer and Solution Architect on Sun Java Platform in this year.
Thanks.

Peter 09/14/06 05:59:12 AM EDT

Hi, I need some interview questions for a Java/J2EE Developer role, it's a phone interview taking place within the next 2 hours. any immediate help will be deeply appreciated.

surya 09/09/06 12:01:25 PM EDT

Hi,
please send me interview questions in core java,jdbc,servlet,jsp,struts which is generally asked for 2+ years experience
and aslo ibm,satyam interview questions.

imran 09/07/06 09:56:10 AM EDT

asdgsda

@ThingsExpo Stories
The BPM world is going through some evolution or changes where traditional business process management solutions really have nowhere to go in terms of development of the road map. In this demo at 15th Cloud Expo, Kyle Hansen, Director of Professional Services at AgilePoint, shows AgilePoint’s unique approach to dealing with this market circumstance by developing a rapid application composition or development framework.

ARMONK, N.Y., Nov. 20, 2014 /PRNewswire/ --  IBM (NYSE: IBM) today announced that it is bringing a greater level of control, security and flexibility to cloud-based application development and delivery with a single-tenant version of Bluemix, IBM's platform-as-a-service. The new platform enables developers to build ap...

"BSQUARE is in the business of selling software solutions for smart connected devices. It's obvious that IoT has moved from being a technology to being a fundamental part of business, and in the last 18 months people have said let's figure out how to do it and let's put some focus on it, " explained Dave Wagstaff, VP & Chief Architect, at BSQUARE Corporation, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
The major cloud platforms defy a simple, side-by-side analysis. Each of the major IaaS public-cloud platforms offers their own unique strengths and functionality. Options for on-site private cloud are diverse as well, and must be designed and deployed while taking existing legacy architecture and infrastructure into account. Then the reality is that most enterprises are embarking on a hybrid cloud strategy and programs. In this Power Panel at 15th Cloud Expo (http://www.CloudComputingExpo.com), moderated by Ashar Baig, Research Director, Cloud, at Gigaom Research, Nate Gordon, Director of T...
The Internet of Things is not new. Historically, smart businesses have used its basic concept of leveraging data to drive better decision making and have capitalized on those insights to realize additional revenue opportunities. So, what has changed to make the Internet of Things one of the hottest topics in tech? In his session at @ThingsExpo, Chris Gray, Director, Embedded and Internet of Things, discussed the underlying factors that are driving the economics of intelligent systems. Discover how hardware commoditization, the ubiquitous nature of connectivity, and the emergence of Big Data a...
SYS-CON Events announced today that Windstream, a leading provider of advanced network and cloud communications, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Windstream (Nasdaq: WIN), a FORTUNE 500 and S&P 500 company, is a leading provider of advanced network communications, including cloud computing and managed services, to businesses nationwide. The company also offers broadband, phone and digital TV services to consumers primarily in rural areas.
“In the past year we've seen a lot of stabilization of WebRTC. You can now use it in production with a far greater degree of certainty. A lot of the real developments in the past year have been in things like the data channel, which will enable a whole new type of application," explained Peter Dunkley, Technical Director at Acision, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that IDenticard will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada. Our partner network encompasses some 300 of the world's leading systems integrators and security s...
DevOps Summit 2015 New York, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that it is now accepting Keynote Proposals. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce software that is obsolete at launch. DevOps may be disruptive, but it is essential.
"People are a lot more knowledgeable about APIs now. There are two types of people who work with APIs - IT people who want to use APIs for something internal and the product managers who want to do something outside APIs for people to connect to them," explained Roberto Medrano, Executive Vice President at SOA Software, in this SYS-CON.tv interview at Cloud Expo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
Nigeria has the largest economy in Africa, at more than US$500 billion, and ranks 23rd in the world. A recent re-evaluation of Nigeria's true economic size doubled the previous estimate, and brought it well ahead of South Africa, which is a member (unlike Nigeria) of the G20 club for political as well as economic reasons. Nigeria's economy can be said to be quite diverse from one point of view, but heavily dependent on oil and gas at the same time. Oil and natural gas account for about 15% of Nigera's overall economy, but traditionally represent more than 90% of the country's exports and as...
The Internet of Things is a misnomer. That implies that everything is on the Internet, and that simply should not be - especially for things that are blurring the line between medical devices that stimulate like a pacemaker and quantified self-sensors like a pedometer or pulse tracker. The mesh of things that we manage must be segmented into zones of trust for sensing data, transmitting data, receiving command and control administrative changes, and peer-to-peer mesh messaging. In his session at @ThingsExpo, Ryan Bagnulo, Solution Architect / Software Engineer at SOA Software, focused on desi...
"At our booth we are showing how to provide trust in the Internet of Things. Trust is where everything starts to become secure and trustworthy. Now with the scaling of the Internet of Things it becomes an interesting question – I've heard numbers from 200 billion devices next year up to a trillion in the next 10 to 15 years," explained Johannes Lintzen, Vice President of Sales at Utimaco, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
"For over 25 years we have been working with a lot of enterprise customers and we have seen how companies create applications. And now that we have moved to cloud computing, mobile, social and the Internet of Things, we see that the market needs a new way of creating applications," stated Jesse Shiah, CEO, President and Co-Founder of AgilePoint Inc., in this SYS-CON.tv interview at 15th Cloud Expo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
Today’s enterprise is being driven by disruptive competitive and human capital requirements to provide enterprise application access through not only desktops, but also mobile devices. To retrofit existing programs across all these devices using traditional programming methods is very costly and time consuming – often prohibitively so. In his session at @ThingsExpo, Jesse Shiah, CEO, President, and Co-Founder of AgilePoint Inc., discussed how you can create applications that run on all mobile devices as well as laptops and desktops using a visual drag-and-drop application – and eForms-buildi...
We certainly live in interesting technological times. And no more interesting than the current competing IoT standards for connectivity. Various standards bodies, approaches, and ecosystems are vying for mindshare and positioning for a competitive edge. It is clear that when the dust settles, we will have new protocols, evolved protocols, that will change the way we interact with devices and infrastructure. We will also have evolved web protocols, like HTTP/2, that will be changing the very core of our infrastructures. At the same time, we have old approaches made new again like micro-services...
Code Halos - aka "digital fingerprints" - are the key organizing principle to understand a) how dumb things become smart and b) how to monetize this dynamic. In his session at @ThingsExpo, Robert Brown, AVP, Center for the Future of Work at Cognizant Technology Solutions, outlined research, analysis and recommendations from his recently published book on this phenomena on the way leading edge organizations like GE and Disney are unlocking the Internet of Things opportunity and what steps your organization should be taking to position itself for the next platform of digital competition.
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that its Call for Papers is now open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
As the Internet of Things unfolds, mobile and wearable devices are blurring the line between physical and digital, integrating ever more closely with our interests, our routines, our daily lives. Contextual computing and smart, sensor-equipped spaces bring the potential to walk through a world that recognizes us and responds accordingly. We become continuous transmitters and receivers of data. In his session at @ThingsExpo, Andrew Bolwell, Director of Innovation for HP's Printing and Personal Systems Group, discussed how key attributes of mobile technology – touch input, sensors, social, and ...