0% found this document useful (0 votes)
52 views30 pages

Java Interview Prep

Uploaded by

Oumima Kh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
52 views30 pages

Java Interview Prep

Uploaded by

Oumima Kh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

Presentation

21 November 2024 10:18

Hello, my name is Oumaima, and I am a 23-year-old software engineer.


I joined NTT Data Morocco in March 2023
In my most recent project I was working in an Italian project where Python was the core
programming language for the solution.
• We developed a data replication solution based on Kafka architecture.
• Implemented Kafka brokers, producers, and consumers for real-time data streaming.
• Collecting, instrumenting, and visualizing application metrics and telemetry data to monitor
and optimize system performance effectively. (Grafana for visualization prometheus and
opentelemetry)

I worked on another module to ensure the application's functionality and performance.


• Automated testing using Selenium.
• Dockerizing the solution for consistency across different environments.
• Building a CI/CD pipeline with GitLab CI/CD to automate testing and build processes, ensuring
high code quality and seamless continuous delivery.

Java interview prep Page 1


Core Java Concepts
12 November 2024 20:43

Lambda Expressions
Abstract method ==> a method that has no implementation
Functional interface ==> it's the interface that has only one abstract method | it can have default or static methods
Simplifies the implementation of functional interfaces and provide a cleaner, concise syntax for anonymous classes
**lambda defines the implementation of one method when the interface has only one abstract method it works fine
because java knows exactly the method that you're implementing in your lambda because there's only one
**
A feature that allows you to write anonymous methods using expressions. They provide a short way to
represent a functional interface.

EXAMPLES:
interface FuncInterface
{
// An abstract function
void abstractFun(int x);
// A non-abstract (or default) function
default void normalFun()
{
System.out.println("Hello");
}
}
class Test
{
public static void main(String args[])
{
// lambda expression to implement above
// functional interface. This interface
// by default implements abstractFun()
FuncInterface fobj = (int x)->System.out.println(2*x);
// This calls above lambda expression and prints 10.
fobj.abstractFun(5);

// Creating an ArrayList with elements


// {1, 2, 3, 4}
ArrayList<Integer> arrL = new ArrayList<Integer>();
arrL.add(1);
arrL.add(2);
arrL.add(3);
arrL.add(4);
// Using lambda expression to print all elements
// of arrL
arrL.forEach(n -> System.out.println(n));
// Using lambda expression to print even elements
// of arrL
arrL.forEach(n -> {
if (n % 2 == 0)
System.out.println(n);
});

}
}

Streams API
Streams is a set of functions that we can perform on a certain data structure. It enables us to perform operations like filtering,
mapping, reducing, and sorting.
It allows to process sequences of elements (like collections) in a functional style (functional-style operations)

Java interview prep Page 2


It allows to process sequences of elements (like collections) in a functional style (functional-style operations)
Benefits of java stream
No Storage: Streams do not store data. They don't hold it in memory, which can save resources.
Pipeline of Functions
Laziness: they don't process elements until needed. avoids unnecessary computations.
Can be Parallelized: splitting tasks across multiple threads.
Can be created from collections, arrays, Files Lines, Methods in Stream, IntStream etc.

1. Intermediate Operations (Non Terminal Operations)


These return a stream and are used to build a processing pipeline. They are lazy—no computation is performed
until a terminal operation is invoked.
a. filter()
Filters elements based on a predicate (condition).

b. map()
Transforms each element in the stream.

c. sorted()
Sorts the elements, either naturally or using a comparator.

d. distinct()
Removes duplicate elements.

Java interview prep Page 3


e. limit() and skip()
• limit(n) reduces the stream to the first n elements.
• skip(n) skips the first n elements.

2. Terminal Operations
These produce a result or a side-effect and end the pipeline.
a. collect()
Converts the elements of the stream into a collection or other data structure.

b. forEach()
Performs an action on each element.

Java interview prep Page 4


c. reduce()
Reduces the stream to a single value using an accumulator.

d. count()
Counts the number of elements in the stream.

e. anyMatch(), allMatch(), noneMatch()


• anyMatch: Checks if any element matches a condition.
• allMatch: Checks if all elements match a condition.
• noneMatch: Checks if no elements match a condition.

Java interview prep Page 5


f. findFirst() and findAny()
• findFirst: Returns the first element, if available.
• findAny: Returns any element (useful in parallel streams).

