Sunteți pe pagina 1din 19

J2EE Interview Questions:

J2EE stands for Java 2 Platform Enterprise Edition. It is an open and standard-based platform for development, deployment,
and management of multi-tier, web-enabled, component based and server centric enterprise applications.

There are several Application programming interfaces comprised with J2EE for a build such systems.
This document focuses on the understanding of J2EE concepts, theories, and some common coding techniques.

What You Will Learn: [show]


Most Popular J2EE Interview Questions And Answers

These J2EE questions would be helpful for experienced professionals as well.

Here we go.

Q #1) What are the components of J2EE applications?


 Client-tier components. Run on the client machine.
 Web tier components. Run on the J2EE server.
 Business tier components. Run on the J2EE server.
 Enterprise information system software (EIS software).Runs on the EIS server.
Q #2) What are the J2EE client types?
 Applets
 Application clients
 Java Web Start enabled clients, by Java Web Start technology.
 Wireless clients, based on Mobile Information Device Profile (MIDP) technology.
Q #3) What is a J2EE container?
Ans) The interface between a component and the low-level platform with specific functionality that support the component is
called a container. Application server maintains control and provides relevant services through an interface or framework calls
as a container.
Q #4) What are defined as web components?
Ans) Java Servlets and Java server pages technology components are identified as web components. Servlets dynamically
receive requests and make responses. JSP server pages also behave as Java Servlets but facilitate to create more static
contents.
Q #5) Describe the MVC on struts?
Ans) MVC stands for Model View Controller. Each section in Model View Controller can describe as follows.
 Model –Model represents the internal state of the system as a set of single or many Java Beans.
 View – Most often view is a constructed using Java Server Pages (JSP) technology.
 Controller –The controller is the process that focuses on client request receiving and produce next level of a user
interface according to the request. The primary component of the controller in the framework
is “ActionServlet” servlet class.
Q #6) Define JSF?
Ans) JSF stands for Java Server Faces. It is the user interface (UI) designing framework for Java Web Applications
developments. There are set of reusable UI components associated with JSF. Also, JSF based on Model-View-Controller
(MVC) design concepts and patterns. The automated data saving process from form to server and display at client side is also
handling by JSF.
Q #7) What is Hashtable?
Ans) Hash table is a Collection Synchronized objects. It allows Null values but not duplicate values. Hash table is like a
HashMap.
Q #8) Define Hibernate?
Ans) Hibernate is an open source object-relational mapping and query service which facilitate to write Hibernate Query
Language (HQL) scripts instead of Structured Query Language (SQL) scripts. It is faster and easy than writing native SQL.
Hibernate has more powerful object oriented contents like associations, inheritance, and polymorphism. Also, Hibernate has a
powerful composition and collections. Hibernate allows making queries using Java base approach.
Q #9) What are the identified limitation of hibernate?
Ans)
 Slower in action – In execution of HQL queries take more time than it executes directly.
 Only composite keys support available and it prevents advanced query options.
 No shared value type references available.
Q #10) What the identified advantages are of hibernate?
Ans)
 Database and vendor independence application.
 Standard Object-relational mapping support.
 Domain object mapping for a relational database.
 Better performance than Java Database Connectivity.
 Java Persistence API based applications.
Q #11) Describe ORM?.
Ans) Object-Relational mapping (ORM) can describe as follows.
The mapped objects in a Java class to the tables of the relational database using metadata which describes the database and
object mapping. The working method is to transform data from one representation to another.

Q #12) What are the advantages of Object-Relational mapping (ORM)?


Ans)
 Productivity – Reduce the time for data access coding with help of automatic code creation base on the defined
data model.
 Maintainability – All code generated from ORM are well tested. Only the developer need to create the correct
functionality
 Performance – The code generated from ORM completely manages the data access needs of the application. No
need to create any data access code to create and also the code is optimized for speed up the data access process.
 Vendor independence – The code generated from ORM is not depending on the vendor. This is to increase the
portability of the application.
Q #13) What is the use of method save()?
Ans) In hibernate this method is used to stores an object into the database. There is a check for duplicate records before
inserting it.
Q #14) What is the use of method saveorupdate()?
Ans) In hibernate this method is used to update an object using identifier. When the value for the identifier is NULL then the
method direct to call save().
Q #15) What is the difference between load() and get()?
Ans) When the object not available in either cache or database, load() thrown an exception. No null return from load().
When the object not available in either cache or database, get() returns null.

Q #16) What is mean by connection pooling?


Ans) Simply connection pooling is a mechanism to re-use the existing connections. The poolingmechanism maintains a
number of already created object connections and when there is a demand the mechanism directly use existing connection
without creating a new one.
Q #17) Define the Collection types in Hibernate?
Ans) One-to-many reference is defined as a collection. There are five main collection types associated with J2EE.They are
Set type, List type, Array type, Map type, Bag type

Q #18) Define thin client?


Ans) A program interface that does not have any operations like database queries, complex business rules or any connection
to the third-party application is called a thin client.
Q #19) Describe the file types *.ear, * .jar and *.war?
Ans)
 *.jar files – Property file contains libraries, resources and accessories are included with the *.jar file extension.
 *.war files – The files that need to development of web application (HTML, java scripts, JSP) included with a *.war
