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
Despite being the market leader, we recognized the need to transform and reinvent our business at Dynatrace, before someone else disrupted the market. Over the course of three years, we changed everything - our technology, our culture and our brand image. In this session we'll discuss how we navigated through our own innovator's dilemma, and share takeaways from our experience that you can apply to your own organization.
Blockchain has numerous revolutionary ambitions, but technology hasn’t evolved enough to make them practical. What is the best use of blockchain technology today? How are asset owners and managers looking at blockchain to transform ownership structures? How will blockchain technology allow global investors to access new markets? What kinds of companies will take advantage of blockchain technology as a more efficient way to raise capital?
This session describes how Professional Services organisations can deliver within Technology-as-a-Service (IaaS) constructs, in private and public enterprise cloud scenarios. See how professional services can be packaged and funded by IaaS cash flows, based upon consumption of technology services. Learn how significant, IT infrastructure transformations can be funded through OPEX spending models with multi-year As-a-Services based contracts. Understand how the automation of repeatable services c...
Since their inception, Total Uptime has been delivering on their promise of unprecedented uptime to major brands as well as Fortune 500 corporations in the United States and around the world. Their Cloud network spans 17 countries and delivers the unique ability to seamlessly route Internet traffic from one site to the next when our customers need us to. It is built on the best SSAE 16 SOC 2 Type 2 and CSAE 3416 Datacenters and boasts an impressive line-up of Network and Transit Provider Redunda...
​Blockchain, also known as Distributed Ledger Technology (DLT), can be used for more than just crypto currencies. This presentation discusses how blockchain and the Internet of Things (IOT) can be used for several supply chain management and logistics use cases. Real world examples that utilize the open source project Hyperledger will be discussed.
Gym Solutions is a software as a service (SaaS) solution purpose-built to service the fitness industry, with over 1000 clients servicing over 2 million gym members across 40 countries making Perfect Gym Solutions one of the largest and fastest growing SaaS applications in the fitness industry. Their platform is a comprehensive package of modern modular products for the management of fitness clubs, sports facilities and gyms an end- to end solution, revolutionising the way that gyms are managed. ...
Enterprises are universally struggling to understand where the new tools and methodologies of DevOps fit into their organizations, and are universally making the same mistakes. These mistakes are not unavoidable, and in fact, avoiding them gifts an organization with sustained competitive advantage, just like it did for Japanese Manufacturing Post WWII.
Eric Taylor, a former hacker, reveals what he's learned about cybersecurity. Taylor's life as a hacker began when he was just 12 years old and playing video games at home. Russian hackers are notorious for their hacking skills, but one American says he hacked a Russian cyber gang at just 15 years old. The government eventually caught up with Taylor and he pleaded guilty to posting the personal information on the internet, among other charges. Eric Taylor, who went by the nickname Cosmo...
There's no doubt that blockchain technology is a powerful tool for the enterprise, but bringing it mainstream has not been without challenges. As VP of Technology at 8base, Andrei is working to make developing a blockchain application accessible to anyone. With better tools, entrepreneurs and developers can work together to quickly and effectively launch applications that integrate smart contracts and blockchain technology. This will ultimately accelerate blockchain adoption on a global scale.
As the fourth industrial revolution continues to march forward, key questions remain related to the protection of software, cloud, AI, and automation intellectual property. Recent developments in Supreme Court and lower court case law will be reviewed to explain the intricacies of what inventions are eligible for patent protection, how copyright law may be used to protect application programming interfaces (APIs), and the extent to which trademark and trade secret law may have expanded relev...
The Blockchain Benchmark asks and answers the questions many people want to know about the state of Blockchain: What are the biggest barriers? What was your motivation to get involved? When will it mainstream? Who are the true influencers? What are its top use cases? Who will win over the next 5 years? How will the future unfold? And 20+ other valuable questions.
Ivo Lukas is the Founder/CEO for 24Notion. 24Notion is the first integrated marketing/digital PR & lifestyle agency with special emphasis on giving back to the global communities. With a broad understanding the art of non- traditional marketing, new media, communications and social influence. 24Notion ranked #12 in Corporate Philanthropy nominated by Portland Business Journal Book of List.
SUSE is a German-based, multinational, open-source software company that develops and sells Linux products to business customers. Founded in 1992, it was the first company to market Linux for the enterprise. Founded in 1992, SUSE is the world’s first provider of an Enterprise Linux distribution. Today, thousands of businesses worldwide rely on SUSE for their mission-critical computing and IT management needs.
Provide an overview of the capabilities of Azure Stack allowing you or your customers to adopt truly consistent Hybrid Cloud capabilities to deliver greater productivity in your cloud world. Ultan Kinahan is on a member of the Global Black Belt team at Microsoft with a focus on Azure Stack Hybrid Cloud. Ultan has been in the Azure team since the beginning, Has held roles in Engineering, Sales and now consults with both small to medium size business and the worlds largest organizations on how to ...
SAP is the world leader in enterprise applications in terms of software and software-related service revenue. Based on market capitalization, we are the world's third largest independent software manufacturer. Harness the power of your data and accelerate trusted outcome-driven innovation by developing intelligent and live solutions for real-time decisions and actions on a single data copy. Support next-generation transactional and analytical processing with a broad set of advanced analytics - r...