Optional class
It's a container object which may or may not contain a non-null value. Helping to avoid NullPointerExceptions.
Is a container object used to represent the presence or absence of a value.
If a value is present, isPresent() will return true and get() will return the value.
1. Creating an Optional
a. Optional.of(value)
Creates an Optional with a non-null value. Throws NullPointerException if the value is null.
Example:

b. Optional.ofNullable(value)
Creates an Optional that can hold a null value.
Example:

c. Optional.empty()

Java interview prep Page 6


c. Optional.empty()
Creates an empty Optional.
Example:

2. Checking the Presence of a Value


a. isPresent()
Returns true if the Optional contains a value; otherwise, false

b. isEmpty()
Returns true if the Optional is empty; otherwise, false. Introduced in Java 11.
Example:

3. Retrieving the Value


a. get()
Returns the value if present; otherwise, throws NoSuchElementException.
Example:

b. orElse(defaultValue)
Returns the value if present; otherwise, returns the provided default value.
Example:

c. orElseGet(Supplier)
Returns the value if present; otherwise, invokes the supplier and returns its result.
Example:

Java interview prep Page 7


d. orElseThrow()
Returns the value if present; otherwise, throws an exception.
Example:

4. Transforming the Value


a. map(Function)
Transforms the value if present and returns a new Optional. If empty, returns an empty Optional.
Example:

5. Filtering the Value


filter(Predicate)
Returns the Optional if the value matches the predicate; otherwise, returns an empty Optional.
Example:

6. Performing Actions
a. ifPresent(Consumer)
Performs the given action if a value is present.
Example:

b. ifPresentOrElse(Consumer, Runnable)
Performs the given action if a value is present; otherwise, runs the given runnable. Introduced in Java 9.
Example:

Java interview prep Page 8


Reactive Programming
Java reactive programming refers to a programming paradigm that focuses on building responsive and scalable
applications that can handle concurrent and asynchronous tasks efficiently.
=>
a system or a program is capable of managing multiple tasks at the same time (concurrent) and can perform tasks that
may not need to be completed in a specific order (asynchronous) without wasting resources or time.

1-Key Concepts of Reactive Programming


a. Asynchronous Programming
• In an asynchronous model, tasks execute independently, and you are notified (via callbacks, promises, or reactive
streams) when they complete.
• Reactive programming enables non-blocking operations where threads are not waiting for a task to complete but
are free to handle other tasks.
b. Key Terms
• Publisher: Emits a sequence of data items or a signal (completion or error).
• Subscriber: Consumes data emitted by the publisher.
• Flux: Represents 0 to N items (like a stream).
• Mono: Represents 0 or 1 item.

just() is typically used to start a reactive stream with some predefined values.

2-Common Methods in Reactive Programming


a. map()
• Purpose: Transforms each item emitted by a publisher by applying a function.
• Behavior: For every item emitted, the function is applied, and the resulting item is emitted downstream.
Example:

b. flatMap()
• Purpose: Similar to map(), but the transformation function returns a new publisher (like Mono or Flux) for each
item.
• Behavior: Emits the items from these new publishers as a single, merged stream.
Example:

Java interview prep Page 9


c. filter()
• Purpose: Filters items emitted by the publisher based on a condition (predicate).
• Behavior: Only items that satisfy the condition are emitted downstream.
Example:

d. zip()
• Purpose: Combines items from two or more publishers into a single publisher.
• Behavior: Combines items in a pairwise fashion (e.g., the first item from each stream, the second item, and so on).
Example:

e. merge()
• Purpose: Combines multiple publishers into a single stream, emitting items as soon as they arrive.
• Behavior: Order is not guaranteed because it emits items as soon as they are available.
Example:

f. concat()
Java interview prep Page 10
f. concat()
• Purpose: Concatenates multiple publishers into a single stream.
• Behavior: Ensures that the first stream completes before starting the next.
Example:

g. doOnNext()
• Purpose: Executes a side effect (logging, monitoring, etc.) when an item is emitted.
• Behavior: Does not modify the stream.
Example:

h. subscribe()
• Purpose: Subscribes to the publisher and starts consuming the stream.
• Behavior: Can handle different types of events (data, errors, or completion signals).
Example:

Java interview prep Page 11


