Full Stack Java Interview Questions

Choose A Topic

Interview Questions

MVC and MVP both are architectural patterns that are used to develop applications.

MVC

MVC stands for Model View Controller. It is an architectural pattern that is used to develop Java Enterprise Applications. It splits an application into three logical components i.e. Model, View, and Controller. It separates the business-specific logic (Model component) from the presentation layer (View component) from each other.

The model components contain data and logic related to it. The View component is responsible for displaying model objects inside the user interface. The Controller receives the input and calls model objects based on handler mapping. It also passes model objects to views in order to display output inside the view layer.

MVP

MVP stands for Model View Presenter. It is derived from the MVC architectural pattern. It adds an extra layer (known as indirection) to the architectural pattern that splits the View and Controller into View and Presenter. The role of Controller is replaced with a Presenter. It exists at the same level as View in MVC. It contains UI business logic for the View. The invocations received from the View directly sends to the Presenter. It maintains the action (events) between View and Model. The Presenter does not directly communicate with the View. It communicates through an interface.

The major difference between MVC and MVP architectural pattern is that in MVC architectural pattern Controller does not pass the data from the Model to the View. It only notifies the View to get the data from the Model itself.

While in MVP architectural pattern the View and Model layers are connected with each other. The presenter itself receives the data from the Model and sends it to the View to show.

Another difference is that MVC is often used in web-frameworks while MVP is used in app development.

Pair programming (a fundamental aspect of programming) is an agile software development technique in which two developer works together on the same machine (system). The developer who writes the code is called the driver and the developer who reviews (checks code, proofread, and spell checks) the code is called the navigator. The programming technique is more efficient and coding mistakes reduced to the minimum. The disadvantage of pair programming is that it increases the cost.

 

4. What is CORS in MVC and how it works?

CORS stands for Cross-Origin Resource Sharing. It is a W3C standard and HTTP-header-based mechanism. It permits a server to indicate any other origins (like domain, port, etc.) instead of the requested one. In other words, it enables one website to access the resources of another website using JavaScript.

It supports secure cross-origin requests and transfers data between servers and browsers. Advanced browsers use CORS in APIs. It is flexible and safe in comparison to JSONP (JSON with Padding). It provides better web service integration.

While using the MVC to enable CORS, the same CORS service can be used but we cannot use the same CORS middleware. We can use a particular CORS for a particular action, for a particular controller, and globally for all controllers.

A pre-flight check (or request) is sent by the browser to the server (hosting the cross-origin resource) which ensures that the server will permit the actual request or not. For example, invoking the URL https://example.com through https://demo.com.

CORS stands for Cross-Origin Resource Sharing. It is a W3C standard and HTTP-header-based mechanism. It permits a server to indicate any other origins (like domain, port, etc.) instead of the requested one. In other words, it enables one website to access the resources of another website using JavaScript.

It supports secure cross-origin requests and transfers data between servers and browsers. Advanced browsers use CORS in APIs. It is flexible and safe in comparison to JSONP (JSON with Padding). It provides better web service integration.

While using the MVC to enable CORS, the same CORS service can be used but we cannot use the same CORS middleware. We can use a particular CORS for a particular action, for a particular controller, and globally for all controllers.

A pre-flight check (or request) is sent by the browser to the server (hosting the cross-origin resource) which ensures that the server will permit the actual request or not. For example, invoking the URL https://example.com through https://demo.com.

We can use the following ways to optimize the scalability and efficiency of a website:

  • Reducing DNS lookup
  • Avoiding URL redirects
  • Avoiding duplicate codes
  • Avoiding unnecessary images
  • Leveraging browser caching
  • Deferring parsing of JavaScript
  • Avoiding inline JavaScript and CSS
  • Using srcset for responsive images
  • Placing all assets on a cookie-free domain, preferably using a CDN

