Java Interview Questions and Answers
ANS: All Java programs are compiled into class files that contain byte codes. These byte codes can be run in any platform and hence java is said to be platform independent.
2. Explain working of Java Virtual Machine (JVM)?
ANS: JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (class is nothing but byte code file.) and Interpreter reads byte codes.
3. What is the Java API?
ANS: The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.
4. Why there are no global variables in Java?
ANS: Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:
The global variables breaks the referential transparency Global variables creates collisions in namespace.
5. What is local class in java?
ANS: In java, local classes can be defined in a block as in a method body or local block.
6. Is null a keyword in java?
ANS: The null value is not a keyword in java. true and false are also not keywords in java. They are reserved words in java language.
7. What is fail-fast in java?
ANS: A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators.
Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
8. What is java class path?
ANS: The class path is an environment variable. It is used to let the compiler know where the class files are available for import.
9. What is the purpose of garbage collection in Java, and when is it used?
ANS: The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
10. What is System. out in Java?
ANS: It is a static member variable in System class. This is called standard output stream, connected to console.
11. Explain different way of using thread?
ANS: The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, ’cause when you are going for multiple inheritance, the only interface can help.
12. What is an Iterator?
ANS: Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
13. What is static in java?
ANS: Static means one per class, not one for each object no matter how many instance of a class might exist. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final.
14. What is final?
ANS: A final class can’t be extended i.e. final class may not be subclassed. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant).
15. What is meant by pass by reference and pass by value in Java?
ANS: Pass by reference means, passing the address itself rather than passing the value. Pass by value means passing a copy of the value.
16. What are Java Access Specifiers?
ANS: Access specifiers are keywords that determine the type of access to the member of a class. These keywords are for allowing privileges to parts of a program such as functions and variables. These are:
• Public: accessible to all classes
• Protected: accessible to the classes within the same package and any subclasses.
• Private: accessible only to the class to which they belong
• Default: accessible to the class to which they belong and to subclasses within the same package
17. Name primitive Java types.
ANS: The 8 primitive types are byte, char, short, int, long, float, double, and boolean.
18. What is the use of final keyword?
ANS: The final keyword can be assigned to
* Class level variable
* class Objects
If a final is assigned to a variable, the variable behaves as a constant. It means that the value of variable once set cannot be changed.
19. What is use of synchronized keyword?
ANS: This keyword is used to prevent concurrency. Synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition
20. Describe what happens when an object is created in Java?
ANS: Several things happen in a particular order to ensure the object is constructed properly:
1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implementation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its uppercase. This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.
21. How can you achieve Multiple Inheritance in Java?
ANS: Java’s interface mechanism can be used to implement multiple inheritance, with one important difference from c++ way of doing MI: the inherited interfaces must be abstract.
22. What is the difference between StringBuffer and String class?
ANS: A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. The String class represents character strings.
23. What are java beans?
ANS: Java Beans is a portable, platform-independent component model written in the Java programming language, developed in collaboration with industry leaders. It enables developers to write reusable components once and run them anywhere — benefiting from the platform-independent power of Java technology.
24. What does the keyword “synchronize” mean in java. When do you use it? What are the disadvantages of synchronization?
ANS: Synchronize is used when you want to make your methods thread safe. The disadvantage of synchronize is it will end up in slowing down the program. Also if not handled properly it will end up in dead lock.