file extension.
 *.ear files – The files for Enterprise Java Beans modules for the application is save as *.ear files.
Q #20) What spring is in related to J2EE?
Ans) Spring is an open source application that reduces the complexity of enterprise application development. Spring is based
on an inversion of control or dependency injection design patterns.
Q #21) What are the advantages in the use of spring for application development?
Ans)
 Plain Old Java Object (POJO) based development facilitates to re-use existing components.
 Possible to reduce development cost by improving the productivity of the application development.
 Improve the testability of application with dependency injection.
 Improve maintainability with reduce code coupling.
 No need to have an application server and works on enterprise service.
Q #22) Discuss the benefit of Spring Framework?
Ans)
 Possibility to organize middle tier objects in an efficient way.
 Easy initialization for properties.
 Easily testable components.
 Lightweight container.
 Possibilities to use configure management service of spring in any runtime environment with whatever architectural
layer.
Q #23) Describe servlet?
Ans) Server side component that provides a powerful mechanism to create server side programs is called a servlet. There are
servlets available with a design for various protocols. Servlet is also server and platform independent. Most commonly use
protocol for the servlet is Hypertext Transfer Protocol (HTTP).Also, a servlet is a pure java object.
Q #24) Describe the phases of servlet lifecycle?
Ans)
 Class loading phase – Web container loads the servlet class file (*.class).
 Instantiation phase – By calling default no-arg constructor the servlet class gets Instantiated.
 Initialize phase – The method Init () called in this phase in only one time of the lifetime of a servlet. Servlet
configuration is assigned to the servlet.
 Request Handling phase – In this phase, only servlets spends most of the time. Servlet provides the services to
various requests by calling Service ().
 Removal phase – The destroy () function calls before servlet destruction. Garbage collection occurs later.
Q #25) What are the difference types of JSP tags?
Ans) There are 4 different types of tags associated with JSP.They are mentioned below
 Directives
 Declarations
 Scriplets
 Expressions
Q #26) Describe action form?
Ans) A java bean that is associated with single or multiple action mapping is called action form. Action form objects are
automatically populated at server end when data has been enter from the client from a user interface (UI).
Session states of a web application are maintained by action forms.

Q #27) Describe Secure Socket Layer (SSL)?


Ans) The technology that use to communicate between the web server and the web browser is called Secure Socket Layer
(SSL).More especially SSL is a protocol that describes how algorithms to be used in encryption. The technology established an
encrypted link between two parties and this link is allowed to secure transmission of sensitive information such as login
credentials, credit/debit card information and social security numbers.
Q #28) What id URL?
Ans) URL stands for Uniform Resource Locator and it is the textual reference writing standard to an arbitrary piece of data in
the World Wide Web (WWW).The general structure of URL is as follows.
protocol://host/local info
 protocol -> Protocol for fetching the object ( eg – HTTP, FTP)
 host -> Internet name of the targeted host.
 local info -> String passed to the protocol handler on the remote host. In many cases, it is a file name with
extension.
Q #29) What is URN?
Ans) URN stands for Uniform resource name. It is the unique identifier that identified an entity. But the information of where the
entity is located is not available.
Q #30) What are the phases of a servlet life cycle?
Ans) Following steps are associated with servlet life cycle.
 Loading of the servlet class.
 Instantiation of Servlet.
 Execution of init method.
 Request handling phase. In this phase service methods will call.
 Removal from service phase. In this phase destroy method will call.
Q #31) Is that Servlet is pure java object or not?
Ans) Yes, Servlet is pure java object.
Q #32) What is EJB?
Ans) EJB stands for Enterprise Java Beans. It is the server side components that executes in EJB container and encapsulates
the business logic for the enterprise application.
Q #33) What are the system services of EJB container?
Ans) EJB Container provides following system services.
 Persistence
 Security
 Transaction
 Connection pooling
 Component lifecycle management
 Threading
Q #34) What are the design principles for EJB?
Ans)
 Behavior of the EJB application is specified by interfaces
 EJB applications are loosely coupled and tired.
 Implementation is hidden from the client side.
 The EJB container supports the application developer.
 The API to the application is in session tier.
 The API to the data sources is in entity tier.
Q #35) What are the advantages of EJB components?
Ans)
 There is a support for the integration of components from different vendors.
 Possibility authored EJB components detailed knowledge of the environment.
 Possibility to assembled applications from separate components source.
 Interaction with its clients is entirely specified in terms of java interfaces.
 Portability support.
 Does not maintain resources.
Q #36) What are the Basic and subtypes of Enterprise Java Beans (EJB)?
Ans) Two main types and subtypes of EJB are as follows.
 Session Beans
 Stateful session beans
 Stateless session beans
 Entity Beans
 Bean Managed Persistence (BMP)
 Container Managed Persistence (CMP)
 Message Driven Beans
Q #37) What is the description for expression element?
Ans) The expressions that use to write dynamic content back to the client browser are called expression elements.
Q #38) What are the two types of comments supported by JSP?
Ans) There are two types of comments are supported by JSP.
 HTML comment.

 JSP comment.