i. interval()
• Purpose: Creates a stream that emits items at fixed time intervals.
• Behavior: Useful for periodic tasks or simulations.
Example:

Functional Interface in Java


A functional interface is an interface in Java that contains exactly one abstract method. These interfaces
are the foundation of functional programming in Java and are designed to be implemented by lambda
expressions, method references, or anonymous classes.

Key Features of Functional Interfaces


1. Single Abstract Method (SAM):
○ The functional interface must have one and only one abstract method.
○ This is sometimes referred to as the "Single Abstract Method" (SAM) rule.
2. Default and Static Methods:
○ A functional interface can have any number of default or static methods because they are
not abstract.
3. @FunctionalInterface Annotation:
○ This annotation is optional but recommended. It ensures that the interface complies with
the SAM rule. If more than one abstract method is added to a functional interface
annotated with @FunctionalInterface, the compiler will throw an error.

Java interview prep Page 12


Design Principles & Clean Code
15 November 2024 19:14

A hight quality code is a reliable maintainable code

SOLID principles
A set of design principles, to create well-structured, maintainable, and scalable software. These
principles help in building robust object-oriented designs by improving code readability, reusability, and
flexibility.

S: Single Responsibility Principle (SRP)


A class should have only one reason to change, meaning it should only have one job or responsibility.
This ensures that each class focuses on a single functionality, making the code easier to maintain and
modify.

Example:

O: Open/Closed Principle (OCP)


Classes should be open for extension but closed for modification. This ensures that the behaviour of a
class can be extended without modifying its source code.

Example:

Java interview prep Page 13


L: Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types without altering the correctness of the program.
Ensures that derived classes extend the behaviour of the base class without breaking functionality.

Example:

Java interview prep Page 14


I: Interface Segregation Principle (ISP)
A class should not be forced to implement interfaces it does not use.
Splits large interfaces into smaller, more specific ones to ensure that implementing classes only deal
with relevant methods.

Example:

Java interview prep Page 15


D?

Design Patterns
Reusable solutions for typical software design challenges
Expert object-oriented software engineers use these best practices to write more structured, manageable, and scalable code.

Key Characteristics of Design Patterns


• Reusability: Patterns can be applied to different projects and problems, saving time and effort in solving similar issues.
• Standardization: They provide a shared language and understanding among developers, helping in communication and
collaboration.
• Efficiency: By using these popular patterns, developers can avoid finding the solution to same recurring problems,
which leads to faster development.
• Flexibility: Patterns are abstract solutions/templates that can be adapted to fit various scenarios and requirements.

There are three types of Design Patterns:


• Creational Design Pattern: Focus on object creation mechanisms.

Singleton:
Ensures that a class has only one instance
All objects that use an instance of this class, use the same instance
Singleton ensures the uniqueness of a class instance and obviously the way to access that instance is unique.

Factory:
it is one of the best ways to create an object.
In the Factory model, we create an object without exposing the creation logic to the client and refer to the newly
created object using a common interface.

• Structural Design Pattern: Focus on the structure and composition of classes and objects.

Decorator:
The Decorator pattern allows a user to add new functionality to an existing object without changing its structure.
This pattern creates a Decorator class that wraps around the original class and provides additional functionality by
keeping the signature of the class methods intact.

Java interview prep Page 16


keeping the signature of the class methods intact.

• Behavioral Design Pattern: Focus on the interactions and responsibilities between objects.

Observer:
Defines a one-to-many dependency so that when one object changes state, all its dependents are notified.

Java interview prep Page 17


Clean Code in Software Development
Writing clean, understandable, and maintainable code is a skill that is crucial for every developer to master.

Java interview prep Page 18


Writing clean, understandable, and maintainable code is a skill that is crucial for every developer to master.

How to Name Variables (and other things)


Names are much easier to recall than using memory address. And, more importantly, they can give us more information
about the variable, so someone else can understand its significance.

How to Create Meaningful Names


"A name should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does
not reveal its intent." – Clean Code

Bad:

Good:

These names are so much better. They tell you what is being measured and the unit of that measurement.

Avoid Disinformation
Be careful about words that mean something specific.
Bad:

Good:

Avoid Noise Words


Noise words are the words that do not offer any additional information about the variable. They are redundant and
should be removed.
Some popular noise words are:
• The (prefix)
• Info
• Data
• Variable
• Object
• Manager
If your class is named UserInfo, you can just remove the Info and make it User. Using BookData instead of Book as class
name is just a no-brainer, as a class stores Data anyways.

