Welcome!

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

Yakov,

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.

Thanks,

Haseen

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

good

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

good

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

hi

sharad nangia 05/24/05 03:48:36 AM EDT

Hi,

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.

Cheers,

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
HyperConvergence came to market with the objective of being simple, flexible and to help drive down operating expenses. It reduced the footprint by bundling the compute/storage/network into one box. This brought a new set of challenges as the HyperConverged vendors are very focused on their own proprietary building blocks. If you want to scale in a certain way, let's say you identified a need for more storage and want to add a device that is not sold by the HyperConverged vendor, forget about it...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, 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.
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world.
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...
"We're focused on how to get some of the attributes that you would expect from an Amazon, Azure, Google, and doing that on-prem. We believe today that you can actually get those types of things done with certain architectures available in the market today," explained Steve Conner, VP of Sales at Cloudistics, 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.
Sanjeev Sharma Joins November 11-13, 2018 @DevOpsSummit at @CloudEXPO New York Faculty. Sanjeev Sharma is an internationally known DevOps and Cloud Transformation thought leader, technology executive, and author. Sanjeev's industry experience includes tenures as CTO, Technical Sales leader, and Cloud Architect leader. As an IBM Distinguished Engineer, Sanjeev is recognized at the highest levels of IBM's core of technical leaders.
As Cybric's Chief Technology Officer, Mike D. Kail is responsible for the strategic vision and technical direction of the platform. Prior to founding Cybric, Mike was Yahoo's CIO and SVP of Infrastructure, where he led the IT and Data Center functions for the company. He has more than 24 years of IT Operations experience with a focus on highly-scalable architectures.
JETRO showcased Japan Digital Transformation Pavilion at SYS-CON's 21st International Cloud Expo® at the Santa Clara Convention Center in Santa Clara, CA. The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get...
All organizations that did not originate this moment have a pre-existing culture as well as legacy technology and processes that can be more or less amenable to DevOps implementation. That organizational culture is influenced by the personalities and management styles of Executive Management, the wider culture in which the organization is situated, and the personalities of key team members at all levels of the organization. This culture and entrenched interests usually throw a wrench in the work...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...
DXWorldEXPO LLC announced today that Dez Blanchfield joined the faculty of CloudEXPO's "10-Year Anniversary Event" which will take place on November 11-13, 2018 in New York City. Dez is a strategic leader in business and digital transformation with 25 years of experience in the IT and telecommunications industries developing strategies and implementing business initiatives. He has a breadth of expertise spanning technologies such as cloud computing, big data and analytics, cognitive computing, m...
As DevOps methodologies expand their reach across the enterprise, organizations face the daunting challenge of adapting related cloud strategies to ensure optimal alignment, from managing complexity to ensuring proper governance. How can culture, automation, legacy apps and even budget be reexamined to enable this ongoing shift within the modern software factory?