Thursday, January 23, 2014

Java Interview questions - 1 - Java Introduction

Testing your skill is the best way to learn...

Well, I received requests to write up something with Java Interview questions. Here, I am trying to accumulate many basic questions, that people face in Java interviews. (I personally faced many of them). If you are trying to get a job or trying to switch organization, you may get a little help.
I am not sure, how this will help you out but I am trying to share what I have gained facing different interviews.

One very much odd question I faced, I would like to share.
Why Java and nothing else ?
My straightforward answer was, I joined Cognizant as fresher and the learning executives chose me the path of Java. I had nothing much to do there.

Counter question I was expecting on this, after training, you could have moved to any other technology. Why did not you ?
Interviewer could read my mind and certainly asked me the same question.

I was a bit puzzled at first but I answered it with some points,

  • Java is everywhere, micro, mini, maxi, server, web, distributed, PaaS whatever the platform you name Java is there
  • Java is simple - easy to learn
  • Java is Architecture independent - Write Once Run Anywhere
  • Java is robust - java is reliable
  • Java is secure
  • Java is Object Oriented
  • Java supports multi-threading 
  • Memory management is not a headache while working in Java
  • Java is free
Some other answers can be there to this question. I have an article here detailing the answer to this question. You can check out.

As soon as I mentioned this points, I got caught at Object Oriented Programming.
After answering the previous question, I was feeling a bit tired, I simply answered A PIE is OOP.
Interviewer didn't take much time on this. But its worth reading about A PIE. This article describes this in detail. Its 5 minute job, you can check this one.

Well, then the expected question I had to face,
You mentioned about multi-threading. What is this ?
I don't know if this one was perfect but my answer was as follows,
Thread is a block of code which runs on different stage of execution in several processor simultaneously.

Any interviewer will get you caught at this answer, be well prepared with this type of answer. I got caught at different stage of execution.
What do you mean by Different stage of execution ?
When you are trying to put multi-threading in work, you must have the knowledge of multi-processing. Processors these days are able to switch contexts within different tasks thrown to it.
Suppose, we have two different code execution, where one is taking the input from database, another one tries to achieve an algorithm. Taking input from database can be sometimes time consuming. If we are executing the program in a single thread model, we have to wait until the process of taking input completes before we can start to solve the algorithm.
The catch is, we can work on the algorithm while we are waiting for the input to be processed. Basically the processing unit is not idle waiting for some response. It can work on something else while it can wait for something to happen.
Basically we are providing different stages for different set of executions independent of each other.
Java creates stacks for different threads to provide their own stage of execution.
A diagram is inevitable here,
Utilisation of time using Multithreading
Well, it was a bit unexpected for me to get a question as follows,
Who does this threading and how does it do that ?
With only 3 years of Java knowledge, this was tough for me to answer, so I basically blamed credited JVM for that.

The area, any interviewer would like to touch is JVM, the core of Java ecosystem. So he was relaxed to hear the term 'JVM'. 
The question may go in any direction from here. Depends on how you are driving the traffic. So, be cautious when answering this question. Only mention the areas you are confident about.
Suggestion - keep it short and crisp.

What is JVM ?
JVM is the core of Java ecosystem. JVM is the execution engine of anything written in Java. JVM is a process which sits on top of the Operating System. We can see JVM as a responsible guardian of the program we write. JVM fulfills all necessity of any class. Our programs breath and work in the environment provided by JVM.

Well, you are a fan of JVM. What JVM does for your program ?
JVM does everything that is far away from my knowledge.

  • allocates memory for my variables
  • manages the memory it allocates
  • provides operating system resources as and when needed
  • provides security for my program
  • provides an interface with OS to protect my program to be rejected by OS itself
  • communicates with different other programs to fulfill my program's need
There are many more tasks JVM does out of my knowledge.

With this said, I had to face another question related to JVM.
What is the difference between JVM and JRE ?

I have already provided a deeper analysis on this topic here. You can check out.

With this, the part on Java basics and architecture got completed. Next part was on OOP. Which I will discuss in later articles.

Keep watching this space for more.

Well, if it helped you to get some basics of how a Java interview looks like, request you to share the knowledge with your friends as well. It takes only a second to do so.

Happy preparing for interview.

Palash Kanti Kundu