1. What is JVM?
Why is Java called the Platform Independent Programming Language? Ans: A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible, because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.
2. In Java Can you access non-static variable in static context?
Ans: A static variable in Java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded. If your code tries to access a non-static variable, without any instance, the compiler will complain, because those variables are not created yet and they are not associated with any instance.
3. How many data types does Java support? What is Autoboxing and Unboxing?
Ans: The eight primitive data types supported by the Java programming language are: 1.byte 2.short 3.int 4.long 5.float 6.double 7.boolean 8.char Autoboxing is the automatic conversion made by the Java compiler between the primitive types and their corresponding object wrapper classes. For example, the compiler converts an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this operation is called unboxing.
4. How you define pass by reference and pass by value in Java?
Ans: When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn’t affect the original value. When an object is passed by reference, this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.
5. Explain different ways of creating a thread in Java. Which one would you prefer and why?
Ans: There are three ways that can be used for a Thread to be created:
1. A class may extend the Thread class
2. A class may implement the Runnable interface.
3. An application can use the Executor framework, to create a thread pool. The Runnable interface is preferred, as it does not require an object to inherit the Thread class.
In case your application design requires multiple inheritance, only interfaces can help you. Also, the thread pool is very efficient and can be implemented and used very easily.
6. What is the difference between a synchronized method and a synchronized block?
Ans: In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in a method level (coarse-grained lock) or block level of code (fine-grained lock).
7. Define Java Priority Queue?
Ans: The PriorityQueue is an unbounded queue, based on a priority heap and its elements are ordered in their natural order. At the time of its creation, we can provide a Comparator that is responsible for ordering the elements of the PriorityQueue. A PriorityQueue doesn’t allow null values, those objects that don’t provide natural ordering or those objects that don’t have any comparator associated with them. Finally, the Java PriorityQueue is not thread-safe, and it requires O(log(n)) time for its enqueuing and dequeuing operations.
8. What is the purpose of finalization method?
Ans: The finalize method is called by the garbage collector, just before releasing the object’s memory. It is normally advised to release resources held by the object inside the finalize method.
9. Explain the difference between Exception and Error in java?
Ans: Exception and Error classes are both subclasses of the Throwable class. The Exception class is used for exceptional conditions that a user’s program should catch. The Error class defines exceptions that are not excepted to be caught by the user program.
10. Justify your answer that N threads can access N resources without deadlock?
Ans: A very simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to follow that ordering. Thus, if all threads lock and unlock the mutexes in the same order, no deadlocks can arise.
Thanks for reading and good luck on your interview! Check more Java Interview Questions & Answers on onlineinterviewquestions.com