Q #39) What is called JSP directive?


Ans) JSP directive is the mechanism to provide Metadata information to web container about JSP file. In the translation and
compilation phases of the JSP life cycle, these Metadata use by the web container.
Q #40) What are the different types of JSP directive?
Ans) There are 3 different types of JSP directives available.
 Page directive
 Include directive
 Taglib directive

Top 40 Popular J2EE Interview Questions and Answers You Should Read
Last Updated:April 23, 2019

J2EE Interview Questions:


J2EE stands for Java 2 Platform Enterprise Edition. It is an open and standard-based platform for development, deployment,
and management of multi-tier, web-enabled, component based and server centric enterprise applications.

There are several Application programming interfaces comprised with J2EE for a build such systems.
This document focuses on the understanding of J2EE concepts, theories, and some common coding techniques.

What You Will Learn: [show]


Most Popular J2EE Interview Questions And Answers

These J2EE questions would be helpful for experienced professionals as well.

Here we go.

Q #1) What are the components of J2EE applications?


 Client-tier components. Run on the client machine.
 Web tier components. Run on the J2EE server.
 Business tier components. Run on the J2EE server.
 Enterprise information system software (EIS software).Runs on the EIS server.
Q #2) What are the J2EE client types?
 Applets
 Application clients
 Java Web Start enabled clients, by Java Web Start technology.
 Wireless clients, based on Mobile Information Device Profile (MIDP) technology.
Q #3) What is a J2EE container?
Ans) The interface between a component and the low-level platform with specific functionality that support the component is
called a container. Application server maintains control and provides relevant services through an interface or framework calls
as a container.
Q #4) What are defined as web components?
Ans) Java Servlets and Java server pages technology components are identified as web components. Servlets dynamically
receive requests and make responses. JSP server pages also behave as Java Servlets but facilitate to create more static
contents.
Q #5) Describe the MVC on struts?
Ans) MVC stands for Model View Controller. Each section in Model View Controller can describe as follows.
 Model –Model represents the internal state of the system as a set of single or many Java Beans.
 View – Most often view is a constructed using Java Server Pages (JSP) technology.
 Controller –The controller is the process that focuses on client request receiving and produce next level of a user
interface according to the request. The primary component of the controller in the framework
is “ActionServlet” servlet class.
Q #6) Define JSF?
Ans) JSF stands for Java Server Faces. It is the user interface (UI) designing framework for Java Web Applications
developments. There are set of reusable UI components associated with JSF. Also, JSF based on Model-View-Controller
(MVC) design concepts and patterns. The automated data saving process from form to server and display at client side is also
handling by JSF.
Q #7) What is Hashtable?
Ans) Hash table is a Collection Synchronized objects. It allows Null values but not duplicate values. Hash table is like a
HashMap.
Q #8) Define Hibernate?
Ans) Hibernate is an open source object-relational mapping and query service which facilitate to write Hibernate Query
Language (HQL) scripts instead of Structured Query Language (SQL) scripts. It is faster and easy than writing native SQL.
Hibernate has more powerful object oriented contents like associations, inheritance, and polymorphism. Also, Hibernate has a
powerful composition and collections. Hibernate allows making queries using Java base approach.
Q #9) What are the identified limitation of hibernate?
Ans)
 Slower in action – In execution of HQL queries take more time than it executes directly.
 Only composite keys support available and it prevents advanced query options.
 No shared value type references available.
Q #10) What the identified advantages are of hibernate?
Ans)
 Database and vendor independence application.
 Standard Object-relational mapping support.
 Domain object mapping for a relational database.
 Better performance than Java Database Connectivity.
 Java Persistence API based applications.
Q #11) Describe ORM?.
Ans) Object-Relational mapping (ORM) can describe as follows.
The mapped objects in a Java class to the tables of the relational database using metadata which describes the database and
object mapping. The working method is to transform data from one representation to another.

Q #12) What are the advantages of Object-Relational mapping (ORM)?


Ans)
 Productivity – Reduce the time for data access coding with help of automatic code creation base on the defined
data model.
 Maintainability – All code generated from ORM are well tested. Only the developer need to create the correct
functionality
 Performance – The code generated from ORM completely manages the data access needs of the application. No
need to create any data access code to create and also the code is optimized for speed up the data access process.
 Vendor independence – The code generated from ORM is not depending on the vendor. This is to increase the
portability of the application.
Q #13) What is the use of method save()?
Ans) In hibernate this method is used to stores an object into the database. There is a check for duplicate records before
inserting it.
Q #14) What is the use of method saveorupdate()?
Ans) In hibernate this method is used to update an object using identifier. When the value for the identifier is NULL then the
method direct to call save().
Q #15) What is the difference between load() and get()?
Ans) When the object not available in either cache or database, load() thrown an exception. No null return from load().
When the object not available in either cache or database, get() returns null.

Q #16) What is mean by connection pooling?


