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

Decode of the image file from hibernate and struts

Create a java file for the decoding the image Image.javapackage com.img.action;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;
import javax.imageio.ImageIO;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.img.dao.ConnectionDAO;import com.img.pojo.Film_TablePojo;import com.img.pojo.imagePojo;import com.opensymphony.xwork2.ActionSupport;
public class Show extends ActionSupport {
List<Object> values = new ArrayList<Object>(); public List<Object> getValues() {return values;}
public void setValues(List<Object> values) {

Commonly asked top 15 Jsp Interview Question and answers

1)what is Jsp?        JSP stands for Java Server Pages. It is a presentation layer technology independent of platform. They are like HTML pages but with Java code pieces embedded in them. They are saved with a .jsp extension. It is used for viewing the page to the user. Jsp is for an dynamic pages.2)Explain Implicit objects in JSP?objects are created from the web container. They are

             1)response              2)exception              3)application              4)request              5)config              6)out              8)page              9)pagecontex 3) Differentiate between response.sendRedirect(url) and <jsp:forward page = …> .?                response.sendRedirect(url): It will create a new request for each and everytime. It based on the http address.      <jsp:forward page ="extension.jsp"> : It will forward the jsp page from one to the another, but it should be a same application context.
4)what are the java Lifecycle?       jspinit();       jspserv…