Skip to main content

Commonly asked Top 26 Java Interview Questions


1.what is a transient variable?

A transient variable is a variable that may not be serialized. If you don't want to serialize any objects then we have to use Transient Keyword.


2. Why do threads block on I/O?

Threads block on i/o (that enters the waiting state) so that other threads may execute while the i/o Operation is performed. So your blocked thread of execution blocks only that: the thread of execution.


3. How are Observer and Observable used?

Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects(refer 1.4).


4. What is synchronization and why is it important?

                     With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors.


5. Can a lock be acquired on a class?

           Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.


6. What's new with the stop(), suspend() and resume() methods in JDK 1.2?

                   The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.


7. Is null a keyword?

                     The null value is not a keyword.


8. What state does a thread enter when it terminates its processing?

            When a thread terminates its processing, it enters the dead state.


9. What is the Collections API?

                 The Collections API(Application programming interface)(set of packages and classes) is a set of classes and interfaces that support operations on collections of objects.


10. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?

            The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.


11. What is the List interface?

               The List interface provides support for ordered collections of objects.


12. How does Java handle integer overflows and underflows?

                      It uses those low order bytes of the result that can fit into the size of the type allowed by the operation. If it overflows, it goes back to the minimum value and continues from there. If it underflows, it goes back to the maximum value and continues from there.


13. What is the Vector class?

               The Vector class provides the capability to implement a growable array of objects. The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. (refer vector1.2) .


14. What modifiers may be used with an inner class that is a member of an outer class?

               A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.


15. What is an Iterator interface?

                 The Iterator interface is used to step through the elements of a Collection.


16. What is the difference between the >> and >>> operators?

                    The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.


17.What is the difference between yielding and sleeping?

                When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state. Sleep() it will wait for the particular time and it will executes.


18. What are wrapped classes?

            A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. Wrapped classes are classes that allow primitive types to be accessed as objects.


19. 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 is possible 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. In java gargabage collector is enabled in default we no need to worry about it.


20. Can an object's finalize() method be invoked while it is reachable?

                 An object's finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object's finalize() method may be invoked by other objects.


21. What is the difference between preemptive scheduling and time slicing?

                Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors. A task executes for a predefined slice of time and then reenters the pool of ready tasks.


22. What is the catch or declare rule for method declarations?

               The checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause. Catch must be followed after a try method. Catch method is used to catch the exception.


23. What is a task's priority and how is it used in scheduling?
        

               A task's priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.



24. Can an anonymous class be declared as implementing an interface and extending a class?

             An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.


25. What is the purpose of finalization?

               The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected. It is used to clean up unwanted memory space.


26. What do you meant by encapsulation?

            The Wrapping up of data and functions into a single unit is known as       Encapsulation.
 Encapsulation is the term given to the process of hiding the implementation details of the object. Once an object is encapsulated, its implementation details are not immediately accessible any more. Instead, they are packaged and are only indirectly accessed via the reference of the object. It can't be accessible from the outside of the classes.





Comments

Popular posts from this blog

All difference Question in java(common difference) (Collections)

1)Difference Between Interfaces And Abstract class?All the methods declared in the Interface are Abstract,where as abstract class must have atleast one abstract method and others may be concrete.In abstract class keyword abstract must be used for method, where as in Interface we need not use the keyword for methods.Abstract class must have Sub class, where as Interface can’t have sub classes. An abstract class can extend only one class, where as an Interface can extend more than one. 2)Difference between Arrays and Vectors?Because Vectors are dynamically-allocated, they offer a relatively memory-efficient way of handling lists whose size could change drastically. Arrays are fixed in size and it's wastage of memory problem.A Vector might be a good choice for such an arrangement. Moreover, Vectors have some useful member functions that make coding simpler. While in array is the bad choice of the arrangement and the code become more complex.Vector is the double the size of the memory…

Top java programs for fresher

1) Write a program to find a repeated word?
package com.repeat;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Set;
public class RepeatedWords {
public static void main(String[] args)  { //Creating wordCountMap which holds words as keys and their occurrences as values         HashMap<String, Integer> wordCountMap = new HashMap<String, Integer>();         BufferedReader reader = null;         try         {             //Creating BufferedReader object             reader = new BufferedReader(new FileReader("C:\\sample.txt"));             //Reading the first line into currentLine             String currentLine = reader.readLine();             while (currentLine != null)             {                     //splitting the currentLine into words                 String[] words = currentLine.toLowerCase().split(" ");                 //Iterating each word                 for (String wo…