Ans) Simply connection pooling is a mechanism to re-use the existing connections. The poolingmechanism maintains a
number of already created object connections and when there is a demand the mechanism directly use existing connection
without creating a new one.
Q #17) Define the Collection types in Hibernate?
Ans) One-to-many reference is defined as a collection. There are five main collection types associated with J2EE.They are
Set type, List type, Array type, Map type, Bag type

Q #18) Define thin client?


Ans) A program interface that does not have any operations like database queries, complex business rules or any connection
to the third-party application is called a thin client.
Q #19) Describe the file types *.ear, * .jar and *.war?
Ans)
 *.jar files – Property file contains libraries, resources and accessories are included with the *.jar file extension.
 *.war files – The files that need to development of web application (HTML, java scripts, JSP) included with a *.war
file extension.
 *.ear files – The files for Enterprise Java Beans modules for the application is save as *.ear files.
Q #20) What spring is in related to J2EE?
Ans) Spring is an open source application that reduces the complexity of enterprise application development. Spring is based
on an inversion of control or dependency injection design patterns.
Q #21) What are the advantages in the use of spring for application development?
Ans)
 Plain Old Java Object (POJO) based development facilitates to re-use existing components.
 Possible to reduce development cost by improving the productivity of the application development.
 Improve the testability of application with dependency injection.
 Improve maintainability with reduce code coupling.
 No need to have an application server and works on enterprise service.
Q #22) Discuss the benefit of Spring Framework?
Ans)
 Possibility to organize middle tier objects in an efficient way.
 Easy initialization for properties.
 Easily testable components.
 Lightweight container.
 Possibilities to use configure management service of spring in any runtime environment with whatever architectural
layer.
Q #23) Describe servlet?
Ans) Server side component that provides a powerful mechanism to create server side programs is called a servlet. There are
servlets available with a design for various protocols. Servlet is also server and platform independent. Most commonly use
protocol for the servlet is Hypertext Transfer Protocol (HTTP).Also, a servlet is a pure java object.
Q #24) Describe the phases of servlet lifecycle?
Ans)
 Class loading phase – Web container loads the servlet class file (*.class).
 Instantiation phase – By calling default no-arg constructor the servlet class gets Instantiated.
 Initialize phase – The method Init () called in this phase in only one time of the lifetime of a servlet. Servlet
configuration is assigned to the servlet.
 Request Handling phase – In this phase, only servlets spends most of the time. Servlet provides the services to
various requests by calling Service ().
 Removal phase – The destroy () function calls before servlet destruction. Garbage collection occurs later.
Q #25) What are the difference types of JSP tags?
Ans) There are 4 different types of tags associated with JSP.They are mentioned below
 Directives
 Declarations
 Scriplets
 Expressions
Q #26) Describe action form?
Ans) A java bean that is associated with single or multiple action mapping is called action form. Action form objects are
automatically populated at server end when data has been enter from the client from a user interface (UI).
Session states of a web application are maintained by action forms.

Q #27) Describe Secure Socket Layer (SSL)?


Ans) The technology that use to communicate between the web server and the web browser is called Secure Socket Layer
(SSL).More especially SSL is a protocol that describes how algorithms to be used in encryption. The technology established an
encrypted link between two parties and this link is allowed to secure transmission of sensitive information such as login
credentials, credit/debit card information and social security numbers.
Q #28) What id URL?
Ans) URL stands for Uniform Resource Locator and it is the textual reference writing standard to an arbitrary piece of data in
the World Wide Web (WWW).The general structure of URL is as follows.
protocol://host/local info
 protocol -> Protocol for fetching the object ( eg – HTTP, FTP)
 host -> Internet name of the targeted host.
 local info -> String passed to the protocol handler on the remote host. In many cases, it is a file name with
extension.
Q #29) What is URN?
Ans) URN stands for Uniform resource name. It is the unique identifier that identified an entity. But the information of where the
entity is located is not available.
Q #30) What are the phases of a servlet life cycle?
Ans) Following steps are associated with servlet life cycle.
 Loading of the servlet class.
 Instantiation of Servlet.
 Execution of init method.
 Request handling phase. In this phase service methods will call.
 Removal from service phase. In this phase destroy method will call.
Q #31) Is that Servlet is pure java object or not?
Ans) Yes, Servlet is pure java object.
Q #32) What is EJB?
Ans) EJB stands for Enterprise Java Beans. It is the server side components that executes in EJB container and encapsulates
the business logic for the enterprise application.
Q #33) What are the system services of EJB container?
Ans) EJB Container provides following system services.
 Persistence
 Security
 Transaction
 Connection pooling
 Component lifecycle management
 Threading
Q #34) What are the design principles for EJB?
Ans)
 Behavior of the EJB application is specified by interfaces
 EJB applications are loosely coupled and tired.
 Implementation is hidden from the client side.
 The EJB container supports the application developer.
 The API to the application is in session tier.
 The API to the data sources is in entity tier.
Q #35) What are the advantages of EJB components?
Ans)
 There is a support for the integration of components from different vendors.
 Possibility authored EJB components detailed knowledge of the environment.
 Possibility to assembled applications from separate components source.
 Interaction with its clients is entirely specified in terms of java interfaces.
 Portability support.
 Does not maintain resources.