Use Pronounceable Names


If you can't pronounce a name, you can't discuss it without sounding silly.
Bad:

Good:
Java interview prep Page 19
Good:

Use Searchable Names


Avoid using magic numbers in your code. Opt for searchable, named constants.

Bad:

Good:

How to Write Functions

Keep them Small


Functions should be small, really small. They should rarely be 20 lines long. The longer a function gets, it is more likely it
is to do multiple things and have side effects.

Make Sure They Just Do One Thing


"Functions should do one thing. They should do it well. They should do it only." – Clean Code

Encapsulate Conditionals in Functions


Refactoring the condition and putting it into a named function is a good way to make your conditionals more readable.

Java interview prep Page 20


Fewer Arguments
Functions should have two or fewer arguments, the fewer the better. Avoid three or more arguments where possible.

Do not use Flag Arguments


A flag argument is a Boolean argument that is passed to a function. Two different actions are taken depending on the
value of this argument.

Java interview prep Page 21


Flag arguments naturally contradict the principle of single responsibility.

Do Not Have Side Effects

It is checking the password, but when the password is valid, it is also initializing the session which is a side-effect.
You can change the name of the function to something like checkPasswordAndInitializeSession to make this effect
explicit. But when you do that, you should notice that your function is actually doing two things and you should not
initialize the session here.

Don't Repeat Yourself


Code repetition may be the root of all evil in software. Duplicate code means you need to change things in multiple
places when there is a change in logic and it is very error prone.

Java interview prep Page 22


00:00:01 Hello Internet! My name is Henrik Ylömo and today we're going to have a look at
SonarQube and software quality. Join me on this exploration of SonarQube and the pursuit of enhanced
software quality. Today the world's largest bookseller is Amazon and it's a software company. Today the
largest video streaming service is also a software company, Netflix and Youtube.

00:00:29 Today, the most popular music company is also a software company, Spotify. Today's
fastest growing entertainment companies are video game creators, again, software. You get the point.
Basically, every successful company today is a software company. And the quality of the software
matters.

00:00:53 In the context of software engineering, software quality refers to two related but
distinct concepts. Software functional quality reflects how well it complies with a given design, based
on the functional requirements. It is the degree to which the correct software was produced. Software
structural quality refers to how it meets non-functional requirements that supports the delivery of the
functional requirements

00:01:22 such as maintainability. It has a lot more to do with the degree to which the software
works as needed. There are two main approaches to software quality. Defect management and quality
attributes. A software defect can be regarded as any failure to address the end user requirements.

00:01:44 Common defects include missed or misunderstood requirements, an error in design,


functional logic, data relationship, process timing, validity checking, and coding errors.

00:01:57 There is also the Quality Model Standard of ISO 25010, which contains eight different
quality characteristics, each composed of a sub-characteristic. Additionally, the standard defines a
quality in-use model with five characteristics.

00:02:18 One of the popular methods of analyzing and determining the quality of the code is by
using static and dynamic code reviews. Static code analysis is done without executing the code. Dynamic
code analysis relies on analyzing how the code behaves during execution.

00:02:38 Static analysis involved no dynamic execution of the software under review and it can
detect possible defects in an early stage before running the program and even during programming
directly in the development environment.

00:02:54 Static analysis can be done by a machine to automatically analyze the source code and
detect non-complying rules to ensure proper coding standards and conventions are used.

00:03:10 Technical depth is a metaphor. Let's imagine you are buying a shiny new kitchen sink.
From your perspective as a customer, you have a romanticized image of how this would look. You now
sign off on the construction. You have your new sink ready. But wait, there is not much water coming.
You go down into the basement and you see water has leaked and repairs will be very costly. How
would you react towards the supplier?

00:03:41 In a similar way, some software today are created as poor and low quality solutions to
create an illusion as shiny objects that on the surface looks fine but may have dark and shady
construction. The metaphor was coined by Ward Cunningham back in 1992 and it can be compared to a
financial debt.

00:04:07 Just as financial debt, you can get your money now, and you can pay interest, and if
you're not able to pay, you go bankrupt. Same with the technical debt, you get early access to the
market,

00:04:20 If you don't do the refactoring, you're unable to maintain your software. The metaphor
is used today in several different ways, both as an estimate of how much you have in your technical
debt, but also to calculate the total cost of ownership and also assessing various business risks and

