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
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held November 11-13, 2018, in New York City. Digital Transformation (DX) is a major focus with the introduction of DXWorldEXPO within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term.
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
Enterprise architects are increasingly adopting multi-cloud strategies as they seek to utilize existing data center assets, leverage the advantages of cloud computing and avoid cloud vendor lock-in. This requires a globally aware traffic management strategy that can monitor infrastructure health across data centers and end-user experience globally, while responding to control changes and system specification at the speed of today’s DevOps teams. In his session at 20th Cloud Expo, Josh Gray, Chie...
Disruption, Innovation, Artificial Intelligence and Machine Learning, Leadership and Management hear these words all day every day... lofty goals but how do we make it real? Add to that, that simply put, people don't like change. But what if we could implement and utilize these enterprise tools in a fast and "Non-Disruptive" way, enabling us to glean insights about our business, identify and reduce exposure, risk and liability, and secure business continuity?
The deluge of IoT sensor data collected from connected devices and the powerful AI required to make that data actionable are giving rise to a hybrid ecosystem in which cloud, on-prem and edge processes become interweaved. Attendees will learn how emerging composable infrastructure solutions deliver the adaptive architecture needed to manage this new data reality. Machine learning algorithms can better anticipate data storms and automate resources to support surges, including fully scalable GPU-c...
In this Women in Technology Power Panel at 15th Cloud Expo, moderated by Anne Plese, Senior Consultant, Cloud Product Marketing at Verizon Enterprise, Esmeralda Swartz, CMO at MetraTech; Evelyn de Souza, Data Privacy and Compliance Strategy Leader at Cisco Systems; Seema Jethani, Director of Product Management at Basho Technologies; Victoria Livschitz, CEO of Qubell Inc.; Anne Hungate, Senior Director of Software Quality at DIRECTV, discussed what path they took to find their spot within the tec...
Enterprises are striving to become digital businesses for differentiated innovation and customer-centricity. Traditionally, they focused on digitizing processes and paper workflow. To be a disruptor and compete against new players, they need to gain insight into business data and innovate at scale. Cloud and cognitive technologies can help them leverage hidden data in SAP/ERP systems to fuel their businesses to accelerate digital transformation success.
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
DXWorldEXPO LLC announced today that Telecom Reseller has been named "Media Sponsor" of CloudEXPO | DXWorldEXPO 2018 New York, which will take place on November 11-13, 2018 in New York City, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
"We host and fully manage cloud data services, whether we store, the data, move the data, or run analytics on the data," stated Kamal Shannak, Senior Development Manager, Cloud Data Services, IBM, in this SYS-CON.tv interview at 18th Cloud Expo, held June 7-9, 2016, at the Javits Center in New York City, NY.
Containers and Kubernetes allow for code portability across on-premise VMs, bare metal, or multiple cloud provider environments. Yet, despite this portability promise, developers may include configuration and application definitions that constrain or even eliminate application portability. In this session we'll describe best practices for "configuration as code" in a Kubernetes environment. We will demonstrate how a properly constructed containerized app can be deployed to both Amazon and Azure ...
"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.
"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.
In this presentation, you will learn first hand what works and what doesn't while architecting and deploying OpenStack. Some of the topics will include:- best practices for creating repeatable deployments of OpenStack- multi-site considerations- how to customize OpenStack to integrate with your existing systems and security best practices.
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.