Java interview questions - Basics

1) What is platform independency ? Is java a platform independent language.

Platform independency means, we can write and compile the code at one platform(e.g. Windows..) and that code can be executed on other platform(e.g. Linux). Yes Java is a platform independent language.

2) What makes Java platform independent ?

In general compiled code in the set of instructions the CPU requires to execute a program. In java compiled code is bytecode and it does not depend on machine or OS.

JVM and Class Libraries take care of machine and OS, that?s why Java binaries (bytecode) can be moved to a entirely different system without affecting it. This makes java platform independent.

3) What is JDK ?

JDK so called Java Development Kit contains tools and environment needed to develop a java program. It contains Java Compiler (javac.exe), Java application launcher(java.exe) and JRE (Java Runtime Environment) in it. JDK also contains many other supportive entities in it like, Appletviewer  , Package classes and Runtime libraries etc.

JDK = Java Compiler, Java Runtime Environment (JRE), JVM, Packages and Classes, Runtime Libraries, Appletviewer ? etc.

4) What is JVM ? Is JVM platform independent ?

JVM is a virtual machine that provides and machine implementation for java code to run on different platforms. JVM can be a number of different types based on different hardare and platforms.

A Java program is compiled to build a intermediate bytecode before converting it to machine code exactly. This is what JVM does, it takes bytecode and convert it to machine code as per required hardware and OS environment.

JVM is not platform independent, there are different JVM?s each depending on different platforms.

5) What is base class for all classes in Java ?

?Object? is the root class for all other classes in java. It resides in java.lang package and is implemented by every class in java by default.

6) Is Java a pure Object Oriented language or not ?

Java is not a pure object oriented language because it supports primitive data types e.g. char, int ? etc.

7) Is it compulsory to have a main() method in each Java Class ?

No, This is not compulsory to have a main() in each java classes.

8) What if a main method is declared private in Java ?

The program will compile properly, but at run time it will throw an exception saying ?main() method not public?.

9) What if a main() method is not declared static ?

The program will compile properly, but at run time it will throw an exception saying ?NoSuchMethodError?.

10) What if no string argument is provided to main() method in Java ?

The program will compile properly, but at run time it will throw an exception saying ?NoSuchMethodError?.

11) What if a Java Application have more than one main() method ?

No problem, When running application we mentioned the name of class we want to run. Compiler will look at that class?s main method and will skip other main() methods.

But a single class can not have more than one main() method.

12) What are properties of a main method in java ?

In java the execution of the program starts from the main() method, there are certain charactersticks of  a main method in Java these are :

1) In java main() method does not returns anything, hence returns void.
2) In java an array of String is passed as argument in main method().
3) In java main method is declared static, because main() method is to be called even before the instatiation of the class.
4) A main() method in java can be overloaded, we can have any number of main() method with different signature in our class.
5) A main() method can be declared ?final?, after this none of the sub class will be able to implement its own main method.

13) Is it possible to have more than one class in a single .java source file.

Yes. We can have any number of classes in a single source file but one of them can be declared as public.

14) Which package is imported by default in Java ?

Java imports ?java.lang? package by default in each java source file.

15) Can be declare a class as static in Java ?

No, We can not declare a top level class as static, but we can declare an inner class as static.

16) What is Garbage Collector in java? What is its use ?.

Garbage collector is an important feature of Java Programming. Garbage collector collects unused objects from memory so that the resources can be released.

When an object is out of reach within a program it is gone for Garbage Collector to be removed from the memory.