Q #36) What are the Basic and subtypes of Enterprise Java Beans (EJB)?
Ans) Two main types and subtypes of EJB are as follows.
 Session Beans
 Stateful session beans
 Stateless session beans
 Entity Beans
 Bean Managed Persistence (BMP)
 Container Managed Persistence (CMP)
 Message Driven Beans
Q #37) What is the description for expression element?
Ans) The expressions that use to write dynamic content back to the client browser are called expression elements.
Q #38) What are the two types of comments supported by JSP?
Ans) There are two types of comments are supported by JSP.
 HTML comment.

 JSP comment.

Q #39) What is called JSP directive?


Ans) JSP directive is the mechanism to provide Metadata information to web container about JSP file. In the translation and
compilation phases of the JSP life cycle, these Metadata use by the web container.
Q #40) What are the different types of JSP directive?
Ans) There are 3 different types of JSP directives available.
 Page directive
 Include directive
 Taglib directive

Top 30 OOP Concept Interview Questions Answers in Java - Object


Oriented Programming
Java is an object-oriented programming language and you will see a lot of object oriented
programming concept questions on Java interviews. The classic questions like difference
between interface and abstract class are always there but from the last couple of years more
sophisticated questions based upon advanced design principles and patterns are also asked
to check OOP knowledge of the candidate. Though, Object oriented programming questions
are more popular on Java interviews for 1 to 3 years experienced programmers. It makes
sense as well, as these are the programmers who must know the OOP basic like
Abstraction, Inheritance, Composition, Class, Object, Interface, Encapsulation etc.

If you look for Java interview questions for 2 to 4 years experienced programmer, you will
find lots of questions based upon OOP fundamentals like Inheritance and Encapsulation but
as you gain more experience, you will see questions based on object-oriented analysis and
design e.g. code a vending design machine or implement a coffeemaker in Java.
These questions are more difficult and require not only true understanding of OOP
fundamental but also about SOLID design principles and patterns.

OOPS Concept Interview Questions in Java


In this article, I am going to share you some OOPS concept based Java interview
questions which I have collected from friends and colleagues and they have seen in various
Java interviews on different companies. They are mostly asked at first few round e.g. on
screening round or on the telephonic round.

If you are senior Java developer then you already know answers to this question and I
suggest you practice more on object-oriented analysis and design skill i.e. how to do code
against a specification. If you are fresher and junior Java developer with 2 to 3 years
experience then you must revise these questions, learn if you don't know to do well on your
Java Job interviews.

What is method overloading in OOP or Java? (answer)


It's one of the oldest OOPS concept questions, I have seen it 10 years ago and still sees it
now. When we have multiple methods with the same name but different functionality then it's
called method overloading. For example. System.out.println() is overloaded as we have 6 or
7 println() method each accepting a different type of parameter.

What is method overriding in OOP or Java? (answer)


It's one of the magic of object oriented programming where the method is chose based upon
an object at runtime. In order for method overriding, we need Inheritance and Polymorphism,
as we need a method with the same signature in both superclass and subclass. A call to
such method is resolved at runtime depending upon the actual object and not the type o
variable. See the answer for more detailed discussion.

What is method hiding in Java? (answer)


When you declare two static methods with same name and signature in both superclass and
subclass then they hide each other i.e. a call to the method in the subclass will call the static
method declared in that class and a call to the same method is superclass is resolved to the
static method declared in the super class.

Is Java a pure object oriented language? if not why? (answer)


Java is not a pure object-oriented programming language e.g. there are many things you can
do without objects e.g. static methods. Also, primitive variables are not objects in Java. See
the answer for a more detailed explanation.
What are rules of method overloading and overriding in Java? (answer)
One of the most important rules of method overloading in Java is that method signature
should be different i.e. either the number of arguments or the type of arguments. Simply
changing the return type of two methods will not result in overloading, instead, the compiler
will throw an error. On the other hand, method overriding has more rules e.g. name and
return type must be same, method signature should also be same, the overloaded method
cannot throw a higher exception etc. See the answer for a full list of rules related to method
overloading and overriding in Java.

The difference between method overloading and overriding? (answer)


Several differences but the most important one is that method overloading is resolved at
compile time and method overriding is resolved at runtime. The compiler only used the class
information for method overloading, but it needs to know object to resolved overridden
method calls. This diagram explains the difference quite well, though:

Can we overload a static method in Java? (answer)


Yes, you can overload a static method in Java. You can declare as many static methods of
the same name as you wish provided all of them have different method signatures. See the
answer for more detailed explanation and code example.

Can we override static method in Java? (answer)


No, you cannot override a static method because it's not bounded to an object. Instead,
static methods belong to a class and resolved at compile time using the type of reference
variable. But, Yes, you can declare the same static method in a subclass, that will result in
method hiding i.e. if you use reference variable of type subclass then new method will be
called, but if you use reference variable of superclass than old method will be called.

