UML interview questions – Unified Modelling Language (UML) Interview Questions in Java

UML interview questions: List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.

Unified Modelling Language (UML) Interview Questions in Java

Question 1.
What is UML and how is it useful in designing large systems?
Answer:
Unified Modeling Language (UML) is a notational language that comprises several tools and techniques to support object-oriented development. UML captures scenarios (use case diagram), object interactions (sequence diagram), class interactions (class diagrams) and object states (state diagrams). UML helps in designing large and complex systems. It starts with an analysis of business requirement and coming up with basic business flow chart and static diagrams i.e. use case diagrams which gives a pictorial view of business requirements and captures scenarios.

The next step is Interaction diagrams, which mainly consist of Sequence diagrams. A Sequence diagram tells how objects interact with each other through message passing in most importantly in what sequence. Then classes are identified of the system with various class identification approaches like ‘Noun Phrase Approach’, ‘CRC Approach’, this exercise results in UML class diagrams.
A modular approach helps in breaking down the complex system where each module can further be divided into components like classes and objects. Once the whole system is defined in terms of reusability of objects, omitting unnecessary objects and classes. The building of skeleton code on best practices of coding, like the pattern-based approach, helps in the foundation of efficient code.

Question 2.
Is UML useful for procedural programming?
Answer:
Procedural programming is an unstructured way of programming that consists of a set of procedures/method calls/instructions to be executed sequentially in such a way to attain the objective of a program.UML can help here in a very basic way in laying out the sequence of executions of instructions.

Question 3.
What are different notations used in UML?
Answer:
UML predominantly includes static and dynamic model diagrams and each diagram has its own set of notations. In brief, the classification of these diagrams
is given as below: #
Structure Diagrams include the Class Diagram, Object Diagram, Component Diagram, Composite Structure Diagram, Package Diagram, and Deployment Diagram.

Behavior Diagrams include the Use Case Diagram (used by some methodologies during requirements gathering); Activity Diagram, and State Machine Diagram.

Interaction Diagrams, all derived from the more general Behavior Diagram, include the Sequence Diagram, Communication Diagram, Timing Diagram, and Interaction Overview Diagram.

Question 4.
What is a Use case and an Actor?
Answer:
A Use case represents a particular scenario that corresponds to the functional requirement (s) of a system to be designed and developed. An Actor is a user/external program or a system (anyone or anything), which interacts with a system. An Actor may input/receive or both (input and receive) information from the system.
In the diagram shown below, a scenario of buying a bus/train/tram ticket from a vending machine is captured through a use case diagram. Here actor is a ‘Customer’ as shown by a stickman and in ovals, all use cases have been documented.

Question 5.
How to identify an Actor?
Answer:
An Actor can be identified by finding an answer for the following points: Who is:

  • interacting
  • benefited
  • maintaining
  • supplying information
  • using information
  • removing information does the system use an external resource?

A good/refined set of actors of the system will arrive iteratively.

Question 6.
What is Generalization?
Answer:
In UML, a generalization relationship is a relationship in which one model element (the child) is based on another model element (the parent). Generalization relationships are used in class, component, deployment, and use case diagrams. Generalization corresponds to inheritance implementation amongst classes.

Question 7.
What is Association and how it maps into a Java class?
Answer:
An association specifies how objects are related to one another. To identify associations, look for verb and prepositional phrases like ‘part of, ‘next to’, ‘works for’ or ‘contained in’. While identifying implicit associations, a lot of common sense and general knowledge is required. It is very important to eliminate redundant

associations while designing a system. The most important aspects of associations are: Cardinality – a cardinality of one on a given relationship end generates a Java reference, for example, public class Customer

{
Purchase Purchase; ...
}

A cardinality of many (depicted as a number or *) generates a Java container:

public class Customer
{
List purchases; ...
}

Navigability – Forgiven an instance of an object on one side of an association you can access an instance on the other side. If an association can only be traversed in one direction then this will be indicated with arrows. If there are no arrows then the association is bi-directional.

Association end – A given class only sees the association through the attributes set on the association end. In other words that simple line actually represents two independent sets of data, one for each of the two classes involved. Besides cardinality and navigability, the most important attribute is the association end name. This name is used to generate the getter and setter methods and in persistent classes database column names.

The different types of associations can be Aggregation and Composition. I will describe them in more detail in the next blogs to come.

Question 8.
What is Aggregation and how it maps into a Java class?
Answer:
An Aggregation is an Association that denotes an “is part of the relationship. Take a ‘Car’, for example, it consists of an engine, a steering wheel, four tires, seats, gearbox, fuel tank, engine oil tank, air filters, etc. So all constituents of a car are parts of it.
If a car is destroyed/smashed, its parts can still be used separately as spares in other cars, so these parts have individual use even when their container entity is destroyed.
In a Java class, aggregation can be represented from the above example as class Car

{
List getTires( );
List getseats( ); 
List getAl1 parts( ); 
}

Question 9.
What is Composition and how it maps into a Java class?
Answer:
A Composition is a tight Association and denotes a “whole-part” relationship. So when an object is destroyed then all its constituents are also destroyed, these ‘parts’ have no meaning/sense in their lone existence from their ‘whole’.
The best example of Composition is a ‘Human body’ which is composed of two legs, two hands, two eyes, two ears, and so on. During the lifetime of a human being, all organs make sense being part of the whole, but once a human being is dead most of these parts are also dead unless some of his body parts are not medically reused.

Now come to map composition to Java world, the best example is the garbage collection feature of the language. While garbage collecting objects, the whole has the responsibility of preventing all its parts from being garbage collected by holding some references to them.

It is the responsibility of the whole to protect references to its parts not being exposed to the outside world. The only way to have a true composition in Java is to never let references to internal objects escape their parent’s scope.
An example of Inner class as shown in the following code snippet may give you an idea of how to implement Composition in Java.

public class Human
{
public Human( )
{
Brain brain = new Brain( );
}
private class Brain
{
. . . 
. . .
}
}

Question 10.
What is Dependency and how it maps into a Java class
Answer:
A Dependency relationship means when a class consumes/uses methods or variables from another class (supplier). So a change to supplier class affects the consumer class as well. Here supplier is independent of any changes being made to the consumer class. In UML class diagrams, a dependency relationship connector appears as a dashed line with an open arrow that points from the consumer class to the supplier class. A dependency relationship means an “import” statement.

Question 11.
What is the purpose of State machine diagrams?
Answer:
Objects have both attributes and behaviors. The attribute is also known as the state. When objects are incredibly complicated then to have a better understanding during different state changes one should develop one or more state machine
diagrams, tormer/y called state chart diagrams in UML I.x, describing how then- instances work.

Question 12.
What are different kinds of Structure diagrams?
Answer:
Structure Diagrams as part of UML2.1:

  • Class diagrams
  • Object diagrams
  • Composite structure diagrams
  • Component diagrams
  • Deployment diagrams
  • Package diagrams

Question 13.
What are different kinds of Interaction diagrams?
Answer:
The Interaction diagrams represent how objects interact with one another through message passing.
There are two kinds of Interaction Diagrams:

  1. Sequence Diagram
  2. Collaboration Diagram

Question 14.
What are different kinds of Behavior diagrams?
Answer:
Behavior Diagrams include:

  • Use Case Diagram (used by some methodologies during requirements gathering)
  • Activity Diagram
  • State Machine Diagram.