S.N. Basis of Comparison Get Post
1 Purpose The Get request is designed for getting data from the server. The Post request is designed for sending the data to the server.
2 Post Mechanism The request is sent via URL. The request is sent via an HTTP request body.
3 Parameter Passing The request parameters are transmitted as a query string appended to the request. The request parameters are transmitted with the body of the request.
4 Default It is the default method hence it implements automatically. We need to specify manually.
5 Capacity We can send limited data with the Get request. We can send a large amount of data with the Post request.
6 Data Type It always submits data as text. We can send any type of data.
7 Security The use of Get is safe because it is idempotent. The use of Post unsafe because it is non-idempotent.
8 Visibility of Data The data is visible to the user as it puts the data in the URL. The data is not visible to the user as it puts the data in the message body.
9 Bookmark and Caching The Get request can be bookmarked and caching. The post request cannot be bookmarked and caching.
10 Efficiency It is more efficient than post. It is less efficient.
11 Example Search is the best example of Get request. Login is the best example of a Post request.

A program may have the property of referential transparency if any two expressions in the program that have the same value can be substituted for one another anywhere in the program without changing the result of the program. It is used in functional programming. For example, consider the following code snippet:

  1. count1 = (fun(x) + y) * (fun(x) – z);
  2. temp = fun(a);
  3. count2 = temp + y * temp – z;

The variables count1 and count2 will be equal if the value of fun(x) is not reflected. If the variable count1 is not equal to the variable count2, the referential transparency is violated.

The term REST stands for Representational State Transfer. It is an architectural style that is used to create Web Services. It uses HTTP requests to access and use the data. We can create, update, read, and delete data.

An API (Application Program Interface) for a website is the code that allows two software programs to communicate with each other. It allows us to write requesting services from an operating system or other application.

A promise is an object that can be returned synchronously from an asynchronous function. It may be in the following three states:

  • Fulfilled: If a promise called the onFulfilled() method, a promise will be in fulfilled state.
  • Rejected: If a promise called the onRejceted() method, a promise will be in rejected state.
  • Pending: If a promise is not yet fulfilled or rejected, a promise will be in pending state.

A promise will be settled if and only if it is not pending.

There are the following ways to optimize the load time of a web application:

  • Optimize image size and format
  • Compress and optimize the content
  • Avoid redirects
  • Cache the web page
  • Minimize the HTTP requests
  • Optimize dependencies
  • Put stylesheet reference at the top
  • Place script reference at the bottom
  • Put JavaScript and CSS externally

CI/CD is a best practice to develop applications in which code changes more frequently and rapidly. Sometimes, it is also known as CI\CD pipeline. It is widely used in DevOps and also an agile methodology.

Continuous integration is a coding philosophy or deployment practice in which developers integrate their code in a shared repository several times a day. Because modern application requires developing code in different platforms. The goal of continuous integration is to establish an automated mechanism that builds, test, and package the application.

Continuous delivery starts where CI ends. It automatically delivers the application to the selected infrastructure. CD ensures the automated delivery of code if any changes are made in the code.

In software design, we use the following architectural design patterns:

  • Model View Controller
  • Master-Slave Pattern
  • Layered Pattern
  • Model View Presenter
  • Monolithic Architecture
  • Event-Driven Architecture Pattern

Long polling is an effective method for creating a stable server connection without using the WebSocket or Server-Side Events protocols. It operates at the top of the conventional client-server model. Note that Node.js is using the same technique as the next development model.

In this method, the client sends the request and the server responds until the connexon is open as it contains new and unique information. As soon as the server responds, a request to the client can be submitted. When the data is available, the server will return a query. It functions when the client application stops and the server ends requests.

In web design, the idea of using HTML elements to indicate what they actually are. It is known as semantic HTML or semantic markup.

Semantic HTML is HTML that represents meaning to the web page rather than just presentation. For example, tag <p> indicates that a paragraph is enclosed in it. It is both semantic and presentational because the user know what paragraph are and the browser also know how to display them. On the other hand, tags such as <b> and <i> are not semantic. They only represent how text should look. These tags do not provide any additional meaning to the markup.

Example of semantic HTML tags are header tags <h1> to <h6><abbr><cite><tt><code><blockquote><em>, etc. There are some other semantic HTML tags that are used to build a standards-compliant website.

