JAVA Interview Questions & Answers

Note : Check out our Android App for 101 interview Questions

JAVA Interview Questions & Answers :

1. What is the difference between Abstract class and Interface?
A class implementing an interface must implement all of the methods defined in the interface while a class extending an abstract class need not implement any of the methods defined in the abstract class. Additionally a class extending an abstract class can implement an infinite number of it's own methods.

2. what is a transient variable?
A variable that won't be allowed for object serialisation. so that the state of the value will always be defaulted after the deserialisation.
For example a variable x's value is set to 9, it's default value is '0' say, when the object has been serialized having x's value 9, after deserialisation will be defaulted to '0' .

3. WHAT IS THE DIFFERENCE BETWEEN MULTI-THREADING AND MULTI-PROCESSING?

Multitasking is running several processes at a time. Multi threading runs several threads of a single process at a time.The priority levels of threads are from 1-10.Normal priority comes to 5. Multi threading is normal in any window environment otherwise the screen would stuck everytime you clicked on something until that execution was accomplished.

4. Does Java has pointer?.
Java do have pointers; but not similar to the one that C has. In Java Pointers are in the form of Objects/references. Also the pointers in Java are not explicit like C. probably JVM internally uses pointers. Every object is a reference to a location making it a pointer...these pointers cannot be directly manipulated.

Differences between the C & Java pointers:

1) A pointer is a variable that holds the memory address of data. In Java 
there are only references which do not hold any memory address but 
which are indices in a table and this table has then the real pointer to 
the data.

2) Pointer Arithmetic is not possible with Java pointers.

5. Can we make construtor STATIC?
You cannot make a constructor as STATIC because if you use any access modifier in constructor compiler interprets it to be a method and expects a return type this class will not compile until you specify a return type since compiler thinks it to be a normal method not a constructor.

6. what is the differance between equals() method and ==?
The operator checks to see if two objects are exactly the same object. Two strings may be different objects but have the same value.The .equals() method to compare strings for equality(content).


7. Is it true the JAVA is realy a plateform indepandent?
No java is not platform independent it looses platform-independeny in 3 cases 

1. while using threads
2. while using AWT components
3. while using native methods 

also one more question 
we know java looses platform independency while running as threads 
but we also knew that in java every application run as a thread then how can we consider java as a platform-independent language?

8. when does JIT plays its role. Does JIT come along with jdk? If so then what is role of nterpreter?
JIT - Just In Time compiler is embedded in the web browser like the internet explorer!

The interpreter for Java which executes the Byte Codes is the JVM - Java Virtual Machine.

JIT is also a part of the JVM and it compiles bytecodes into real-time code when needed as on demand. The entire Java program is not compiled into an executable code at once coz there need to performed various run-time checks. However JIT compiles code as and when needed during execution. To put it simply the JIT compiler is faster than the JVM.

JIT comes into play when you execute applications written in JavaScript.
9. Does a class inherit the constructors of its superclass ?
A super default constructor is automatically available to the subclass when the subclass object is created. but if you want to access super class parameterized constructors we must use super keyword.

10. Advantage of servlets over JSP ?
Both the technologies are server side technologies each of them has its own advantages.The basic advantages of Servlet over JSP are

1) The execution time required in the servlet is lesser than because jsp is derived from the servlet so when client makes a request first time to the specific jsp file the container will perfom three steps a)Translation b)Compilation c)construction where as in the servlet these steps are not required except construction.

2) A servlet can handle different dynamic pages where as the jsp can handle only on dynamic page.

11. can we use final keyword before constructor?
generally final keyword is used when we want not to override the class or not to over ride the subclass methods but Constructor have the methods or datamembers to activated when we instantiaed so final keyword should not use before constructor.... and also it does not take final k'word before constructor .

12. What is final class ?
A class which cannot be inherited by any other class is called a final class.

13. What is the difference between static variable and instance variable?
Static variable the field is allocated when the class is created. It belongs to the class and not any object of the class. It is class variable

Instace variable the field is allocated when the class is instanciated to the class is called instance variable or non-static variable

14. Does garbage collection guarantee that a program will not run out of memory?
Garbage collection does not guarantee that a program will not run out of memory. It ispossible for programs to use up memory resources faster than they are garbage collected.It is also possible for programs to create objects that are not subject to garbage collection.


15. What is the significance of null interface in Java?
we say this as an mark up interface we use this interface because it shouid be recognized by Java Virtual Machine in order to make the objects selected for network enable and serialized and select particular objects to be participated in Client/server Communication.
16. Why is the main() method declared public and static?
Main is declared public because JVM accesses this method.

it is declared static because it is instance independent.

17. How does multithreading take place on a computer with a single CPU .
The operating system's task scheduler allocates execution time to multiple tasks. Byquickly switching between executing tasks, it creates the impression that tasks executesequentially.

18. What is the Advantage of using sendRedirect than RequestDispatcher ?
sendRedirect used with response object where Requestdiapacther used with request object.

19. What are some alternatives to inheritance?
Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn't force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

20. What is the difference between Java and J2EE? Is J2EE advanced version of Java?
Java is a object-oriented programming language which is platform neutral. That is unlike C or C++ java programs, Java can be run on any operating system with its JVM.

J2EE is just a specification for server side programs. That is to support internet applications, distributed and uses component model. So that Enterprises use this server side technology in their distributed business.

Since J2EE is just a server side specification, anybody can implement the specification but again using Java.
Hence today we have Custom implemented J2EE servers from Oracle, SUN, IBM and so on..

21. What is the difference between Classpath and Import?
Difference b/w the Classpath and Import:

Classpath: Class path is a system variable. It is a collection of files in the directories.Normally classpath will be used by Java interpreter to exucute the class files(.class).

Import :Import is a keyword. It is used to getting the required files into the program without writing the hard code.
eg. import java.io.*;

22. Why Java is not 100 % pure OOPS?
A pure Object Orientad Language is which implements All the OOP concepts.
Java doesn't support Multiple Inheritance & Operator loading
thats why "Java is not 100% pure Object Language".

23. What is Anonymous class,Singleton Class and Assertions?
Anonymous: instantiating the class without object name.
Singleton: it can instantiated by only one time.

Assertions: to show warning to the user.