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.
JVM,

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

Next
Palash Kanti Kundu

Wednesday, January 1, 2014

Java Interview Questions - 2 - OOP

If you are preparing for Java Interview, get yourself a concept of OOP...

Java is Object Oriented Programming language. Well, to be true Java is not purely Object Oriented.

WHY SO ?
Cause Java uses primitives as data type. A pure Object Oriented technology will have the den full of Objects scattered everywhere in memory.
Nothing else matters...

Well, what is an Object ?
Objects are memory representation of your program written in any of Object Oriented language. Or in other ways we can define Object as abstraction of processing. Object is the basic unit of any OOP technology.
If you want to refresh your Object funda refreshed, you can take a look here.

So, can we define OOP as a concept ?
Yes, we can perfectly define OOP as a concept, which many software platform follows nowadays.

Name some of the OOP technology.
C++, C#, Java, Ruby, Python, PHP, Scala and many more. Full list is available here.

What is the difference between Java and JavaScript ?
Java is Object Oriented language while JavaScript is Object Based technology.

Object Based ?
This is a special type of language group which is more like Object Oriented Programming but lacks the Inheritance feature.

What is Inheritance ?
Inheritance is not a simple on liner concept to state. Take some time to read this and find out the best you can deliver in front of interviewer. Keep things simple short and pointed.

From here, the discussion can bifurcate. So take some time before you answer the question.

What are the way, you can achieve Inheritance in Java ?
We have two ways available to use the powerful concept of Inheritance,

Java does not support multiple inheritance. Is it true ?
Absolutely true.

Why so ?
If multiple inheritance was supported, we were more likely to face Deadly Diamond of Death.

We get Diamond while working on Java ?
We get Diamond of Death...
The whole discussion is here. Take some time to read and understand the situation. You will answer this question pretty well.

Give me a real world situation where all concept of OOP is there.
This one I got after having a discussion on OOP. Although I will share how I dealt with this one, I suggest you to find your most comfortable object to prove it as an object.

Well, my answer was on top of a Cigarette.
Cigarette follows A PIE
I will discuss this point by point.
Abstraction: Cigarette is made of Tobacco, that we know. But how the tobacco is filled inside, what other contents are mixed with it, we really don't care. We know we have to smoke, to do that we light a Cigarette.

So, Ideally we can think of this as following,

 /**  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class Cigarette {  
      private Tobacco tobaccoContent;  
      private Other otherContent;  
      private Filter filter;  
      /**  
       * Light Cigarette with CigarLighter 
       */  
      public static void light(CigarLighter lighter) {  
           // Algorithm to light the cigarette  
      }  
      public void smoke() {  
           // Algorithm to smoke Cigarette  
      }  
 }  
We really now don't care of the private attributes and what Cigarette does with them. We just care for lighting it and smoking it.

Polymorphism: We all know what polymorphism means and how do you get that in your code. For Cigarettes, this was a bit tough to explain but I did it in an awkward way. Cause I did not find a better way to do it.
Polymorphism for both the classes and methods are shown below.

 /**  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class Cigarette {  
      private Tobacco tobaccoContent;  
      private Other otherContent;  
      private Filter filter;  
      /**  
       * Light Cigarette with CigarLighter  
       */  
      public static void light(CigarLighter lighter) {  
           // Algorithm to light the cigarette  
      }  
      /**  
       * Smoke implementation  
       */  
      public void smoke() {  
           // Algorithm to smoke Cigarette  
      }  
      /**  
       * Default filter implementation  
       *   
       * @return  
       */  
      public Filter getFilter() {  
           return this.filter;  
      }  
 }  
 class Flake extends Cigarette {  
 }  
 /**  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class GoldFlake extends Cigarette {  
      private TippedFilter tippedFilter;  
      /*  
       * (non-Javadoc)  
       *   
       * @see main.Cigarette#getFilter()  
       */  
      public Filter getFilter() {  
           super.getFilter();  
      }  
      /**  
       * Overloading performed  
       *   
       * @param size  
       * @return  
       */  
      public Filter getFilter(Size size) {  
           switch (size) {  
           case Size.Small:  
                return getFilter();  
           case Size.King:  
                return this.tippedFilter;  
           }  
      }  
 }  
 /**  
  * Test polymorphism on object  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class CigaretteTest {  
      public static void main(String args[]) {  
           Cigarette cigarette = new Flake();  
           Cigarette cigarette2 = new GoldFlake();
           /*
            *Power of Inheritance. It promotes code reusability
            */
           cigarette.smoke();
           cigarette2.smoke();
      }  
 }  

Inheritance: The power of Inheritance is also shown in the earlier example. Flake does not even define any method. But as it is child of Cigarette, it is by default having the behavior of Cigarette.

Encapsulation: This part is more conceptual. So, we can say, that everything is encapsulated in the wrapper of Cigarette. Lots of things goes under the hood, which we really don't need to take care of. When we call a Cigarette, we are getting everything in a single wrapper, we just use the require to get the particular part out of it without much bothering about what other things are there in the wrapper.

So, Abstraction and Encapsulation deals with information hiding. What is the difference ?
Abstraction purely deals with the interface of your class. This solves the problem in design side. On the other hand, Encapsulation deals with the internal implementation. So, in a nutshell, we can tell that, Encapsulation is the implementation of Abstraction.

That's all for now. Keep watching this space for more...

Prev Next
Palash Kanti Kundu

Java Interview Questions - 3 - Serialization

With Java comes the concept of Serialization, multi-threading, exception handling, Collections, annotation etc. So, it is always better to brush up your skills.
I am here going to touch up some of them.

What is serialization in Java ?
Serialization is the most basic form of data persistence and transfer.
What do you mean by data persistence ?
In its basic sense, data persistence refers to make an object available even after the execution of a program is complete and main memory is cleared.


Why do we need that ?
System can not be always available and even if it is although, memory has limitation to store data in it. Even if we agree on a larger main memory option, it is expensive.


Where do we store data if not in main memory ?
Preferably in secondary memory or disk.


Isn't disk access costly in terms of processing and time ?
Of course, it is. Disk read-write requires more processing than main memory processing but it is cost effective in a whole.


OK, so how about data transfer ?
Yeah, Serialization also helps in data migration in point to point communication over the network. The data can be read as in Input Stream from the network sockets and can be reformed to an object.

Is it always better to store or transfer the data ? What about banking security related information ?
No, it is not always a better choice to store or transfer data over network or in the disk.


Isn't it a flaw ?
No, never. Everything in this world has its own pros and cons. So does have Serialization as well. While storing or transferring an object is a choice, there can be scenarios where some part of it can freely be not stored or transferred. The choice depends on the developer to partially or fully store or transfer an object.


Moving forward, what about the usage of serialization?
RMI calls, distributed computing environment extensively use serialization to interact withing VMs.


How to use serialization in Java ?
The most simplest form of using serialization is to simply implement the java.lang.Serializable marker interface, additionally you can write your own readObject() and writeObject() to perform custom serialization or deserialization.


Deserialization, what is that ?
Deserialization is the reverse process of reconstructing the saved or transferred object back into main memory for further processing


If you are able to relate your knowledge with these articles, would you like to share them in your networks ?


Prev
Palash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti KunduPalash Kanti Kundu