Can we prevent overriding a method without using the final modifier? (answer)
Yes, you can prevent the method overriding in Java without using the final modifier. In fact,
there are several ways to accomplish it e.g. you can mark the method private or static, those
cannot be overridden.
Can we override a private method in Java? (answer)
No, you cannot. Since the private method is only accessible and visible inside the class they
are declared, it's not possible to override them in subclasses. Though, you can override
them inside the inner class as they are accessible there.

What is covariant method overriding in Java? (answer)


In covariant method overriding, the overriding method can return the subclass of the object
returned by original or overridden method. This concept was introduced in Java 1.5 (Tiger)
version and it's very helpful in case original method is returning general type like Object
class, because, then by using covariant method overriding you can return more suitable
object and prevent client side type casting. One of the practical use of this concept is in
when you override the clone() method in Java.

Can we change the return type of method to subclass while overriding? (answer)
Yes, you can, but only from Java 5 onward. This feature is known as covariant method
overriding and it was introduced in JDK 5 release. This is immensely helpful if original
method return super-class e.g. clone() method return java.lang.Object. By using this, you
can directly return the actual type, preventing client-side type casting of the result.

Can we change the argument list of an overriding method? (answer)


No, you cannot. The argument list is part of the method signature and both overriding and
overridden method must have the same signature.

Can we override a method which throws runtime exception without throws


clause? (answer)
Yes, there is no restriction on unchecked exception while overriding. On the other hand, in
the case of checked exception, an overriding exception cannot throw a checked exception
which comes higher in type hierarchy e.g. if original method is throwing IOException than
overriding method cannot throw java.lang.Exception or java.lang.Throwable.

How do you call superclass version of an overriding method in sub class? (answer)
You can call a superclass version of an overriding method in the subclass by using super
keyword. For example to call the toString() method from java.lang.Object class you can call
super.toString().

Can we override a non-static method as static in Java? (answer)


Yes, you can override the non-static method in Java, no problem on them but it should not
be private or final :)

Can we override the final method in Java? (answer)


No, you cannot override a final method in Java, final keyword with the method is to prevent
method overriding. You use final when you don't want subclass changing the logic of your
method by overriding it due to security reason. This is why String class is final in Java. This
concept is also used in template design pattern where template method is made final to
prevent overriding.

Can we have a non-abstract method inside interface? (answer)


From Java 8 onward you can have a non-abstract method inside interface, prior to that it
was not allowed as all method was implicitly public abstract. From JDK 8, you can add static
and default method inside an interface.

What is the default method of Java 8? (answer)


Default method, also known as extension method are new types of the method which you
can add on the interface now. These method has implementation and intended to be used
by default. By using this method, JDK 8 managed to provide common functionality related
to lambda expression and stream API without breaking all the clients which implement their
interfaces. If you look Java 8 API documentation you will find several useful default method
on key Java interface like Iterator, Map etc.

What is an abstract class in Java? (answer)


An abstract class is a class which is incomplete. You cannot create an instance of an
abstract class in Java. They are provided to define default behavior and ensured that client
of that class should adore to those contract which is defined inside the abstract class. In
order to use it, you must extend and implement their abstract methods. BTW, in Java, a
class can be abstract without specifying any abstract method.

What is an interface in Java? What is the real user of an interface? (answer)


Like an abstract class, the interface is also there to specify the contract of an API. It supports
OOP abstraction concept as it defines only abstract behavior. It will tell that your program will
give output but how is left to implementors. The real use of the interface to define types to
leverage Polymorphism. See the answer for more detailed explanation and discussion.

The difference between Abstract class and interface? (answer)


In Java, the key difference is that abstract class can contain non-abstract method but the
interface cannot, but from Java 8 onward interface can also contain static and default
methods which are non-abstract. See the answer for more detailed discussion as I have
described a lot of points there.
Can we make a class abstract without an abstract method? (answer)
Yes, just add abstract keyword on the class definition and your class will become abstract.

Can we make a class both final and abstract at the same time? (answer)
No, you cannot apply both final and abstract keyword at the class same time because they
are exactly opposite of each other. A final class in Java cannot be extended and you cannot
use an abstract class without extending and make it a concrete class. As per Java
specification, the compiler will throw an error if you try to make a class abstract and final at
the same time.

Can we overload or override the main method in Java? (answer)


No, since main() is a static method, you can only overload it, you cannot override it because
the static method is resolved at compile time without needing object information hence we
cannot override the main method in Java.

What is the difference between Polymorphism, Overloading, and Overriding? (answer)


This is slight tricky OOP concept question because Polymorphism is the real concept behind
on both Overloading and Overriding. Overloading is compiled time Polymorphism and
Overriding are Runtime Polymorphism.

Can an interface extend more than one interface in Java?


Yes, an interface can extend more than one interface in Java, it's perfectly valid.

Can a class extend more than one class in Java?


No, a class can only extend another class because Java doesn't support multiple
inheritances but yes, it can implement multiple interfaces.

What is the difference between abstraction and polymorphism in Java? (answer)


Abstraction generalizes the concept and Polymorphism allow you to use different
implementation without changing your code. This diagram explains the abstraction quite
well, though:
Object Oriented design principle and pattern Interview
Questions
Now let's see some OOPS concept questions based on the SOLID design principles and
GOF design patterns which take advantage of OOPS concept discussed here.