Java interview prep Page 23


debt, but also to calculate the total cost of ownership and also assessing various business risks and
explaining the concept of quality.

00:04:47 And also in a portfolio and quality perspective it's very useful to use this metaphor. In
the 1870s a ship named HMS Challenger set about the oceans to map the ocean floor. For several years
the ship sailed around the world measuring how deep the sea was, using only ropes and lead weights.

00:05:13 At the time, it was the only way to measure the ocean floor. When measuring over the
Pacific, the scientists were amazed. How deep could the sea be? Is it a hole? A thin hole? What type of
feature has it? So many questions. Many years later comes a new invention, sonar. Using submarine
hunting technology for scanning the seas makes it much easier, faster and more detailed than ever
before.

00:05:44 In a similar way, SonarQube makes the probing of large codebases much easier and
faster than ever before. SonarQube is an open-source solution developed by SonarSource for
continuous inspection of code quality by performing automatic reviews with static code analysis to
detect bugs, code smells and security vulnerabilities in many, many different programming languages.

00:06:15 SonarQube can record metric history and provide evolution graphs. SonarQube offers
reports on duplicate code, coding standards, unit tests, code coverage, code complexity, comments,
bugs, and security vulnerabilities.

00:06:33 Sonar Source has the ambition to solve problems that virtually every company
developing software is facing, with statements like code quality should be the concern of every
developer, not just a few experts. Unless you enforce a quality gate at release time, you don't have code
quality practice in place. Managers should be able to make decisions on existing risks in their portfolios.

00:07:00 SonarSource created SonarCube and also offers a cloud solution that integrates code
stored in Bitbucket, GitHub or Azure DevOps repos called SonarCloud. In SonarCloud, there is many
different types of projects, more than 80,000. You can also have lots of different filters. For example,
this is useful for checking out various specific programming languages.

00:07:25 You can also have different perspectives. You can look at the risks, for example, or you
can look at maybe maintainability and see the size of the various projects. You can sort by size and you
can see which is the largest. Here we have an example of 11 million lines of code.

00:07:44 It's something with Android or kernel of Android. You can see here you have the
reliability and you have the maintainability. You have 7000 days of technical depth. You can dig into this
and find out more. You can find specific bugs that are not complying with the given rule set.

00:08:05 You can even step down into the specific code line. You can see which rule and how you
really should be doing it according to your coding standard. Well, that's all for today, folks. I hope this
was useful. If you do like this type of content, then hit that like button below. And you can also subscribe
to my YouTube channel. So until next time, have a great one.

Java interview prep Page 24


SonarQube & Jenkins (Basics)
19 November 2024 21:00

SonarQube
SonarQube is an open-source platform for continuous code quality inspection. It performs static code analysis
to detect bugs, vulnerabilities, and code smells in your codebase. By integrating SonarQube into your
development pipeline, teams can ensure high-quality code and improve maintainability, reliability, and security.

Is a Code Quality Assurance tool that collects and analyses source code, and provides
reports for the code quality of your project. It combines static and dynamic analysis tools and
enables quality to be measured continually over time.
SonarQube reduces the risk of software development within a very short amount of time . It
detects bugs in the code automatically and alerts developers to fix them before rolling it out for production.
SonarQube also highlights the complex areas of code that are less covered by unit tests

Advantages of SonarQube
Improves Code Quality:
○ Detects potential bugs and code smells.
○ Ensures adherence to coding standards and best practices.
Supports Multiple Languages:
○ Offers support for 30+ programming languages, including Java, Python, JavaScript, C++, and more.
Continuous Integration:
○ Easily integrates with CI/CD pipelines like Jenkins, GitHub Actions, and GitLab CI.
Enhanced Security:
○ Identifies security vulnerabilities in the code.
○ Helps developers address potential threats like SQL injection and XSS.
Improves Collaboration:
○ Developers can see issues in their code in real-time, encouraging a culture of accountability.
○ Reports and dashboards foster communication among teams.
Customizable Rules:
○ Allows you to define custom rules and thresholds to suit your project's needs.
Comprehensive Reports:
○ Provides detailed reports on code coverage, complexity, and issue trends.

Code smells are specific characteristics or patterns in your code that indicate potential problems or areas
for improvement. They are not necessarily bugs or errors, but they can make your code harder to understand,
maintain, or extend over time.

