Monday, September 17, 2012

Java Interview Questions - Class/Object


Can we create an Empty java file?

Yes, it is valid to create an empty Java file. The compilation will be successful but no class file will be created. It’s just that the compiler won’t throw any error.
As there is no class file gets generated, we can’t even try running the empty file.


Can we create an Empty class?

Yes, we can create a class which don’t have any variables or methods. Once the file is compiled, we get a class file.


What happens when we run a java class without main method? 
We get a classNotFoundException.The JVM when tries to run an file, tries to find the public class present in the file and then tries to search for a valid main method in that particular class.
As, in this case, we don’t have any main method, JVM complains that it didn’t find any public class which has a valid main method.
The below will be the exception stack trace
Exception in thread "main" java.lang.NoClassDefFoundError:
Caused by: java.lang.ClassNotFoundException:
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class:.  Program will exit.



Can a file contain more than one class?
Yes, but only one public class is allowed in a file.
When a file is executed using the java command, it searches for a main method in the public class to start its execution. So, if the class have two public class, it won’t be able to figure out in which class it needs to search for the main method.
But it is perfectly possible to have more than one class in a file until that particular file has only one public class.
Screenshot showing two classes in a file:






Once this file is compiled, two class files will be generated, one for each

Compiler giving error when having more than one public class in a file:








How many class files are generated when you compile a java file having two classes?

Two class files. One for each class is generated.

Can the main method have any return type other than void?
The JVM searches for the public class in the file specified to run and then tries to find the main method which is static having String[] as arguments and whose return type is void.
If the return type is changed from void, an exception will be throws saying Main method is not found.
For example, compiling and running the below file:






Will result in the below exception when we try to execute.
Exception in thread "main" java.lang.NoSuchMethodError: main
But when another main method is also present which is the original one, then the compiler shows an error saying duplicate method.







Can we change the argument list of the main method?
Yes, but JVM won’t treat it as an original main method. See the above question for explanation.



Can we overload the main method?
Once the file is run using the java command, it goes to the public class in that file and searches for the main method having void as return type and which is static and which takes String[] as arguments. So, it does not bother to have another main method with different signature. The JVM calls only the original main method.
The other main method should be called manually in the code, the same way it applies for other normal methods. 






Can we have multiple methods with the name “main” in a single class?

Yes, But the method which is static and have the return type as void and having Sting[] as arguments only will be called by the JVM.
For example, the below is a valid class file which don’t give any compilation or Run time error.


The above program on execution, prints the below output:
From the Original main method




Can we have two main methods in our application in two different classes?
Yes, any application can have multiple classes having the main method. It is up to the user to choose which one he wants to execute.



What is a class?
A class is a blue print to create objects.

What is an object?
Object is an instance of the class.









Tuesday, August 28, 2012

Java Interview Questions - General



What are the features of Java language?

Platform Independence - Write Once, Run Anywhere.


Object Oriented - Everything in this Universe is an Object.Even in Java, everything can be a object, making the programmer life easier in making him/her to relate the programming to the real world.

Simple - All the complexeties that exists in C and C++ are elimated or repaired to make them easy.

Robust - More Stress is laid on Complier to detect errors.Mechanism involved in handling the exceptions make the application built in java minimal chances to crash.

Multi Threaded - Java is multi threaded where the task is shared among threads and making the program time efficient.

Security - Java Provides sevaral tools to help you manage access to resources on any system.

Rich API- Java has tons of libraries which are ready to use.

Please click here for the detailed explanation.

Why do you choose Java?


First reason that I chose Java is beacuse of the Object Oriented Programming.

By having a Object Oriented programming language, the programmer can relate the programming to the real world and it maked to break the complex problems to small chunks thus making the program more efficient.


Platform Independence - As a programmer, I don't like to build my source code once for each plat form.It would be good If I build it once and if the complied code runs anywhere on any machine in the universe provided they have the compatible JVM.

Secure - Java is Simple when compared to many other programming languages.

Rick API- Java has tons and tons of library classes where one can use them readymade.

Java is Vast - There are many open source technologies that are built on Java.Java is vast giving the programmer many options to design a application.There are many framerworks, patterns, open source technolgies,extension built on top of Java(Ex:Spring, Hibernate etc.)

What is Object Oriented Programming ?
Object Oriented programming starts by considering everything that we see as an object.The object oriented approach involves the below three steps.


1) Identify different type of Objects
2) Define the state and behavior of each object
3) Define the Object Interaction.

Please click here for the detailed explanation.