What problem is solved by Strategy pattern in Java? (answer)


Strategy pattern allows you to introduce new algorithm or new strategy without changing the
code which uses that algorithm. For example, the Collections.sort() method which
sorts the list of the object uses the Strategy pattern to compare object. Since every object
uses different comparison strategy you can compare various object differently without
changing sort method.

Which OOP concept Decorator design Pattern is based upon? (answer)


Decorator pattern takes advantage of Composition to provide new features without modifying
the original class. A very good to-the-point question for the telephonic round. This is quite
clear from UML diagram of Decorator pattern, as you can see the Component is associated
with Decorator.
When to use Singleton design pattern in Java? (answer)
When you need just one instance of a class and wants that to be globally available then you
can use Singleton pattern. It's not free of cost though because it increases coupling between
classes and makes them hard to test. This is one of the oldest design pattern questions from
Java interviews. Please see the answer for more detailed discussion.

What is the difference between State and Strategy Pattern? (answer)


Though the structure or class diagram of State and Strategy pattern is same, their intent is
completely different. State pattern is used to do something specific depending upon state
while Strategy allows you to switch between algorithms without changing the code which
uses it.

What is the difference between Association, Aggregation, and Composition in


OOP? (answer)
When an object is related to another object it called association. It has two forms,
aggregation, and composition. the former is the loose form of association where the related
object can survive individually while later is a stronger form of association where a related
object cannot survive individually. For example, the city is an aggregation of people but is
the composition of body parts.

What is the difference between Decorator, Proxy and Adapter pattern in


Java? (answer)
Again they look similar because their structure or class diagram is very similar but their intent
is quite different. Decorator adds additional functionality without touching the class, Proxy
provides access control and Adapter is used to make two incompatible interfaces work
together.

What is the 5 objects oriented design principle from SOLID? (answer)


SOLID is the term given by Uncle Bob in his classic book, the Clean Code, one of the must-
read books for programmers. In SOLID each character stands for one design principle:
S for Single Responsibility Principle
O for Open closed design principle
L for Liskov substitution principle
I for Interface segregation principle
D for Dependency inversion principle

What is the difference between Composition and Inheritance in OOP? (answer)


This is another great OOPS concept question because it tests what matters, both of them
are very important from a class design perspective. Though both Composition and
Inheritance allows you to reuse code, formerly is more flexible than later. Composition allows
the class to get an additional feature at runtime, but Inheritance is static. You can not change
the feature at runtime by substitution new implementation. See the answer for more detailed
discussion.

Read more: http://www.java67.com/2015/12/top-30-oops-concept-interview-questions-answers-java.html#ixzz5mmp5Nbr3

1. What is OOPS?
Object Oriented Programming System is the programming technique to write programs based on the real world
objects. The states and behaviors of an object are represented as the member variables and methods. In OOPS
programming programs are organized around objects and data rather than actions and logic.

2. What are the advantages of OOPS concepts?


Major advantages of OOPS programming are;

1. Simplicity: OOPS programming objects model real world objects, so the complexity is reduced and the
program structure is clear.
2. Modularity: Each object forms a separate entity whose internal workings are decoupled from other parts of
the system.
3. Modifiability: It is easy to make minor changes in the data representation or the procedures in an OO
program. Changes inside a class do not affect any other part of a program, since the only public interface
that the external world has to a class is through the use of methods.
4. Extensibility: Adding new features or responding to changing operating environments can be solved by
introducing a few new objects and modifying some existing ones.
5. Maintainability: Objects can be maintained separately, making locating and fixing problems easier.
6. Reusability: Objects can be reused in different programs.
3. What is the difference between Procedural programming and OOPS?
1. Procedural language is based on functions but object oriented language is based on real world objects.
2. Procedural language gives importance on the sequence of function execution but object oriented language
gives importance on states and behaviors of the objects.
3. Procedural language exposes the data to the entire program but object oriented language encapsulates the
data.
4. Procedural language follows top down programming paradigm but object oriented language follows bottom
up programming paradigm.
5. Procedural language is complex in nature so it is difficult to modify, extend and maintain but object oriented
language is less complex in nature so it is easier to modify, extend and maintain.
6. Procedural language provides less scope of code reuse but object oriented language provides more scope
of code reuse.
4. What are the core concepts of OOPS?
OOPS core concepts are;

1. Abstraction
2. Encapsulation
3. Polymorphism
4. Inheritance
5. Composition
6. Association
7. Aggregation
5. What is Abstraction?
Abstraction is an OOPS concept to construct the structure of the real world objects. During this construction only
the general states and behaviors are taken and more specific states and behaviors are left aside for the
implementers.

6. What is Encapsulation?
Encapsulation is an OOPS concept to create and define the permissions and restrictions of an object and its
member variables and methods. A very simple example to explain the concept is to make the member variables of
a class private and providing public getter and setter methods. Java provides four types of access level modifiers:
public, protected, no modifier and private.

7. What is the difference between Abstraction and Encapsulation?