Quality Gates:
Quality Gates are a set of predefined conditions that must be met for the code to be considered of high
quality. Quality Gates are defined on the SonarQube server and can be customized based on the
project’s requirements. If the Quality Gates are not met, the SonarQube server fails the analysis, and the
code is flagged as problematic.
Example:
A Quality Gate can be defined to ensure that the code coverage is above 80% and that there are
no critical vulnerabilities. If the analysis report shows that the code coverage is below 80% or that
there are critical vulnerabilities, the analysis fails, and the code is flagged as problematic.

Java interview prep Page 25


What is Continuous Integration?
It is a part of the software development process generally used in DevOps practices where users/teams
can collaborate and seamlessly work together to integrate new code changes or add new features into
the existing codebase using a version control system.
• With Continuous Integration (CI) you or your team members can automate project workflows and
simplify the process of building, testing, and deployment of software to different stages in the
environment. Here environments are nothing but Staging, Dev, Test, QA and Prod.
• Continuous Integration (CI) will improve communication, track changes, fix bugs, and reduce
conflicts in the code.

Adopting CI will help to improve code and software quality in a phase wise manner.
By automating the CI software developers can spend more time on tasks related to new code additions
and fixing bugs rather than focusing on infrastructure deployment.

By adopting CI developers or teams can identify and fix bugs before they are merged into the main
codebase and released to production. This will also reduce the time it takes to release new updates by
automating the build and test process.

With implementation of CI you or your team member can have the complete visibility and transparency
of the development process. Anyone who has access on the CI can identify and track the process of build
and test. With that information team users can identify bottlenecks and can find areas for improvement.

Continuous Integration Benefits


CI simplifies the process of automating the tasks. These are some benefits that you should be aware of:
Reduce Manual Tasks: CI helps in reducing the manual tasks involved in the software development
process. By reducing the manual activities in the projects save lot of time and efforts.
Code Integrations and Improvements: Help to improve code quality by automating the testing of
code changes and by providing developers with feedback on their changes.
Collaboration with Team members: If you are in a team working together with your team members
then this CI will help your developers team to share and integrate their work. This will improve
communication and reduce conflicts in the code.
Visibility and Transparency: CI provides developers with visibility of code change. This helps
developers to quickly identify the changes and to fix problems asap.
Faster Release: When you set up continuous integration with continuous deployment, your changes
will be live whenever you make changes to your code and push it to the release branch.

Java interview prep Page 26


Git and Maven (Advanced)
20 November 2024 10:42

GIT
Git is a distributed version control system that is used for tracking changes in source code during the
development process. It is used to coordinating work among programmers.
and version control is a system that tracks changes to files over a period of time.

The difference with other version-control systems


What makes it different with other version-control system is that it is distributed, means that in other non-
distributed VCS there is one repository where everyone checks out something, make changes, and send those
changes to back to the same repository.
In git, there can be more than one central repository, but instead of checking out from that repository, people
make a local copy of that central repository on, make changes to that copy, and then send those changes back to
the central repository.
By that analogy, one of the major differences is that you can work without the internet with distributed VCS
because once you have your local copy, you can checks out something and make changes without the internet. Of
course, you will need the internet to send those changes back to the central repository or another repository.

Git Areas:
There are 3 main areas:
Working directory, Staging area, and repository

Working Directory
Is the area where you are currently working. It is where your files live.
This area is the “untracked” area of git. Any changes to files will be seen in this area marked. If you don't explicitly
save these changes to git, you will lose it. It happened because git is not aware of changes in the working
directory until you tell git to pay attention to them.
If you make changes in the working directory, git will recognized that they are changed, but it won't save anything
until you use git add command to make git start tracking all your changes in staging area.
You can see what files are in your working directory by using command git status. This command will show you
files in the working directory (untracked files) and files in the staging area (tracked files)

Staging Area
This is the area when git starts to track all the changes that occur in files. In this area too, git will save the changes
that happen that are reflected in .git directory. In this area, git will track all the changes that are happening to all
the files that are added, but, git won’t know track all the changes that are happening to a new file that you add
unless you explicitly add them too.

Java interview prep Page 27


To make git track all the changes in files, you use git add <filename> .

