Home Programming Kids Programming Hardware & Software Hardware & Networking APP security Software Education Kids Study MCQS Download OTHERS Login

Java OOPS and Servlets Java Technical Interview Questions

Categories: Education

Java OOPS Interview Questions

 

Q.1. What is an association?

 

Ans. Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take the example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationships can be one to one, one to many, many to one and many to many.

 

Q.2. What do you mean by aggregation?

 

Ans. An aggregation is a specialized form of Association where all object has their own lifecycle but there is ownership and child object can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department teacher object will not destroy.

 

Q.3. What is composition in Java?

 

Ans. Composition is again a specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of a relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different houses if we delete the house room will automatically delete.

 

Q.4. What is a marker interface?

 

Ans. In Java, a marker interface is an interface that does not declare any methods or fields. Its sole purpose is to mark or tag a class as having a certain characteristic or capability. By implementing a marker interface, a class indicates that it possesses specific behavior or qualifies for a particular treatment.

Marker interfaces are purely a convention and serve as a form of metadata. They provide a way for developers or frameworks to identify classes that meet certain criteria without requiring any additional methods or fields.

 

Some common examples of marker interfaces in Java include:

(i) Serializable: The Serializable interface is a marker interface that indicates a class can be serialized, allowing objects of that class to be converted into a byte stream for storage or transmission.

(ii) Cloneable: The Cloneable interface is a marker interface that indicates a class can be cloned using the `clone()` method. Implementing this interface allows for creating a copy of an object.

(iii) Remote: The Remote interface is a marker interface used in Java’s Remote Method Invocation (RMI) framework. It marks an interface that can be invoked remotely, enabling the execution of methods across different JVMs.

 

Marker interfaces can be useful in various scenarios, such as:

(i) Providing hints to the Java runtime or frameworks about special handling or optimizations for certain classes.

(ii) Enabling conditional behavior based on the presence of a marker interface during runtime.

(iii) Supporting integration with external libraries or frameworks that recognize and operate on marker interfaces.

 

It’s important to note that with the introduction of annotations in Java, the use of marker interfaces has become less common. Annotations offer a more flexible and expressive way to attach metadata to classes and methods. However, marker interfaces still have their place in certain contexts and are part of the Java language’s design and heritage.

 

Q.5. What is object cloning in Java?

 

Ans. Object cloning in Java is the process of creating an exact copy of an object. It basically means the ability to create an object with a similar state as the original object. To achieve this, Java provides a method clone() to make use of this functionality. This method creates a new instance of the class of the current object and then initializes all its fields with the exact same contents of corresponding fields. To object clone(), the marker interface java.lang.Cloneable must be implemented to avoid any runtime exceptions. One thing you must note is Object clone() is a protected method, thus you need to override it.

 

Q.6. What is a copy constructor in Java?

 

Ans. Copy constructor is a member function that is used to initialize an object using another object of the same class. Though there is no need for copy constructor in Java since all objects are passed by reference. Moreover, Java does not even support automatic pass-by-value.

 

Servlets Java Technical Interview Questions

 

Q.7. What is a servlet?

 

Ans. (i) Java Servlet is server-side technologies to extend the capability of web servers by providing support for dynamic response and data persistence.

(ii) The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets.

(iii) All servlets must implement the javax.servlet.Servlet interface, which defines servlet lifecycle methods. When implementing a generic service, we can extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.

(iv) Most of the times, web applications are accessed using HTTP protocol and thats why we mostly extend HttpServlet class. Servlet API hierarchy is shown in below image.

 

Q.8. What is Request Dispatcher?

 

Ans. RequestDispatcher interface is used to forward the request to another resource that can be HTML, JSP or another servlet in same application. We can also use this to include the content of another resource to the response.

 

There are two methods defined in this interface:

  1. void forward()
  2. void include()

 

Q.9. What is the life-cycle of a servlet?

 

Ans. There are 5 stages in the lifecycle of a servlet:

(i) Servlet is loaded

(ii) Servlet is instantiated

(iii) Servlet is initialized

(iv) Service the request

(v) Servlet is destroyed

 

Q.10. How does cookies work in Servlets?

 

Ans. (i) Cookies are text data sent by server to the client and it gets saved at the client local machine.

(ii) Servlet API provides cookies support through javax.servlet.http.Cookie class that implements Serializable and Cloneable interfaces.

(iii) HttpServletRequest getCookies() method is provided to get the array of Cookies from request, since there is no point of adding Cookie to request, there are no methods to set or add cookie to request.

(iv) Similarly HttpServletResponse addCookie(Cookie c) method is provided to attach cookie in response header, there are no getter methods for cookie.

Top articles
Being a Cutting-edge Internet based Teacher Published at:- 5 Procedures for Developing Nearer to Your Most Difficult Understudies Published at:- A Standout Distance Learning MBA Published at:- Best Education University In UAE Published at:- MBBS Education Course in Russia Published at:- Tips for Get the Most Out of Your Education Published at:- Breaking Down Elon Musk's Education: From South Africa to Silicon Valley Published at:- The French Education System Explained: Structure and Curriculum Published at:- The Prestigious History of Education At Cambridge University Published at:- The Power of Business Analytics: Unleashing Openings Through Online Courses Published at:- Data Analysis Courses for Beginners: Unlocking the Power of Data Published at:- The Dynamic Landscape of the Education System in the USA Published at:- How Does the American Education System Work Published at:- Sustainable Development Projects: Empowering Class 10 Students for a Brighter Future Published at:- Exploring Synonyms for Learning and Development: Broadening Horizons and Cultivating Growth Published at:- Exploring Alternatives: A Quest for Enhancing Professional Growth Published at:- Exploring Alternatives: Another Term for Training and Development Published at:- Exploring the Role of a Code Artisan: Another Name for Software Engineer Published at:- Navigating Excellence: The Best Colleges in the United States Published at:- Exploring Excellence: The Best Medical Universities in America Published at:- The Top 10 Universities for Medicine in the USA: A Comprehensive Guide Published at:- What is WSM Coin: A New Emerging Memecoin? Published at:- March 2024's Cosmic Calendar: 6 Exciting Astronomical Events to Watch Out For Published at:- UNESCO World Heritage Day A Global Perspective on Cultural Preservation Published at:- Top Antivirus Apps for Android in 2024 Published at:- Expert Tips for Selecting the Perfect Antivirus App for Android Published at:- Inspiring Change: World Environment Day Campaigns Published at:- Discover Hidden Gems: Off-the-Beaten-Path Hikes in Cape Town Published at:- Java OOPS and Servlets Java Technical Interview Questions Published at:- Sports (MCQs) Published at:-

Java OOPS and Servlets Java Technical Interview Questions