Related Topics: SYS-CON MEDIA


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

Latest Stories
DX World EXPO, LLC, a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Conference Guru has been named “Media Sponsor” of the 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. A valuable conference experience generates new contacts, sales leads, potential strategic partners and potential investors; helps gather competitive intelligence and even provides inspiration for new products and services. Conference Guru works with conference organizers to pass great deals to gre...
DevOps is under attack because developers don’t want to mess with infrastructure. They will happily own their code into production, but want to use platforms instead of raw automation. That’s changing the landscape that we understand as DevOps with both architecture concepts (CloudNative) and process redefinition (SRE). Rob Hirschfeld’s recent work in Kubernetes operations has led to the conclusion that containers and related platforms have changed the way we should be thinking about DevOps and...
The Internet of Things will challenge the status quo of how IT and development organizations operate. Or will it? Certainly the fog layer of IoT requires special insights about data ontology, security and transactional integrity. But the developmental challenges are the same: People, Process and Platform. In his session at @ThingsExpo, Craig Sproule, CEO of Metavine, demonstrated how to move beyond today's coding paradigm and shared the must-have mindsets for removing complexity from the develop...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
The next XaaS is CICDaaS. Why? Because CICD saves developers a huge amount of time. CD is an especially great option for projects that require multiple and frequent contributions to be integrated. But… securing CICD best practices is an emerging, essential, yet little understood practice for DevOps teams and their Cloud Service Providers. The only way to get CICD to work in a highly secure environment takes collaboration, patience and persistence. Building CICD in the cloud requires rigorous ar...
Companies are harnessing data in ways we once associated with science fiction. Analysts have access to a plethora of visualization and reporting tools, but considering the vast amount of data businesses collect and limitations of CPUs, end users are forced to design their structures and systems with limitations. Until now. As the cloud toolkit to analyze data has evolved, GPUs have stepped in to massively parallel SQL, visualization and machine learning.
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
"ZeroStack is a startup in Silicon Valley. We're solving a very interesting problem around bringing public cloud convenience with private cloud control for enterprises and mid-size companies," explained Kamesh Pemmaraju, VP of Product Management at ZeroStack, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Enterprises are adopting Kubernetes to accelerate the development and the delivery of cloud-native applications. However, sharing a Kubernetes cluster between members of the same team can be challenging. And, sharing clusters across multiple teams is even harder. Kubernetes offers several constructs to help implement segmentation and isolation. However, these primitives can be complex to understand and apply. As a result, it’s becoming common for enterprises to end up with several clusters. Thi...