1. “Program to interfaces, not implementations” is the principle for Abstraction and “Encapsulate what varies” is
the OO principle for Encapsulation.
2. Abstraction provides a general structure of a class and leaves the details for the implementers.
Encapsulation is to create and define the permissions and restrictions of an object and its member variables
and methods.
3. Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented
using four types of access level modifiers: public, protected, no modifier and private.
8. What is Polymorphism?
Polymorphism is the occurrence of something in various forms. Java supports various forms of polymorphism like
polymorphic reference variables, polymorphic method, polymorphic return types and polymorphic argument types.

9. What is Inheritance?
A subclass can inherit the states and behaviors of it’s super class is known as inheritance.

10. What is multiple inheritance?


A child class inheriting states and behaviors from multiple parent classes is known as multiple inheritance.

11. What is the diamond problem in inheritance?


In case of multiple inheritance, suppose class A has two subclasses B and C, and a class D has two super
classes B and C.If a method present in A is overridden by both B and C but not by D then from which class D will
inherit that method B or C? This problem is known as diamond problem.
12. Why Java does not support multiple inheritance?
Java was designed to be a simple language and multiple inheritance introduces complexities like diamond
problem. Inheriting states or behaviors from two different type of classes is a case which in reality very rare and it
can be achieved easily through an object association.

13. What is Static Binding and Dynamic Binding?


Static or early binding is resolved at compile time. Method overloading is an example of static binding.

Dynamic or late or virtual binding is resolved at run time. Method overriding is an example of dynamic binding.

14. What is the meaning of “IS-A” and “HAS-A” relationship?


“IS-A” relationship implies inheritance. A sub class object is said to have “IS-A” relationship with the super class or
interface. If class A extends B then A “IS-A” B. It is transitive, that is, if class A extends B and class B extends C
then A “IS-A” C. The “instanceof” operator in java determines the “IS-A” relationship.

When a class A has a member reference variable of type B then A “HAS-A” B. It is also known as Aggregation.

15. What is Association?


Association is a relationship between two objects with multiplicity.

16. What is Aggregation?


Aggregation is also known as “HAS-A” relationship. When class Car has a member reference variable of type
Wheel then the relationship between the classes Car and Wheel is known as Aggregation. Aggregation can be
understood as “whole to its parts” relationship.

Car is the whole and Wheel is part. Wheel can exist without the Car. Aggregation is a weak association.

17. What is Composition?


Composition is a special form of Aggregation where the part cannot exist without the whole. Composition is a
strong Association. Composition relationship is represented like aggregation with one difference that the diamond
shape is filled.

18. What is Dependency?


When one class depends on another because it uses that at some point in time then this relationship is known as
Dependency. One class depends on another if the independent class is a parameter variable or local variable of a
method of the dependent class. A Dependency is drawn as a dotted line from the dependent class to the
independent class with an open arrowhead pointing to the independent class.

19. What is the difference between Association and Dependency?


The main difference between Association and Dependency is in case of Association one class has an attribute or
member variable of the other class type but in case of Dependency a method takes an argument of the other class
type or a method has a local variable of the other class type.

20. What is a Class?


A class is the specification or template of an object.

21. What is an Object?


Object is instance of class.
Commonly Asked OOP Interview Questions | Set 1
What is Object Oriented Programming?
Object Oriented Programming (OOP) is a programming paradigm where the complete software operates as a bunch of
objects talking to each other. An object is a collection of data and methods that operate on its data.
Why OOP?
The main advantage of OOP is better manageable code that covers following.
1) The overall understanding of the software is increased as the distance between the language spoken by developers
and that spoken by users.
2) Object orientation eases maintenance by the use of encapsulation. One can easily change the underlying
representation by keeping the methods same.
OOP paradigm is mainly useful for relatively big software.
What are main features of OOP?
Encapsulation
Polymorphism
Inheritance

What is encapsulation?
Encapsulation is referred to one of the following two notions.
1) Data hiding: A language feature to restrict access to members of an object. For example, private and protected
members in C++.
2) Bundling of data and methods together: Data and methods that operate on that data are bundled together.
What is Polymorphism? How is it supported by C++?
Polymorphism means that some code or operations or objects behave differently in different contexts. In C++, following
features support polymorphism.
Compile Time Polymorphism: Compile time polymorphism means compiler knows which function should be called when
a polymorphic call is made. C++ supports compiler time polymorphism by supporting features like templates, function
overloading and default arguments.
Run Time Polymorphism: Run time polymorphism is supported by virtual functions.The idea is, virtual functions are
called according to the type of object pointed or referred, not according to the type of pointer or reference. In other
words, virtual functions are resolved late, at runtime.
What is Inheritance? What is the purpose?
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other
class.
The purpose of inheritance is Code Reuse.
What is Abstraction?
The first thing with which one is confronted when writing programs is the problem. Typically we are confronted with
“real-life” problems and we want to make life easier by providing a program for the problem. However, real-life problems
are nebulous and the first thing we have to do is to try to understand the problem to separate necessary from
unnecessary details: We try to obtain our own abstract view, or model, of the problem. This process of modeling is
called abstraction.

S-ar putea să vă placă și