Java Interview Questions And Answers

What is Java?
Java is object oriented programming language developed by Sun Microsystems, USA in 1995. Java is developed to make the language simple, portable and highly reliable.
Who developed Java and when?
A team of Sun Microsystems programming headed by James Gosling was developed Java and released in 1995.
Define some features of Java.
These are the features of Java -
1. Object Oriented
2. Compiled and Interpreted
3. Platform – Independent and Portable
4. Distributed
5. Multithreaded and Interactive
6. Robust and Secure
Which process is used by Java to execute a program?
Java executes a program in two stage – complied and interpreted. In first stage, java compiler translates the source code into byte codes and in second stage, java interpreter generates machine code to execute the program by machine.
What is Java Development Kit (JDK)?
JDK contains a large number of development tools. These tools are used for developing and running java programs. The JDK contains Java Runtime Environment, Java Compiler, Java Interpreter, Java Archiver and other tools.
What is javac?
javac is the Java Complier, which translates Java sourcecode to bytecode files.
What is jdb?
jdb is the java debugger, which is used to find error in programs.
Why is Java known as platform–neutral language?
Java program is compatible in different operating systems, processors. It can be easily move and run from one computer system to another.
Why java is called truly object oriented language?
Java is truly object oriented language, everything must be placed inside a class.
What is main() method?
main() method is the starting point of the execution of a program. It indicates the interpreter to start the execution of a program. Every java application program must include a main method.
What is the execution statement in java?
The execution statement in Java is –
System.out.println(“Printing message”);

The println method is a member of the out object, which is static data member of System class. This method always append a new line character to the end of the string.
Define keywords with examples?
Keywords are the reserved words. We cannot use them as names for classes, methods , variables and so on.
Examples – import, static, class, double, finally.
What is the syntax of compiling a program?
Java compiler javac is used to compile the program. To compile a program write the source file name followed by javac on command promt.

The above command creates a file called Demo.class, containing the bytecodes of the program.
What is the syntax of running the program?
Java Interpreter is used to run the stand alone program. To run the program type the source file name followed by java keyword.
Java Demo
The interpreter looks for the main method in the program and starts execution from there.
What is JVM?
Java complier produces an intermediate code known as byte code for a machine that does not exist. This machine is called the Java Virtual Machine and it exists only inside the computer memory.
What is a constant?
Constants in java refer to fixed values that do not change during the execution of a program.
What is type casting?
If there is need to store a value of one type into a variable of another type. We must cast the value to be stored by preceding it with type name in parentheses.
type variable1 = (type) variable2;

What is Instanceof Operator?
The instanceof is an object reference operator and returns true, if the object on the left hand side is an instance of the class given on the right hand side. This operator allows us to determine whether the object belongs to a particular class or not.
What is branching?
When a program breaks the sequential flow and jumps to another part of the code, it is called branching. These are the decision making statements –
1.If statement
2.Switch statement
3.Conditional operator statement
What is conditional operator?
Conditional Operator is used for making two way decisions.
Conditional expression ? expression1 : expression2;
IF the conditional expression result is true then expression1 is evaluated otherwise expression2 is evaluated.
What is Switch Statement?
If there is need to match the value from many different cases and execute the statements within the matched case. Switch statement executes line by line and read all the cases. If the case value is matched with a value passed in the switch expression, then statement within the matched case is executed.
What is infinite loop?
If loop executes infinite times without any termination, it is called infinite loop.
What is while loop?
While loop is used for iteration. In each iteration, it evaluates the truth expression just like If statement. If the condition evaluates to true, then the statement inside the loop is executed and control goes to the next iteration.
What is for loop?
Loop is used to run a set of instruction at given number of times or until a particular condition is satisfied.
What is break and continue?
The break statement is placed inside the loop to terminate the iteration and start to execute the code immediately after the loop and the continue statement is also used inside a loop. When continue statement is executed, it stops the current iteration of the loop and continue with the next iteration of the loop.
What is polymorphism?
Polymorphism means multiple forms. This is the ability to take multiple forms. There are two types of polymorphism in Java - Compile Time (Static) and Run Time(Dynamic).
How to instantiating an object?
The new operator is used to instantiating an object of previously specified class.
What is constructors?
The constructor is declared inside a class like a function and have the same name as the class itself. It calls automatically while creating an object. It does not specify any return type.
Define Methods Overloading?
If a class have multiple methods with the same name but with different parameters and behavior is called method overloading.
What is Static Members?
Static methods and variables are not bound to any specific object, they are common to all the objects. These members are accessed by the class name.
Define Inheritance?
Inheritance is the process of driving a new class from an existing class. The existing class is called parent class and new class is called child class. The new class inherits all the properties and methods of the parent class.
Define Overriding Methods?
If the method in the child class has same name, same arguments as a method in parent class, it is called method overriding.
What is Finalize Methods?
Finalize method is used to free the memory from non object resources.
What is Abstract Methods?
The abstract class is declared with the keyword abstract. This abstract class cannot be instantiated, it only inherited. An abstract class has at least one abstract method, preceded with the abstract keyword.
Describe different forms of inheritance.
These are the different forms of Inheritance -

1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
How to defined methods?
Methods are declared inside a class. The general syntax of defining a method is -
type methodname(argument list) 

type is the datatype of value return by method, all the statements are written inside opening and closing braces and all arguments are separated by comma.
When should we declare a method final?
If there is need to prevent child classes from overriding the methods of parent class, we should declare those methods as final.
What is the difference between overloading and overriding?
Overloading is used to have same function name which behave differently depending upon parameters passed to them. Overriding function is used in inheritance when derived class function has to do some added or different tasks than the base class.

What is Garbage Collection?
Garbage Collection is a process of automatically manage the heap memory. It checks the used and usused objects in memory and gets rid the unused objects.
What is this keyword?
This keyword provides reference to the current object. It can be used to refer instance variable of current class, to invoke or initiate current class constructor and it can be passed as an argument in the method call.
What is the use of instanceof operator in Java?
The instanceof operator is used to check whether the object is an instance of the specified class or interface.

Define static methods and properties?
Static methods and properties are not bound to any specific object. It belongs to the class and commonly refer to all objects of the class. It gets memory only once at the time of class loading.
What is the use of throw in Java?
'throw' keyword is used to throw an exception in java.
What is the importance of finally block in Java?
The code which is important to execute whether exception is handled or not is written in finally block. The code written in finally block is always executed.
What is nested class?
A class which is declared inside another class is known as Nested class. The scope of the nested is bounded by the scope of its enclosing class.

Is multithreading supported in Java?
Yes, Java supports multithreading. In java, multiple threads can be executed simultaneously, so that we can perform multiple operations at same time.
What is JIT?
JIT stands for Just-In-Time. JIT is compiler that converts java bytecode into native machine code. It reduces the compilation time and improve the performance.
How can you make copy of a java object?
We can make copy of a java object by using clone() method. clone() method makes exact copy of an object.

Can we implement destructor in Java?
No, we cannot implement destructor in java, as java does not support destructor. Java supports garbage collection to free the memory space automatically.
What are constructors in Java?
A constructor is a member function that is executed automatically whenever an object is created.

Read more articles