Local Repository
This area is everything that is in your .git directory. To add items from the staging area to this area, you can use git
command. A commit is simply checkpoint telling git to track all the changes that are happened based on our last
commit as a comparison. After you commit, your staging area will be empty.

Git vs GitHub/GitLab
• Git: A version control system for tracking changes.
• GitHub/GitLab: Cloud-hosted platforms that provide Git repositories with additional collaboration features
like pull requests, CI/CD, and project management.

Basic Commands
1. Initialize a repository:

2. Check repository status:

3. Add changes to staging area:

4. Commit changes:

Java interview prep Page 28


5. View history:

Branching Strategy
It's a strategy that the software development teams adopt for(writing, merging and deploying code)
It lays down a set of rules to follow in the development process and how to interact with a shared
codebase

1. Git Flow
Best for: Large projects with scheduled releases.
Structure:
• Main branch: Holds the stable production-ready code.
• Develop branch: Used for integrating new features.
• Feature branches: Created for specific features based on the develop branch.
• Release branches: Staging area before release.
• Hotfix branches: For urgent production fixes.
Example:
• A developer creates a feature branch:

• Merges it back into develop after completion:

This strategy provides clear roles for branches but can be complex for small teams.

2. Trunk-Based Development (TBD)


Best for: Fast-paced teams practicing continuous integration (CI). (Frequent releases and agility)
Structure:
• Developers commit directly to the main branch or use short-lived branches merged quickly (within
a day).
• Uses feature flags to enable or disable incomplete features in production.
Example:
• Commit small changes frequently:

This strategy minimizes merge conflicts but requires strong CI/CD pipelines.

To learn more about Git Areas You can check this Blog below:
4. Feature Branching
What is Git? How does it work? (part 1) | by Farhan Amin | Medium
Best for: Teams working on isolated features. (Small projects or isolated features)
Structure:
• Each feature or bug fix gets its own branch.
• Merges happen only after thorough testing and reviews.
Example:
• Start working on a new feature:

• Merge into main after testing:

Advantage: Simplicity and isolation of work.

Fetch vs Pull
• git fetch: Downloads updates from the remote repository but doesn't merge them into your local
branch.
• git pull: Combines fetch and merge, automatically merging remote changes into your branch.

Merge
Combines two branches without changing their history.
It creates a new commit that shows where the two branches came together.

Rebase
Moves your branch to start from the latest changes of another branch.
It rewrites the commit history to make it linear and clean.

Fast Forward Merge


If no new commits have been added to the main branch since the feature branch was created, merging
feature into main results in a fast-forward merge. Git simply moves the main branch pointer to the
latest commit on feature, maintaining a linear history without creating a new merge commit.

Cherry-Picking
Cherry-picking in Git allows you to apply a specific commit from one branch onto another, without

Java interview prep Page 29


Cherry-picking in Git allows you to apply a specific commit from one branch onto another, without
merging the entire branch.
you're working on the main branch and realize that a specific commit from the feature branch contains a
bug fix you need. Instead of merging the entire feature branch, you can cherry-pick that single commit.

Stashing:
Definition: Stashing in Git allows you to temporarily save your uncommitted changes, providing a clean
working directory.
You're working on a new feature but need to switch to another branch to address a critical bug. Your
current changes aren't ready to commit, so you decide to stash them.

What is Maven?
Maven is a build automation and project management tool primarily used in Java development. It
simplifies the process of managing a project's build, reporting, and documentation through the Project
Object Model (POM).

Key Features of Maven:


• Dependency Management: Maven automatically handles the inclusion of external libraries and
dependencies required for a project, reducing manual efforts.
• Standardized Project Structure: It enforces a consistent project layout, making it easier for
developers to navigate and understand the project's organization.
• Build Automation: Maven automates repetitive tasks such as compilation, testing, packaging, and
deployment, streamlining the development workflow.

POM
The Maven Project Object Model (POM) is a fundamental concept in Apache Maven.
The POM is an XML file that contains information about project configuration and project dependencies,
including their versions, which are used by Maven to build the project.
The POM file specifies dependencies, build plugins, and other project-related settings.

Maven uses the POM to manage the project's dependencies and build lifecycle, and plugins.
When you run Maven commands, it reads the pom.xml file to understand how to compile, test, and
package the code. It also resolves and downloads the necessary dependencies specified in the POM
from remote repositories.

Java interview prep Page 30

You might also like