We should use the semantic HTML for the following reasons:

  • It provides additional information about the document in which it is used. It also aids in communication.
  • Semantic tags make it clear to the browser what the meaning of a page and its content is.
  • It provides information about the contents of those tags that goes beyond just how they look on a page.
  • It gives us many more hooks for styling the content of the page.
  • The clarity of semantic tag is also communicated with search engines that ensure the right pages are delivered for the right queries.

Null: Null means a variable is assigned with a null value. If we use it with typeof operator it gives result as an object. We should never assign a variable to null because the programmer uses it to represent a variable that has no value. Note that JavaScript will never automatically assign the value to null.

Undefined: Undefined means the variable is declared but not assigned any value to it. It may be a variable itself does not exist. If we use it with typeof operator it gives the result undefined. It is not valid in JSON.

Note: Null and undefined both are primitive.

Let’s understand it through an example.

  1. var var1
  2. var var2 = null //assigning null value to the variable var2
  3. log(`var1 : ${var1}, type : ${type of(var1)}`)
  4. console.log(`var2 : ${var2}, type : ${type of(var2)}`)

When we execute the above code, it generates the following output:

  1. Var1 : undefined, type : undefined
  2. var2 : null, type : object

From the above output, we can observe that the value of var1 is undefined also its type is undefined. Because we have not assigned any value to the variable var1. The value null is assigned to the variable var2. It prints its type as abject. Since null is an assignment value and we can assign it to a variable. Therefore, JavaScript treats null and undefined relatively equally because both represent an empty value.

Both, REST and GraphQL, are API design architectures that can be used to develop web services, especially for data-driven applications.

GraphQL REST
GraphQL is an API design architecture, but with a different approach that is much flexible. REST is a robust methodology and API design architecture used to implement web services.
It follows client-driven architecture. It follows server-driven architecture.
It does not deal with the dedicated resources. It deals with the dedicated resources.
It has a single endpoint that takes dynamic parameters. It has multiple endpoints.
It provides stateless servers and structured access to resources. It provides stateless servers and flexible controlled access to resources.
It is elastic in nature. It is not rigid in nature.
It supports only JSON format. It supports XML, JSON, HTML, YAML, and other formats also.
The client defines response data that it needs via a query language. Data represented as resources over HTTP through URI.
It provides synchronous and asynchronous communication in multiple protocols such as HTTP, MQTT, AMQP. It provides synchronous communication through HTTP only.
Its design based on HTTP (status, methods, and URI). Its design based on message exchange.
It provides high consistency across all platforms. It is difficult to achieve high consistency across all platforms.
Development speed is fast. Development speed is slow.

In Java, a connection leak is a situation when the developer forgets to close the JDBC connection, it is known as connection leak. The most common type of Connection Leak experienced in Java development, is when using a Connection Pool (such as DBCP). We can fix it by closing the connection and giving special attention to the error handling code.

A session is a conversational state between client and server and it can consist of multiple requests and responses between client and server. Therefore, HTTP and web server both are stateless, the only way to maintain a session is when some unique information about the session (session-id) is passed between server and client in every request and response. We can use the following methods to maintain the session:

  • User Authentication
  • HTML Hidden Field
  • Cookies
  • URL Rewriting
  • Session Management API

Servlet Context Servlet Config
Servlet Context represents the whole web application running on a particular JVM  and common for all the servlet. Servlet Config object represents single servlet.
It is just like a global parameter associated with the whole application. It is the same as the local parameter associated with a particular servlet.
It has application-wide scope so define outside servlet tag in the web.xml file. It is a name-value pair defined inside the servlet section of web.xml files so it has servlet wide scope.
get Servlet Context() method is used to get the context object. get Servlet Config() method is used to get the config object.
To get the MIME type of a file or application session related information is stored using a servlet context object The shopping cart of a user is a specific to particular user so here we can use servlet config.

RequestDispatcher is an interface that is used to forward the request to another resource that can be HTMLJSP, or another servlet in the same application. We can also use it to include the content of another resource in the response. The interface contains two methods forward() and include().

Scroll to Top