Java Interview Questions And Answers

1.
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.
2.
Who developed Java and when?
A team of Sun Microsystems programming headed by James Gosling was developed Java and released in 1995.
3.
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
4.
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.
5.
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.
6.
What is javac?
javac is the Java Complier, which translates Java sourcecode to bytecode files.
7.
What is jdb?
jdb is the java debugger, which is used to find error in programs.
8.
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.
9.
Why java is called truly object oriented language?
Java is truly object oriented language, everything must be placed inside a class.
10.
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.
11.
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.
12.
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.
13.
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.
Javac  Demo.java

The above command creates a file called Demo.class, containing the bytecodes of the program.
14.
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.
15.
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.
16.
What is a constant?
Constants in java refer to fixed values that do not change during the execution of a program.
17.
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;

18.
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.
19.
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
20.
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.
21.
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.
22.
What is infinite loop?
If loop executes infinite times without any termination, it is called infinite loop.
23.
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.
24.
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.
25.
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.
26.
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).
27.
How to instantiating an object?
The new operator is used to instantiating an object of previously specified class.
28.
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.
29.
Define Methods Overloading?
If a class have multiple methods with the same name but with different parameters and behavior is called method overloading.
30.
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.
31.
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.
32.
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.
33.
What is Finalize Methods?
Finalize method is used to free the memory from non object resources.
34.
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.
35.
Describe different forms of inheritance.
These are the different forms of Inheritance -

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

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.
37.
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.
38.
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.

39.
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.
40.
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.
41.
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.

42.
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.
43.
What is the use of throw in Java?
'throw' keyword is used to throw an exception in java.
44.
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.
45.
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.

46.
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.
47.
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.
48.
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.

49.
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.
50.
What are constructors in Java?
A constructor is a member function that is executed automatically whenever an object is created.




Read more articles